using UnityEngine;
using System.Collections.Generic;

public class Switch : MonoBehaviour, IActivatable, IInteractable, IObserver
{
    public delegate void ActivationStateChanged(object sender, bool newState);

    #region Events
    public event ActivationStateChanged Triggered;
    #endregion

    #region Public Properties
    public bool IsActivated
    {
        get
        {
            return _isActivated;
        }

        set
        {
            if (value != _isActivated)
            {
                _isActivated = value;

                if (Triggered != null)
                    Triggered(this, _isActivated);
            }
        }
    }
    #endregion

    #region Unity Fields
    public int id;
    public int[] dependencies;
    #endregion

    protected bool _isActivatableOnKey;

    #region Unity Methods
    protected void Awake()
    {
        Reset();

        LevelManager.Instance.RegisterSwitch(this);

        if (dependencies != null && dependencies.Length > 0)
            foreach (int depId in dependencies)
                LevelManager.Instance.ListenForSwitch(this, depId);
    }

    protected void Reset()
    {
        _isActivated = false;
        _isActivatableOnKey = true;
    }
    #endregion

    public bool Interact(object sender)
    {
        IsActivated = !IsActivated;

        return true;
    }

    public void Notify(object sender)
    {
    }

    #region Private Members
    protected bool _isActivated;
    #endregion

    public bool IsActivatableOnKey
    {
        get { return _isActivatableOnKey; }
    }


    public bool IsHandheld
    {
        get { return false; }
    }
}
