#pragma once

#include "VideoIOTypes.h"
#include "VideoPlayoutPolicy.h"

#include <cstdint>

class VideoPlayoutScheduler
{
public:
	void Configure(int64_t frameDuration, int64_t timeScale);
	void Configure(int64_t frameDuration, int64_t timeScale, const VideoPlayoutPolicy& policy);
	void Reset();
	VideoIOScheduleTime NextScheduleTime();
	void AlignNextScheduleTimeToPlayback(int64_t streamTime, uint64_t leadFrames);
	VideoPlayoutRecoveryDecision AccountForCompletionResult(VideoIOCompletionResult result, uint64_t readyQueueDepth = 0);
	double FrameBudgetMilliseconds() const;
	uint64_t ScheduledFrameIndex() const { return mScheduledFrameIndex; }
	uint64_t CompletedFrameIndex() const { return mCompletedFrameIndex; }
	int64_t FrameDuration() const { return mFrameDuration; }
	uint64_t LateStreak() const { return mLateStreak; }
	uint64_t DropStreak() const { return mDropStreak; }
	int64_t TimeScale() const { return mTimeScale; }
	const VideoPlayoutPolicy& Policy() const { return mPolicy; }

private:
	uint64_t MeasureLag(VideoIOCompletionResult result, uint64_t readyQueueDepth) const;

	int64_t mFrameDuration = 0;
	int64_t mTimeScale = 0;
	uint64_t mScheduledFrameIndex = 0;
	uint64_t mCompletedFrameIndex = 0;
	uint64_t mLateStreak = 0;
	uint64_t mDropStreak = 0;
	VideoPlayoutPolicy mPolicy;
};
