package handlers

import (
	"customServer/internal/protocol"
	"fmt"
	"net"
)

// Dispatcher Function
// Note: We might pass dependencies here if we want to avoid globals,
// but for this refactor we'll stick to dispatching to handler functions.

func Dispatch(conn net.Conn, packetID int64, requestNumber int32, requestData []byte) ([]byte, int) {
	fmt.Printf("[Dispatcher] Dispatching Request %d (PacketID: %d)\n", requestNumber, packetID)

	switch requestNumber {
	// Auth
	case 400:
		return HandleAsyncAuthRequest(conn, requestData)
	case 401:
		return HandleAsyncDisconnectRequest(conn, requestData)
	case 408:
		return HandleAskServerStatisticsRequest(conn, requestData)

	// System / Buddies
	case 515:
		return HandleAsyncBuddyListRequest(conn, requestData)
	case 560:
		return HandleAsyncIgnoreListRequest(conn, requestData)

	// Lobby
	case 600:
		return HandleEnterLobbyRequest(conn, requestData)
	case 604:
		return HandleLobbyPlayerListRequest(conn, requestData)
	case 607:
		return HandleLobbyCreateGameRequest(conn, requestData)
	case 609:
		return HandleLobbyGameListRequest(conn, requestData)
	case 610:
		return HandleLobbyJoinGameRequest(conn, requestData)
	case 622:
		return HandleObservableGameListRequest(conn, requestData)

	// Game
	case 511:
		return HandleWhatsNewPussycatRequest(conn, requestData)
	case 608:
		// Note: 608 is LobbyGameCreatedRequest (OUT), 607 is IN.
		// If client sends 608, it's weird.
		return nil, 0

	// System / Ping
	case 777:
		return HandlePingRequest(conn, requestData)

	default:
		fmt.Printf("[Dispatcher] Unknown Request %d\n", requestNumber)
		return nil, 0
	}
}

// Helper to encode varint for handlers that need it locally
func encodeVarint(v uint64) []byte {
	return protocol.EncodeVarint(v)
}
