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

public class TimerGame : Singleton<TimerGame> {
    
    public Color colorBlink;
    public Action TimeClosedEvent;

    public Text mNumberTime;

    protected override void Awake()
    {
        base.Awake();
    }

    public void StartTime(int time)
    {
        this.killCoroutines();
        if (time < 10)
        {
            mNumberTime.text = "0" + time;
        }
        else
        {
            mNumberTime.text = time.ToString();
        }
        Timing.RunCoroutine(CountTime(time), this.guid);
        
    }

    IEnumerator<float> CountTime(int time)
    {
        while (time > 0) {
            yield return Timing.WaitForSeconds(1.0f);
            SoundEffectsPlayer.Instance.Play("sfx-tile-azul-2");
            time--;
            if (time < 10)
            {
                Timing.RunCoroutine(Blink());
                mNumberTime.text = "0" + time;
            }
            else
            {
                mNumberTime.text = time.ToString();
            }
        }

        SoundEffectsPlayer.Instance.Play("sfx-tile-efeito-roxo"); //time out
        StartEvent();
    }

    IEnumerator<float> Blink()
    {
        mNumberTime.color = colorBlink;
        yield return Timing.WaitForSeconds(0.2f);
        mNumberTime.color = new Color(0, 0, 0, 1);
    }

    public void Interrupt()
    {
        this.killCoroutines();

        this.StartEvent();
    }

    private void StartEvent()
    {
        if(TimeClosedEvent != null)
        {
            TimeClosedEvent();
        }
    }
}
