using UnityEngine;
using System.Linq;
using System.Collections.Generic;

public class SafeTile : Tile
{
    [SerializeField] private Transform centerPlacementPoint;
    [SerializeField] private Transform[] placementPoints;

    protected Queue<Transform> placementQueue = new Queue<Transform>();
    protected Dictionary<PlayerTypes, PlayerTileData> playerTypesDict = new Dictionary<PlayerTypes, PlayerTileData>();

    public int PlayerTypesCount => playerTypesDict.Count;
    public bool HasMoreThanOnePlayerType => playerTypesDict.Count > 1;
    public List<PlayerPawn> GetPlayerPawns(PlayerTypes playerType) => playerTypesDict[playerType].playerPawns.Values.ToList();

    public bool ContainsPlayerType(PlayerTypes playerType) => playerTypesDict.ContainsKey(playerType);

    private void Awake()
    {
        foreach (var placement in placementPoints)
        {
            placementQueue.Enqueue(placement);
        }
    }

    public override void InitPlayerPawn(PlayerPawn playerPawn, PlayerTypes playerType)
    {
        // PlayerPawn = playerPawn;
        if (!playerTypesDict.ContainsKey(playerType))
        {
            playerTypesDict.Add(
            playerType, 
                new PlayerTileData { playerCount = 1,
                playerPawns = new Dictionary<int, PlayerPawn>()
            });

            playerTypesDict[playerType].playerPawns.Add(playerPawn.PlayerId, playerPawn);
            playerTypesDict[playerType].commonPlacementTransform = placementQueue.Dequeue();

            Debug.Log($"targetSafeTile. Adding player {playerType} {playerTypesDict[playerType].playerCount} tileName: {name}");
        }
        else
        {
            playerTypesDict[playerType].playerCount++;
            Debug.Log($"targetSafeTile. Adding player {playerType} {playerTypesDict[playerType].playerCount}, tileName: {name}");
            playerTypesDict[playerType].playerPawns.Add(playerPawn.PlayerId, playerPawn);
        }
    }

    public void UpdateSafeZonePlayerData(PlayerTypes playerType, PlayerPawn playerPawn)
    {
        if (playerTypesDict.ContainsKey(playerType))
        {
            if (playerTypesDict[playerType].playerCount > 0)
            {
                Debug.Log($"targetSafeTile. Removing player {playerType} {playerTypesDict[playerType].playerCount}, tileName: {name}");
                playerTypesDict[playerType].playerCount--;
                Debug.Log($"targetSafeTile. Removing player {playerType} after: {playerTypesDict[playerType].playerCount}");
                playerTypesDict[playerType].playerPawns.Remove(playerPawn.PlayerId);

                if (playerTypesDict[playerType].playerCount == 0)
                {
                    placementQueue.Enqueue(playerTypesDict[playerType].commonPlacementTransform);
                    playerTypesDict.Remove(playerType);
                    Debug.Log($"targetSafeTile. Removing player {playerType}");
                    lastOccupiedIndex--;
                }
            }
        }
    }

    public PlayerTypes GetFirstPlayerType() => playerTypesDict.Keys.FirstOrDefault();

    public int GetPlayerPawnsCountInTile(PlayerTypes playerType)
    {
        return playerTypesDict[playerType].playerPawns.Count;
    }

    public PlayerPawn IterateAndGetPlayerPawn(PlayerTypes playerType, int index)
    {
        return playerTypesDict[playerType].playerPawns[index + 1];
    }

    public Transform GetPlacementPoint(PlayerTypes playerType)
    {
        return playerTypesDict[playerType].commonPlacementTransform;
    }
}
