package cache

import (
	"jannex/admin-dashboard-backend/modules/structs"
	"sync"

	"github.com/gofiber/websocket/v2"
)

var socketClients []*structs.SocketClient
var mu sync.RWMutex

func AddSocketClient(sessionId string, socketClient *structs.SocketClient) {
	mu.Lock()
	socketClients = append(socketClients, socketClient)
	mu.Unlock()
}

func DeleteClientByConn(conn *websocket.Conn) {
	mu.Lock()

	for i := 0; i < len(socketClients); i++ {
		if socketClients[i].Conn == conn {
			socketClients = removeSocketClient(socketClients, i)
			break
		}
	}

	mu.Unlock()
}

func removeSocketClient(s []*structs.SocketClient, i int) []*structs.SocketClient {
	return append(s[:i], s[i+1:]...)
}

func GetSocketClients() []*structs.SocketClient {
	mu.RLock()
	defer mu.RUnlock()

	return socketClients
}
