package state

import (
	_ "embed"
	"encoding/base64"
	"fmt"
	"sync"
)

// Global Game State Manager

type GameManager struct {
	mu             sync.Mutex
	hasActiveGame  bool
	activeGameID   int64
	activeGameData []byte
}

var GlobalManager *GameManager

func init() {
	GlobalManager = &GameManager{
		hasActiveGame: false,
		activeGameID:  0,
	}
	// Pre-decode the embedded game data
	data, err := base64.StdEncoding.DecodeString(MockGameDataBase64)
	if err != nil {
		fmt.Printf("Error decoding mock game data: %v\n", err)
	} else {
		GlobalManager.activeGameData = data
	}
}

func (m *GameManager) HasActiveGame() bool {
	m.mu.Lock()
	defer m.mu.Unlock()
	return m.hasActiveGame
}

func (m *GameManager) GetActiveGameID() int64 {
	m.mu.Lock()
	defer m.mu.Unlock()
	return m.activeGameID
}

func (m *GameManager) GetActiveGameData() []byte {
	m.mu.Lock()
	defer m.mu.Unlock()
	return m.activeGameData
}

func (m *GameManager) CreateGame(id int64) {
	m.mu.Lock()
	defer m.mu.Unlock()
	m.hasActiveGame = true
	m.activeGameID = id
	m.activeGameData = GetMockIronGameStateBytes()
}

// Embeds

//go:embed mock_gamedata.b64
var MockGameDataBase64 string
