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

public class TimerHandler
{
    public bool endDay;

    public float totalTimerTime;
    public float timerTime;
    private float initialTime;
    private int millisec;

    public Text timerText;
    public Text millisecText;

    public TimerHandler(float totalTimerTime, Text timerText)
    {
        this.totalTimerTime = totalTimerTime;
        initialTime = Time.time;
        this.timerText = timerText;
        this.millisecText = timerText.transform.GetChild(0).gameObject.GetComponent<Text>();

        timerText.gameObject.SetActive(true);

        endDay = false;
    }

    public int GetTimerMinutes()
    {
        if ((int)(timerTime / 60) <= 0)
            return 0;

        return (int)(timerTime / 60);
    }

    public int GetTimerSeconds()
    {
        if ((int)(timerTime % 60) <= 0)
            return 0;

        return (int)(timerTime % 60);
    }

    public int GetTimerMilliseconds()
    {
        if ((int)((Math.Round(timerTime, 2) - (int)timerTime) * 100) <= 0)
            return 0;

        return (int)((Math.Round(timerTime, 2) - (int)timerTime) * 100);
    }


    public void Update()
    {
        timerTime = totalTimerTime - (Time.time - initialTime);

        if (GetTimerMilliseconds() == 0 && GetTimerMinutes() == 0 && GetTimerSeconds() == 0)
            endDay = true;

        if (GetTimerSeconds() < 10)
            timerText.text = GetTimerMinutes() + ":0" + GetTimerSeconds();
        else
            timerText.text = GetTimerMinutes() + ":" + GetTimerSeconds();
        
        millisec = GetTimerMilliseconds();

        if (millisec < 10)
            millisecText.text = "0" + millisec;
        else
            millisecText.text = "" + millisec;

        timerText.fontSize = (int)(40 + (((timerTime * 100) % 100) / 100) * 5);
    }
}
