using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

	int health;
	int maxHealth = 100;

	public GUIText healthText;

	//Game over
	int gameOverTimerMax = 60;
	int gameOverTimer;
	int layer = 0;
	GameObject player;

	// Use this for initialization
	void Start () {
		health = maxHealth;
		gameOverTimer = gameOverTimerMax;

		player = GameObject.Find ("Player");
	}
	
	// Update is called once per frame
	void Update () {
		healthText.text = "Vitality: " + health + "%";

		//Make the text redder when we take more damage
		healthText.color = new Color(255, 255 - (maxHealth - health)*2, 255 - (maxHealth - health)*2);

		if(health <= 0){
			gameOver ();
		}

		//Close the game
		if(Input.GetKeyDown (KeyCode.Escape)){
			Application.Quit ();
		}
	}

	public void loseHealth(int amount){
		health -= amount;
	}

	void gameOver(){
		gameOverTimer --;
		if(gameOverTimer <= 0){
			layer ++;
			gameOverTimer = gameOverTimerMax;
		}

		if(this.layer == 0){
			GameObject.Find("YellowLayer").GetComponent<Layer>().fall();
		}
		else if(this.layer == 1){
			GameObject.Find("RedLayer").GetComponent<Layer>().fall();
		}
		else if(this.layer == 2){
			GameObject.Find("BlueLayer").GetComponent<Layer>().fall();
		}
		else if(this.layer == 3){
			GameObject.Find("GreenLayer").GetComponent<Layer>().fall();
		}

		else if(layer >= 10 && layer < 13){
			player.GetComponent<PlayerMovement>().teleportTo(new Vector3(0, 0, -1000), "red");
		}
		else if(layer == 13){
			player.renderer.enabled = false;
			GameObject.Find ("Hologram").renderer.enabled = false;
		}
		else if(layer == 15){
			//Pass the score to the next scene
			GameObject.Find ("Score").GetComponent<PassScore>().score = GameObject.Find ("ScriptContainer").GetComponent<ScoreManager>().getScore ();
			Application.LoadLevel ("GameOver");
		}

	}
}
