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

public class NetworkPlayerCommandButton : MonoBehaviour {

	int numberOfTaps;

	Button button;
	[SerializeField]
	Image normalImage;

	[SerializeField]
	Image glowImage;

	const float ALPHA_FADE_VALUE = 0.6f;

	//const float TIME_TO_WAIT = 0.25f;

	const float FADE_IN_TIME = 0.35f;

	void Start()
	{
		button = GetComponent<Button>();
		//normalImage = GetComponent<Image>();

		// We don't want players to use the abilities until the player has started.
		SetNumberOfTaps(1);

		Invoke("ShowButton", 2f);
	}

	public void SetNumberOfTaps(int tapsRequired)
	{
		numberOfTaps = tapsRequired;
		AlphaButton();

	}

	public bool OnTap()
	{
		numberOfTaps--;

		if(numberOfTaps <= 0)
		{
			if(normalImage.color.a == ALPHA_FADE_VALUE)
			{
				ShowButton();
				return true;
			}
		}

		return false;
	}

	void AlphaButton()
	{
		button.enabled = false;
		Color colour = normalImage.color;
		colour.a = ALPHA_FADE_VALUE;
		normalImage.color = colour;
	}

	void ShowButton()
	{
		button.enabled = true;
		/*Color colour = normalImage.color;
		colour.a = 1f;
		normalImage.color = colour;*/
		StartCoroutine(ShowButtonCoroutine());
	}

	IEnumerator ShowButtonCoroutine()
	{
		// First we need to get the original sprite back to white
		Color imageColour  = normalImage.color;
		imageColour.a = ALPHA_FADE_VALUE;
		normalImage.color = imageColour;
		
		float timer = 0;
		
		while(timer <= FADE_IN_TIME * 0.6)
		{
			timer += Time.deltaTime;
			float zeroTo1 = timer / (FADE_IN_TIME * 0.6f);
			Debug.Log(zeroTo1);
			normalImage.color = Color.Lerp(imageColour, Color.white, zeroTo1 );
			yield return 0;
		}

		// Now we need to glow the glow.

		Color fadeColour = new Color(1,1,1,0);
		timer = 0;

		while(timer <= FADE_IN_TIME)
		{
			timer += Time.deltaTime;
			float zeroTo1 = timer / FADE_IN_TIME;
			Debug.Log(zeroTo1);
			glowImage.color = Color.Lerp(fadeColour, Color.white, zeroTo1 );
			yield return 0;
		}

		timer = FADE_IN_TIME;

		while(timer > 0)
		{
			timer -= Time.deltaTime;
			float zeroTo1 = timer / FADE_IN_TIME;
			Debug.Log(zeroTo1);
			glowImage.color = Color.Lerp(fadeColour, Color.white, zeroTo1 );
			yield return 0;
		}




	}
	
}
