package gameapi

import (
	"net/http"

	"hyapp/services/gateway-service/internal/auth"
	"hyapp/services/gateway-service/internal/client"
	"hyapp/services/gateway-service/internal/transport/http/httproutes"
)

// Handler owns game-facing HTTP endpoints.
// gateway only translates app/user context into game-service RPCs; game session state stays in game-service.
type Handler struct {
	gameClient        client.GameClient
	userAuthClient    client.UserAuthClient
	userProfileClient client.UserProfileClient
	walletClient      client.WalletClient
	statisticsClient  client.StatisticsClient
	jwtVerifier       *auth.Verifier
	// 热游固定回调地址先进入 hyapp gateway；chatapp3 老 token 需要原样转给 chatapp3，hyapp token 继续落到 game-service。
	hotgameCompatHTTPClient *http.Client
}

type Config struct {
	GameClient        client.GameClient
	UserAuthClient    client.UserAuthClient
	UserProfileClient client.UserProfileClient
	WalletClient      client.WalletClient
	StatisticsClient  client.StatisticsClient
	JWTVerifier       *auth.Verifier
}

func New(config Config) *Handler {
	return &Handler{
		gameClient:              config.GameClient,
		userAuthClient:          config.UserAuthClient,
		userProfileClient:       config.UserProfileClient,
		walletClient:            config.WalletClient,
		statisticsClient:        config.StatisticsClient,
		jwtVerifier:             config.JWTVerifier,
		hotgameCompatHTTPClient: hotgameCompatHTTPClient(),
	}
}

func (h *Handler) Handlers() httproutes.GameHandlers {
	return httproutes.GameHandlers{
		ListGames:                  h.listGames,
		ListRecentGames:            h.listRecentGames,
		ListExploreWinners:         h.listExploreWinners,
		GetBridgeScript:            h.getBridgeScript,
		LaunchGame:                 h.launchGame,
		GetDiceConfig:              h.getDiceConfig,
		MatchDice:                  h.matchDice,
		CreateDiceMatch:            h.createDiceMatch,
		JoinDiceMatch:              h.joinDiceMatch,
		GetDiceMatch:               h.getDiceMatch,
		RollDiceMatch:              h.rollDiceMatch,
		CancelDiceMatch:            h.cancelDiceMatch,
		GetRPSConfig:               h.getRPSConfig,
		MatchRPS:                   h.matchRPS,
		CreateRPSMatch:             h.createRPSMatch,
		JoinRPSMatch:               h.joinRPSMatch,
		GetRPSMatch:                h.getRPSMatch,
		RollRPSMatch:               h.rollRPSMatch,
		CancelRPSMatch:             h.cancelRPSMatch,
		ReportSelfGameH5Event:      h.reportSelfGameH5Event,
		GetRoomRPSConfig:           h.getRoomRPSConfig,
		ListRoomRPSChallenges:      h.listRoomRPSChallenges,
		CreateRoomRPSChallenge:     h.createRoomRPSChallenge,
		AcceptRoomRPSChallenge:     h.acceptRoomRPSChallenge,
		GetRoomRPSChallenge:        h.getRoomRPSChallenge,
		HandleCallback:             h.handleGameCallback,
		HandleZGameCallback:        h.handleZGameCallback,
		HandleAMGUser:              h.handleAMGUser,
		HandleAMGSettlement:        h.handleAMGSettlement,
		HandleHotgameGetUserInfo:   h.handleHotgameGetUserInfo,
		HandleHotgameUpdateBalance: h.handleHotgameUpdateBalance,
	}
}

func (h *Handler) ListGames(writer http.ResponseWriter, request *http.Request) {
	h.listGames(writer, request)
}

func (h *Handler) ListRecentGames(writer http.ResponseWriter, request *http.Request) {
	h.listRecentGames(writer, request)
}

func (h *Handler) ListExploreWinners(writer http.ResponseWriter, request *http.Request) {
	h.listExploreWinners(writer, request)
}

func (h *Handler) GetBridgeScript(writer http.ResponseWriter, request *http.Request) {
	h.getBridgeScript(writer, request)
}

func (h *Handler) LaunchGame(writer http.ResponseWriter, request *http.Request) {
	h.launchGame(writer, request)
}

func (h *Handler) GetDiceConfig(writer http.ResponseWriter, request *http.Request) {
	h.getDiceConfig(writer, request)
}

func (h *Handler) MatchDice(writer http.ResponseWriter, request *http.Request) {
	h.matchDice(writer, request)
}

func (h *Handler) CreateDiceMatch(writer http.ResponseWriter, request *http.Request) {
	h.createDiceMatch(writer, request)
}

func (h *Handler) JoinDiceMatch(writer http.ResponseWriter, request *http.Request) {
	h.joinDiceMatch(writer, request)
}

func (h *Handler) GetDiceMatch(writer http.ResponseWriter, request *http.Request) {
	h.getDiceMatch(writer, request)
}

func (h *Handler) RollDiceMatch(writer http.ResponseWriter, request *http.Request) {
	h.rollDiceMatch(writer, request)
}

func (h *Handler) CancelDiceMatch(writer http.ResponseWriter, request *http.Request) {
	h.cancelDiceMatch(writer, request)
}

func (h *Handler) GetRPSConfig(writer http.ResponseWriter, request *http.Request) {
	h.getRPSConfig(writer, request)
}

func (h *Handler) MatchRPS(writer http.ResponseWriter, request *http.Request) {
	h.matchRPS(writer, request)
}

func (h *Handler) CreateRPSMatch(writer http.ResponseWriter, request *http.Request) {
	h.createRPSMatch(writer, request)
}

func (h *Handler) JoinRPSMatch(writer http.ResponseWriter, request *http.Request) {
	h.joinRPSMatch(writer, request)
}

func (h *Handler) GetRPSMatch(writer http.ResponseWriter, request *http.Request) {
	h.getRPSMatch(writer, request)
}

func (h *Handler) RollRPSMatch(writer http.ResponseWriter, request *http.Request) {
	h.rollRPSMatch(writer, request)
}

func (h *Handler) CancelRPSMatch(writer http.ResponseWriter, request *http.Request) {
	h.cancelRPSMatch(writer, request)
}

func (h *Handler) GetRoomRPSConfig(writer http.ResponseWriter, request *http.Request) {
	h.getRoomRPSConfig(writer, request)
}

func (h *Handler) ListRoomRPSChallenges(writer http.ResponseWriter, request *http.Request) {
	h.listRoomRPSChallenges(writer, request)
}

func (h *Handler) CreateRoomRPSChallenge(writer http.ResponseWriter, request *http.Request) {
	h.createRoomRPSChallenge(writer, request)
}

func (h *Handler) AcceptRoomRPSChallenge(writer http.ResponseWriter, request *http.Request) {
	h.acceptRoomRPSChallenge(writer, request)
}

func (h *Handler) GetRoomRPSChallenge(writer http.ResponseWriter, request *http.Request) {
	h.getRoomRPSChallenge(writer, request)
}

func (h *Handler) HandleCallback(writer http.ResponseWriter, request *http.Request) {
	h.handleGameCallback(writer, request)
}

func (h *Handler) HandleAMGUser(writer http.ResponseWriter, request *http.Request) {
	h.handleAMGUser(writer, request)
}

func (h *Handler) HandleAMGSettlement(writer http.ResponseWriter, request *http.Request) {
	h.handleAMGSettlement(writer, request)
}

func (h *Handler) HandleHotgameGetUserInfo(writer http.ResponseWriter, request *http.Request) {
	h.handleHotgameGetUserInfo(writer, request)
}

func (h *Handler) HandleHotgameUpdateBalance(writer http.ResponseWriter, request *http.Request) {
	h.handleHotgameUpdateBalance(writer, request)
}
