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

public class PlayerHealth : MonoBehaviour 
{

	public int StartHP = 100 ;
	public int currentHP ;
	public Slider hpSlider ;
	public Image dmgImage;
	public float flashSpeed = 5f ;                              
	public Color flashColour = new Color(1f, 0f, 0f, 0.1f) ; 

	//PlayerMovement playerMovement;
	bool damaged;
	bool isDead ;

	void Awake () 
	{
		//playerMovement = GetComponent <PlayerMovement> ();
		currentHP = StartHP ;
	}
	
	// Update is called once per frame
	void Update () 
	{
		if (damaged)
		{
			dmgImage.color = flashColour ;
		}
		else
		{
			dmgImage.color = Color.Lerp (dmgImage.color, Color.clear, flashSpeed * Time.deltaTime);
		}
		damaged = false ;

	}

	public void HPctrl (int amount)
	{
		if (amount<0)
			damaged = true;

		currentHP += amount;

		hpSlider.value = currentHP ;

		if(currentHP <= 0 && !isDead)
		{
			Death ();
		}
	}

	void Death()
	{
		isDead = true ;
		Application.LoadLevel (0);
		//anim stuff here
		//playerMovement.enabled = false;
		//game.gameover()
	}
}
