import type { NextApiRequest } from "next"
import { Server } from "socket.io"
import { NextApiResponseWithSocket } from "../../interfaces/NextApiSocket"

const SocketHandler = (req: NextApiRequest, res: NextApiResponseWithSocket) => {
  if (res.socket.server.io) {
    console.log("Socket is already running " + req.url)
  } else {
    console.log("Socket is initializing " + req.url)
    const io = new Server(res.socket.server)
    res.socket.server.io = io

    io.on("connection", (socket) => {
      socket.on("input-change", (msg) => {
        socket.broadcast.emit("update-input", msg)
      })
      // console.log(socket.id)
      // console.log(socket)
      // ...

      socket.on("test", (payload) => {
        console.log("Got test:", payload)
        // ...
      })

      socket.emit("test2", "lol")
    })
  }
  res.end()
}

export default SocketHandler
