namespace Syntriax.Engine.Systems.Time;

public class TickerBehaviour : StopwatchBehaviour, ITicker
{
    public event ITicker.TickerTickEventHandler? OnTick = null;

    public double TickPeriod { get; set; } = 1f;
    public int TickCounter { get; private set; } = 0;

    private double nextTick = 0f;

    public override void StopwatchStart()
    {
        TickCounter = 0;
        base.StopwatchStart();
        nextTick = Time + TickPeriod;
    }

    protected override void OnUpdate()
    {
        base.OnUpdate();

        if (Time < nextTick)
            return;

        nextTick += TickPeriod;
        TickCounter++;
        OnTick?.Invoke(this);
    }

    protected override void OnFinalize()
    {
        base.OnFinalize();

        TickCounter = 0;
        nextTick = 0f;
    }
}