package routes

import (
	"fmt"
	"net/http"
	"time"

	"github.com/gorilla/websocket"

	"github.com/AFK_AlignedFamKernel/afk_monorepo/pixel-backend/core"
	routeutils "github.com/AFK_AlignedFamKernel/afk_monorepo/pixel-backend/routes/utils"
)

var WsMsgPool []map[string]string

func InitWebsocketRoutes() {
	http.HandleFunc("/ws", wsEndpoint)
	http.HandleFunc("/ws-msg", wsMsgEndpoint)
}

func wsMsgEndpoint(w http.ResponseWriter, r *http.Request) {
	// TODO: Only allow consumer to send messages
	msg, err := routeutils.ReadJsonBody[map[string]string](r)
	if err != nil {
		routeutils.WriteErrorJson(w, http.StatusBadRequest, "Invalid request body")
		return
	}

	WsMsgPool = append(WsMsgPool, *msg)
	routeutils.WriteResultJson(w, "WS message added to queue")
}

func StartWebsocketServer() {
	// Send all messages in the pool every 5 seconds
	timer := 5
	for {
		msgPoolCopy := make([]map[string]string, len(WsMsgPool))
		copy(msgPoolCopy, WsMsgPool)
		WsMsgPool = WsMsgPool[:0]
		routeutils.SendWebSocketMessages(msgPoolCopy)
		time.Sleep(time.Duration(timer) * time.Second)
	}
}

func wsReader(conn *websocket.Conn) {
	for {
		// TODO: exit on close in backend?
		// TODO: handle different message types
		messageType, p, err := conn.ReadMessage()
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println("WS message received: ", messageType, string(p))
	}
}

func wsEndpoint(w http.ResponseWriter, r *http.Request) {
	upgrader := websocket.Upgrader{
		ReadBufferSize:  core.AFKBackend.BackendConfig.WebSocket.ReadBufferSize,
		WriteBufferSize: core.AFKBackend.BackendConfig.WebSocket.WriteBufferSize,
	}
	upgrader.CheckOrigin = func(r *http.Request) bool { return true }

	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		fmt.Println(err)
		return
	}

	core.AFKBackend.WSConnectionsLock.Lock()
	core.AFKBackend.WSConnections = append(core.AFKBackend.WSConnections, ws)
	core.AFKBackend.WSConnectionsLock.Unlock()
	wsReader(ws)
}
