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

public class EnergyBar : MonoBehaviour 
{
	private const int SHIELD_USAGE_COST = 10;
	private const int LASER_USAGE_COST  =  2;

	[SerializeField]
	private int power = 50;

	private const int MAX_POWER = 100;
	private const int POWER_INCREMENT_QTY = 2;
	private const float POWER_INCREMENT_TIME = 0.125f;

	[SerializeField]
	private int powerCoreQty = 1;

	[SerializeField]
	private Image uiCanvas;

	// Use this for initialization
	void Awake () 
	{
		//energyState = EnergyState.PowerCore;
		//powerCoreQty = 1;

		StartCoroutine (IncrementPower ());	
	}
	

	private IEnumerator IncrementPower () 
	{
		power += powerCoreQty * POWER_INCREMENT_QTY;
		if (power > MAX_POWER)
			power = MAX_POWER;

		uiCanvas.fillAmount = (float) power / (float) MAX_POWER;

		yield return new WaitForSeconds (POWER_INCREMENT_TIME);
		StartCoroutine (IncrementPower ());
	}

#if DEBUG
	public void Update ()
	{

		if (Input.GetKeyDown(KeyCode.B))
		{
			Debug.Log ("Can Fire Laser: " + CanFireLaser());
			power -= 2;

		}
		if (Input.GetKeyDown(KeyCode.N))
		{
			Debug.Log ("Can use shield: " + CanUseShield());
			power -= 10;
		}
	}
#endif

	public bool CanFireLaser ()
	{
		if (power >= LASER_USAGE_COST)
		{
			power -= LASER_USAGE_COST;
			return true;
		}

		return false;
	}

	public bool CanUseShield ()
	{
		if (power >= SHIELD_USAGE_COST)
		{
			power -= SHIELD_USAGE_COST;
			return true;
		}
		
		return false;
	}
}
