using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MergeLightControl : MonoBehaviour {

	#region Private Variables
	private List <SpotlightControl> _spotlights;
	private SpriteRenderer _renderer;

	[SerializeField]
	private FollowerStateControl _followerState;

	[SerializeField]
	private AppearMechanic _appearMechanic;
	#endregion


	#region Events
	void Awake () {

		_renderer = GetComponent <SpriteRenderer> ();
		
	}

	void Start () {
		
		_spotlights = new List <SpotlightControl> (GameObject.FindObjectsOfType <SpotlightControl> ());
		UpdateColor ();

	}
	#endregion


	#region Methods
	public void UpdateColor () {

		Dictionary <SpotlightColor, int> _colors = new Dictionary<SpotlightColor, int> ();

		foreach (SpotlightControl spotlight in _spotlights) {
			if (!_colors.ContainsKey (spotlight.color))
				_colors.Add (spotlight.color, 1);
			else
				_colors [spotlight.color]++;
		}

		Color currentColor = new Color (1f, 1f, 1f, 0.3f);

		currentColor.r = _colors.ContainsKey (SpotlightColor.Red) ? 1f : 0f;
		currentColor.g = _colors.ContainsKey (SpotlightColor.Green) ? 1f : 0f;
		currentColor.b = _colors.ContainsKey (SpotlightColor.Blue) ? 1f : 0f;

		if (_colors.ContainsKey (SpotlightColor.None))
			currentColor.a = 0.3f - (_colors [SpotlightColor.None] / 10f);
		else
			currentColor.a = 0.3f;

		_renderer.color = currentColor;

		_followerState.UpdateState (currentColor);
		_appearMechanic.Check (currentColor);

	}
	#endregion

}
