#pragma once

#include <chrono>
#include <cstdint>

class RenderCadenceClock
{
public:
	using Clock = std::chrono::steady_clock;
	using Duration = Clock::duration;
	using TimePoint = Clock::time_point;

	struct Tick
	{
		bool due = false;
		uint64_t skippedFrames = 0;
		Duration sleepFor = Duration::zero();
	};

	explicit RenderCadenceClock(double frameDurationMilliseconds = 1000.0 / 60.0);

	void Reset(TimePoint now = Clock::now());
	Tick Poll(TimePoint now = Clock::now());
	void MarkRendered(TimePoint now = Clock::now());

	Duration FrameDuration() const { return mFrameDuration; }
	TimePoint NextRenderTime() const { return mNextRenderTime; }
	uint64_t OverrunCount() const { return mOverrunCount; }
	uint64_t SkippedFrameCount() const { return mSkippedFrameCount; }

private:
	Duration mFrameDuration;
	TimePoint mNextRenderTime = Clock::now();
	uint64_t mPendingFrameAdvance = 1;
	uint64_t mOverrunCount = 0;
	uint64_t mSkippedFrameCount = 0;
};
