using System.Collections.Generic;
using UnityEngine;

///<summary>
/// Object handling Room logic
///</summary>
///
public class RoomHandler : MonoBehaviour
{
    [Header("Doors")]
    [SerializeField] private GameObject wallNorth;
    [SerializeField] private GameObject wallSouth;
    [SerializeField] private GameObject wallEast;
    [SerializeField] private GameObject wallWest;

    public enum Side
    {
        North,
        South,
        East,
        West
    }

    // Room Spawn Points
    [Header("Spawn Points")]
    [SerializeField] private List<GameObject> spawnPoints;
    [SerializeField] private GameObject test;
    [SerializeField] private bool allowSpawn = false;

    private readonly Dictionary<Side, DoorAnimation> doors = new();

    public void RegisterDoor(Side dir, DoorAnimation doorAnim) => doors[dir] = doorAnim;

    /// <summary>
    /// Creates entrances to corridors leading to other rooms
    /// </summary>
    /// <param name="northOpen"></param>
    /// <param name="southOpen"></param>
    /// <param name="eastOpen"></param>
    /// <param name="westOpen"></param>
    public void SetEntrances(bool northOpen, bool southOpen, bool eastOpen, bool westOpen)
    {
        wallNorth.SetActive(!northOpen);
        wallSouth.SetActive(!southOpen);
        wallEast.SetActive(!eastOpen);
        wallWest.SetActive(!westOpen);
    }

    public void ToggleAllDoors()
    {
        foreach (DoorAnimation door in doors.Values)
        {
            door.ToggleDoor();
        }
    }

    public void SetDoorState(Side dir, bool open)
    {
        if (doors.TryGetValue(dir, out DoorAnimation da))
        {
            da.ToggleDoor();
            Debug.Log("Door " + dir + " is now " + (open ? "open" : "closed"));
        }
    }

    public void SpawnEnemies(List<GameObject> enemyPrefabs)
    {
        if (!allowSpawn)
        {
            Debug.LogWarning("Enemy spawning is not allowed in this room.");
            return;
        }
        else
        {
            int i = 0;
            List<GameObject> enemyPrefabsLocal = new List<GameObject>(enemyPrefabs);
            while (enemyPrefabsLocal.Count > 0)
            {
                // Spawns enemy and removes it from the list
                GameObject enemyPrefab = enemyPrefabsLocal[0];
                enemyPrefabsLocal.RemoveAt(0);

                // Select a spawn point with round-robin
                Debug.Log("Ammount of spawn points: " + spawnPoints.Count);
                GameObject spawnPoint = spawnPoints[i % spawnPoints.Count];

                Instantiate(enemyPrefab, spawnPoint.transform.position + new Vector3(0, 1, 0), Quaternion.identity);
                Debug.Log("Spawned enemy: " + enemyPrefab.name + " at " + spawnPoint.transform.position);

                i++;
            }
        }

    }
    void Start()
    {
        int enemyCount = Random.Range(1, 4);
        List<GameObject> enemyPrefabs = new List<GameObject>();
        for (int i = 0; i < enemyCount; i++)
        {
            enemyPrefabs.Add(test);
        }

        SpawnEnemies(enemyPrefabs);
    }
}
