using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Events;

public class Tool : MonoBehaviour {
	public ToolSet set;
	public GameObject sub;
	public GameObject antiSub;
	public GameObject prefab;
	public int identity;
	public UnityEvent OnActivated;
	public UnityEvent OnDeActivated;
	private bool active = false;

	private void activateComponents(bool on)
	{
		var button = GetComponent<Button>();
		var toggle = GetComponent<Toggle>();
		if (button != null)
			button.interactable = !on;
		if (toggle != null && toggle.isOn != on)
			toggle.isOn = on;
	}
	private void activateSubs(bool on)
	{
		if (sub != null)
			sub.SetActive(on);
		if (antiSub != null)
			antiSub.SetActive(!on);
	}

	public void Activate()
	{
		if (active) return;
		active = true;
		if (set != null)
			set.Active = this;
		activateComponents(true);
		activateSubs(true);
		OnActivated.Invoke();
	}

	public void DeActivate()
	{
		if (!active) return;
		active = false;
		activateComponents(false);
		activateSubs(false);
		OnDeActivated.Invoke();
	}

	public void Toggle()
	{
		if (GetComponent<Toggle>().isOn)
			Activate();
		else
		{
			if (set != null && set.Active == this)
				set.Active = null;
			else
				DeActivate();
		}
	}
}
