package gameapi

import gamev1 "hyapp.local/api/proto/game/v1"

type gameListData struct {
	Games        []gameItemData `json:"games"`
	ServerTimeMS int64          `json:"server_time_ms"`
}

type gameItemData struct {
	GameID       string `json:"game_id"`
	PlatformCode string `json:"platform_code"`
	NameKey      string `json:"name_key"`
	Name         string `json:"name"`
	IconURL      string `json:"icon_url"`
	CoverURL     string `json:"cover_url"`
	Category     string `json:"category"`
	LaunchMode   string `json:"launch_mode"`
	Orientation  string `json:"orientation"`
	SafeHeight   int32  `json:"safe_height"`
	SafeHeightV2 int32  `json:"safeHeight"`
	MinCoin      int64  `json:"min_coin"`
	Enabled      bool   `json:"enabled"`
	Maintenance  bool   `json:"maintenance"`
	SortOrder    int32  `json:"sort_order"`
}

type gameLaunchData struct {
	SessionID    string `json:"session_id"`
	LaunchURL    string `json:"launch_url"`
	ExpiresAtMS  int64  `json:"expires_at_ms"`
	Orientation  string `json:"orientation"`
	SafeHeight   int32  `json:"safe_height"`
	SafeHeightV2 int32  `json:"safeHeight"`
	ServerTimeMS int64  `json:"server_time_ms"`
}

type gameBridgeScriptData struct {
	BridgeScriptURL       string `json:"bridge_script_url"`
	BridgeScriptURLV2     string `json:"bridgeScriptUrl"`
	BridgeScriptVersion   string `json:"bridge_script_version"`
	BridgeScriptVersionV2 string `json:"bridgeScriptVersion"`
	BridgeScriptSHA256    string `json:"bridge_script_sha256"`
	BridgeScriptSHA256V2  string `json:"bridgeScriptSha256"`
	ServerTimeMS          int64  `json:"server_time_ms"`
}

func gameListDataFromProto(resp *gamev1.ListGamesResponse) gameListData {
	if resp == nil {
		return gameListData{}
	}
	items := make([]gameItemData, 0, len(resp.GetGames()))
	for _, item := range resp.GetGames() {
		items = append(items, gameItemData{
			GameID:       item.GetGameId(),
			PlatformCode: item.GetPlatformCode(),
			NameKey:      item.GetNameKey(),
			Name:         item.GetName(),
			IconURL:      item.GetIconUrl(),
			CoverURL:     item.GetCoverUrl(),
			Category:     item.GetCategory(),
			LaunchMode:   item.GetLaunchMode(),
			Orientation:  item.GetOrientation(),
			SafeHeight:   item.GetSafeHeight(),
			SafeHeightV2: item.GetSafeHeight(),
			MinCoin:      item.GetMinCoin(),
			Enabled:      item.GetEnabled(),
			Maintenance:  item.GetMaintenance(),
			SortOrder:    item.GetSortOrder(),
		})
	}
	return gameListData{Games: items, ServerTimeMS: resp.GetServerTimeMs()}
}

func gameLaunchDataFromProto(resp *gamev1.LaunchGameResponse) gameLaunchData {
	if resp == nil {
		return gameLaunchData{}
	}
	return gameLaunchData{
		SessionID:    resp.GetSessionId(),
		LaunchURL:    resp.GetLaunchUrl(),
		ExpiresAtMS:  resp.GetExpiresAtMs(),
		Orientation:  resp.GetOrientation(),
		SafeHeight:   resp.GetSafeHeight(),
		SafeHeightV2: resp.GetSafeHeight(),
		ServerTimeMS: resp.GetServerTimeMs(),
	}
}

func gameBridgeScriptDataFromProto(resp *gamev1.GetBridgeScriptResponse) gameBridgeScriptData {
	if resp == nil {
		return gameBridgeScriptData{}
	}
	config := resp.GetConfig()
	return gameBridgeScriptData{
		BridgeScriptURL:       config.GetBridgeScriptUrl(),
		BridgeScriptURLV2:     config.GetBridgeScriptUrl(),
		BridgeScriptVersion:   config.GetBridgeScriptVersion(),
		BridgeScriptVersionV2: config.GetBridgeScriptVersion(),
		BridgeScriptSHA256:    config.GetBridgeScriptSha256(),
		BridgeScriptSHA256V2:  config.GetBridgeScriptSha256(),
		ServerTimeMS:          resp.GetServerTimeMs(),
	}
}
