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

public class GameManager : MonoBehaviour {

	// Game Attributes
	public float score;

	// Display attributes
	public Text scoreText;

	// Object/Script references
	private GameObject player;
	private BoulderMovementScript pScript;
	private GameObject gameOverTextObj;
	private Text gameOverText;

	// Use this for initialization
	void Start () 
	{
		// Initialise attributes
		score = 0;
		SetScoreText();

		// Initialise object/script references
		player = GameObject.Find ("Boulder");
		pScript = player.GetComponent<BoulderMovementScript>();
		gameOverTextObj = GameObject.Find ("GameOver");
		gameOverText = gameOverTextObj.GetComponent<Text> ();

		gameOverText.enabled = false;
	}
	
	// Update is called once per frame
	void Update () 
	{
	// Score manager
		// Add 100 when enemy is killed
		if (pScript.enemyKilled == true)
		{
			score += 100;
			pScript.enemyKilled = false;
		}
		// Decide how much to add to score per frame
		// based on current player speed
		float speedVal = pScript.rigBody.velocity.magnitude-10;
		speedVal = Mathf.Round (speedVal); // Rounds value to nearest integer
		float amountToAdd = 10 + speedVal;

		score += amountToAdd;

		SetScoreText();

		if (pScript.dead)
		{
			StartCoroutine(LevelReset());
		}
	}

	void SetScoreText ()
	{

		scoreText.text = score.ToString ();
	}

	// Co-routine to countdown to game restart
	IEnumerator LevelReset()
	{
		gameOverText.enabled = true;
		yield return new WaitForSeconds (3.0f);
		Application.LoadLevel (Application.loadedLevelName); //Reset current scene
	}

}
