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

public class Timer : MonoBehaviour
{
    public float timeLeft = 70.0f;
    public bool isStarted = false;
    public bool isFinished = false;
    public UnityEngine.UI.Text displayText;

    private float minutes;
    private float seconds;
    private float prevSec;

    public AudioSource tick;
    // Use this for initialization
    void Start ()
    {
		displayText.text = "";
		prevSec=0;
    }

    public void StartTimer(float _time)
    {
        timeLeft = _time;
        isStarted = true;
    }

    public void ResetTimer()
    {
        isStarted = false;
    }
	
	// Update is called once per frame
	void Update ()
    {
        if (!isStarted)
            return;

		if(!isFinished)
        {
            timeLeft -= Time.deltaTime;
        }

        if (timeLeft > 0)
        {
            minutes = Mathf.Floor(timeLeft / 60);
            seconds = timeLeft % 60;
            if(seconds!=prevSec)
            {
				prevSec=seconds;
				Debug.Log(seconds);
				tick.Play();
            }
            if (seconds > 59) seconds = 59;
            if (minutes < 0)
            {
                isFinished = true;
                minutes = 0;
                seconds = 0;
            }
            displayText.text = Mathf.FloorToInt(minutes).ToString() + " : " + Mathf.FloorToInt(seconds).ToString();
        
        }
        else
        {
            isFinished = true;
            minutes = 0;
            seconds = 0;
        }
    }
}
