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

public class VictoryScreen : MonoBehaviour
{
    private Goal goal;
    public Timer timerScript;

    public GameObject Timer;

    public float WinTime;
    public float currentDisplayTime;

    public float IterateAmount;

    public Text timeText;

	// Use this for initialization
	void Awake ()
    {
        goal = GameObject.Find("Goal").GetComponent<Goal>();
        currentDisplayTime = 0;
        WinTime = timerScript.winTime;
        StartCoroutine(DisplayTime());
	}

    public void LoadNextScene()
    {
        SceneManager.LoadScene(goal.linkedScene);
    }

    public void RestartScene()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    public void BackToMenu()
    {
        SceneManager.LoadScene(0);
    }

    IEnumerator DisplayTime()
    {
        yield return new WaitForSeconds(1f);

        Timer.SetActive(true);

        while (currentDisplayTime < WinTime)
        {
            currentDisplayTime += IterateAmount;
            UpdateDisplayTime(currentDisplayTime);
            yield return new WaitForEndOfFrame();
        }

        UpdateDisplayTime(WinTime);
    }

    void UpdateDisplayTime(float time)
    {
        float minutes = time / 60;
        float seconds = time % 60;
        float fraction = (time * 100) % 100;

        timeText.text = string.Format("{0:00} : {1:00} : {2:000}", minutes, seconds, fraction);
    }
}
