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

public class GameplayHandler : MonoBehaviour {


	float startTime;
	[SerializeField] GameObject canvas;
	[SerializeField] GameObject loseT;
	[SerializeField] GameObject winT;
	[SerializeField] GameObject loseS;
	[SerializeField] GameObject winS;
	void Start () 
	{
		startTime = Time.realtimeSinceStartup;
	}

	void Die ()
	{
		GetComponent<Rigidbody> ().isKinematic = true;
		float score = Time.realtimeSinceStartup - startTime;
		if (GetComponent<Scoring> ().SetScore (score)) 
		{
			canvas.SetActive (true);
			winS.SetActive (true);
			winT.SetActive (true);
			winS.GetComponent<Text> ().text = "New Highscore!!! : " + Formatioso (score);
		} 
		else 
		{
			canvas.SetActive (true);
			loseS.SetActive (true);
			loseT.SetActive (true);
			loseS.GetComponent<Text> ().text = "Highscore: " + Formatioso (GetComponent<Scoring> ().highScore);
		}

	}

	string Formatioso(float time)
	{
		float minutes = time / 60; //Divide the guiTime by sixty to get minutes
		float seconds = time % 60; //use the euclidean division for the seconds
		float fraction = (time * 100) % 100;

		//update the label value
		return string.Format("{0:00} : {1:00} : {2:000}", minutes, seconds, fraction);
	}
}
