using UnityEngine;
using System.Collections;

public class TriggerBetween : MonoBehaviour {

	#region Private Variables
	[SerializeField]
	private SpriteRenderer[] _sideOne;

	[SerializeField]
	private SpriteRenderer[] _sideTwo;

	[SerializeField]
	private ConnectMechanic _mechanic;

	private PersonaControl[] _personaControls;

	private bool _sideOneVisible = false;
	#endregion


	#region Events
	void Start () {

		_personaControls = GameObject.FindObjectsOfType <PersonaControl> ();
		Switch ();
		
	}

	void Update () {

		if (Input.GetMouseButtonDown (0)) {
			_mechanic.CheckStage ();

			if (_mechanic.stage != ConnectStage.ConnectWires && _mechanic.stage != ConnectStage.CutWire)
			return;

			Collider2D c = Physics2D.OverlapPoint (Camera.main.ScreenToWorldPoint (Input.mousePosition));
			if (c != null && c == GetComponent <Collider2D> ())
				Switch ();
		}

	}
	#endregion


	#region Methods
	public void Switch () {

		_sideOneVisible = !_sideOneVisible;

		foreach (SpriteRenderer sr in _sideOne)
			sr.color = _sideOneVisible ? Color.white : new Color (1f, 1f, 1f, 0f);

		foreach (SpriteRenderer sr in _sideTwo)
			sr.color = _sideOneVisible ? new Color (1f, 1f, 1f, 0f) : Color.white;

		foreach (PersonaControl pc in _personaControls)
			pc.SetTargetOpacity (_sideOneVisible ? 0f : 1f);

	}
	#endregion
	
}
