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

public class GameOver : MonoBehaviour
{
    Text label;
    CubeSpawn global;
    bool bShown;

    // Start is called before the first frame update
    void Start()
    {
        label = transform.Find("YouSurvived").GetComponent<Text>();
        global = (CubeSpawn)FindObjectOfType(typeof(CubeSpawn));

        foreach(Transform child in transform)
        {
            child.GetComponent<Text>().enabled = false;
        }

        bShown = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (global.IsGameOver() && false == bShown)
        {
            foreach (Transform child in transform)
            {
                child.GetComponent<Text>().enabled = true;
            }

            TimeSpan span = TimeSpan.FromSeconds(global.GameTime());
            string survivetime = string.Format("{0:0}:{1:00}.{2:0}", span.Minutes, span.Seconds, span.Milliseconds/100);

            int cubeCount = GameObject.FindGameObjectsWithTag("Cube").Length;

            label.text = string.Format("Time Survived: {0}\nMost Cubes: {1}", survivetime, cubeCount);

            bShown = true;
        }
    }
}
