using API.Models.DataClasses;

namespace API.Models.Game;

public enum GameState
{
    Lobby,
    Running,
    Ended
}

public class Game(Coordinates gFs, SixDigitInt gameCode)
{
    public string Id { get; init; } = Guid.NewGuid().ToString();
    public SixDigitInt GameCode { get; } = gameCode;
    public List<Player> Players { get; set; } = new();
    public int CurrentTurn { get; set; } = 1;
    public GameState State { get; private set; } = GameState.Lobby;
    public GameField Field { get; } = new(gFs);

    public bool AddPlayer(Player player)
    {
        if(Players.Count >= 2)
            return false;
        
        player.PlayerTag = Players.Count + 1;
        Players.Add(player);

        if (Players.Count == 2)
        {
            StartGame();
        }

        return true;
    }
    
    public void RemovePlayer(string playerConnectionId)
    {
        Players.RemoveAll(x => x.ConnectionId == playerConnectionId);
    }

    public Player? GetPlayerByConnectionId(string playerConnectionId)
    {
        return Players.FirstOrDefault(x => x.ConnectionId == playerConnectionId);
    }

    public Player? GetPlayerByTag(int playerTag)
    {
        return Players.FirstOrDefault(x => x.PlayerTag == playerTag);
    }

    public void StartGame()
    {
        State = GameState.Running;
    }

    public void EndGame()
    {
        State = GameState.Ended;
    }
}