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

public class HealthManager : MonoBehaviour {

	public static float lifePoints;
	public bool isDead = false;

	public Animator anim;

	//public SphereCollider collider;
	public Image bar;
	public float timeLimit = 30;
	float timer;
	bool isSafe = false;
	// Use this for initialization
	void Start () {
		lifePoints = 30;
		timer = timeLimit;
		//collider = GetComponent<SphereCollider> ();
	}
	
	// Update is called once per frame
	void Update () {
		bar.fillAmount  = timer / timeLimit;
		//if (lifePoints <= 0) {
		//	isDead = true;
		//}
		//timeLimit -= Time.deltaTime;
		print(timer);
		if (timer <= 0 && !isDead) {
			isDead = true;
			anim.SetTrigger ("isDead");

			Destroy (gameObject,10f);
		}
		if (!WallsManager.allHeaven && !isSafe) {
			timer -= Time.deltaTime;
		}
		if (WallsManager.allHeaven) {
			timer = timeLimit;
		}

	}
	void receiveDamage(float x){
		lifePoints -= x;
	}
	void healPlayer(float x){
		lifePoints += x;
	}


	void OnTriggerEnter(Collider coll){
		if (coll.gameObject.tag == "Camp") {
			timer = timeLimit;
			isSafe = true;
		}
	}
	void OnTriggerExit(Collider coll){
		Debug.Log(coll.gameObject.tag);
		if (coll.gameObject.tag == "Camp") {
			timer = timeLimit;
			isSafe = false;
		}
	}
}