/// <summary>
/// Game controller.
/// This script controls game state, such as time left, points and spawners
/// </summary>

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

public class gameController : MonoBehaviour {

	public AudioClip priest;
	public AudioClip pure;
	public AudioClip baby;

	public float startTime;
	public float currentTime;
	private bool paused = false; //returns true if the game is running

	private float player1P = 0;
	private float player2P = 0;

	public Text timerDisplay;
	public Text centreDisplay;

	public Animator god;
	public Camera endCam;
	public Transform blueEnd;
	public Transform redEnd;

	public float totalAnimals;
	public float maxAnimals;

	// Use this for initialization
	void Start () {
		currentTime = startTime;
	}
	
	// Update is called once per frame
	void Update () {

		if (Input.GetButtonDown ("Cancel")) {
			Pauser ();		
		}

		if (currentTime > 0) {
			currentTime -= Time.deltaTime;

			timerDisplay.text = "Time: " + Mathf.Round (currentTime);

		} else {
			currentTime = 0;

			endCam.gameObject.SetActive (true);
			GameObject.Find ("Player 1").transform.position = blueEnd.position;
			GameObject.Find ("Player 2").transform.position = redEnd.position;

			if (player1P > player2P) {
				god.SetBool("BlueWin", true);
				god.SetTrigger ("End");
				centreDisplay.text = "Player 1 Wins! \n Press Any Key To Play Again";

				if (Input.anyKeyDown) {
					SceneManager.LoadScene (1);
				}

			} else {
				centreDisplay.text = "Player 2 Wins! \n Press Any Key To Play Again";
				god.SetBool("BlueWin", false);
				god.SetTrigger ("End");

				if (Input.anyKeyDown) {
					SceneManager.LoadScene (1);
				}
			}
				
		}

		if (player1P > player2P) {
			god.SetBool("BlueWin", true);
		} else {
			god.SetBool("BlueWin", false);
		}

	}

	void Pauser(){
		if (paused == false){
			Time.timeScale = 0;
			paused = true;
			centreDisplay.text = "Paused!";
		}else{
			Time.timeScale = 1;
			paused = false;
			centreDisplay.text = " ";
		}
	}

	void Player1Add(float p){
		player1P += p;
		totalAnimals--;
	}

	void Player2Add(float p){
		player2P += p;
		totalAnimals--;
	}

	void KillAnimal(){
		totalAnimals--;
	}
}
