using UnityEngine;
using System.Collections.Generic;

public class Door : MonoBehaviour, IObserver
{
    public GameObject door;
    public int[] dependencies;
    public bool[] matchingStates;

    public bool locked;

    private Dictionary<int, int> _switchIndexMapping;
    private bool[] _currentState;

    private void Start()
    {
        if (dependencies.Length == 0)
        {
            if (!locked)
                door.SetActive(false);

            return;
        }

        _switchIndexMapping = new Dictionary<int, int>();

        for (int i = 0; i < dependencies.Length; i++)
        {
            LevelManager.Instance.ListenForSwitch(this, dependencies[i]);
            _switchIndexMapping.Add(dependencies[i], i);
        }

        _currentState = new bool[dependencies.Length];
    }

    public void Notify(object sender)
    {
        Switch s = (Switch)sender;
        _currentState[_switchIndexMapping[s.id]] = s.IsActivated;

        bool statesMatch = MatchStates();

        door.SetActive(!statesMatch);
        //door.renderer.enabled = !statesMatch;
        //door.collider.enabled = !statesMatch;
    }

    private bool MatchStates()
    {
        bool match = true;
        for (int i = 0; i < matchingStates.Length; i++)
            match &= _currentState[i] == matchingStates[i];

        return match;
    }
}
