﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.UI;
using System;

public class UICanvasControl : MonoBehaviour {

    private DateTime startTime;
    private TimeSpan elapsedTime;

    private Text timer;
    private Text score;

    // Use this for initialization
    void Start () {
        // Find the child timer and score text game objects nested inside this game object
        GameObject timerText = gameObject.transform.Find("Timer Text").gameObject;

        // The score text is a child of the score image (other words grand child of this game object)
        GameObject scoreText = gameObject.transform.FindChild("Score Image")
            .transform.Find("Score Text").gameObject;

        // Get their text conmponent
        timer = timerText.GetComponent<Text>();
        score = scoreText.GetComponent<Text>();

        // Initialise the start time and elapsed time
        startTime = DateTime.Now;
        elapsedTime = TimeSpan.Zero;
    }
	
	// Update is called once per frame
	void Update () {
		if (!Managers.IsGameOver)
        {
            // Update elapsed time
            elapsedTime = DateTime.Now - startTime;
            
            // Display the current time elapsed
            timer.text = string.Format("{0:00}:{1:00}:{2:00}", elapsedTime.Hours, elapsedTime.Minutes, elapsedTime.Seconds);

            // Display the current score
            score.text = Managers.Player.Score.ToString();
        }
	}
}
