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
    }

    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"));
        }
    }
}
