namespace DragonISO.Engine.Pipeline;

public sealed class PeriodicTimerFrameClock : IFrameClock, IDisposable
{
    private readonly PeriodicTimer _timer;

    public PeriodicTimerFrameClock(double framesPerSecond)
    {
        if (framesPerSecond <= 0)
            throw new ArgumentOutOfRangeException(nameof(framesPerSecond));
        var periodMs = 1000.0 / framesPerSecond;
        _timer = new PeriodicTimer(TimeSpan.FromMilliseconds(periodMs));
    }

    public long NowTicks => DateTime.UtcNow.Ticks;

    public ValueTask<bool> WaitForNextTickAsync(CancellationToken cancellationToken) =>
        _timer.WaitForNextTickAsync(cancellationToken);

    public void Dispose() => _timer.Dispose();
}
