
using UnityEngine;
using RetroDev.GameTimer;

public class ExampleTimer : MonoBehaviour
{
    // Our timer. You can instantiate multiple at one given time.
    public GameTimer timer;


    // Instantiate a new timer on awake
    private void Awake()
    {
        // The timer constructor takes a duration (float) and optional callback functions as parameters
        timer = new GameTimer(10f, onTimerCompleteCallback: OnCountdownDone, onStartCallback:OnTimerStart, onStopCallback: OnTimerStopped );
    }

    // Example callback functions for when the timer is started
    public void OnTimerStart()
    {
        Debug.Log("Timer has started!");
    }

    // Example callback functions for when the timer is stopped
    public void OnTimerStopped()
    {
        Debug.Log("Timer has stopped!");
    }

    // Example callback to execute code when the timer runs out
    public void OnCountdownDone()
    {
        Debug.Log("Time has run out!");
    }

    // Always use the timer in the update method
    public void Update()
    {
        // It's crucial to call the update method in the timer class in order for the timer to work reliably.
        timer.Update();

        // You can get the current elapsed time using the GetElapsedTime() function.
        Debug.Log(timer.GetElapsedTime());

        // Example for starting and stopping a timer
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (!timer.IsRunning())
            {
                timer.Start();
            }
            else
            {
                timer.Stop();
            }
        }

        // Example for pausing a timer
        if (Input.GetKeyDown(KeyCode.P))
        {
            if (timer.IsRunning() && !timer.IsPaused())
            {
                timer.Pause();
            }
            else
            {
                timer.Resume();
            }
        }

        // Example for resetting a timer
        if (Input.GetKeyDown (KeyCode.R))
        {
            timer.Reset();
        }
    }
}
