#pragma once

#include "CadenceTelemetry.h"
#include "../json/JsonWriter.h"

#include <cstdint>
#include <string>

namespace RenderCadenceCompositor
{
inline void WriteCadenceTelemetryJson(JsonWriter& writer, const CadenceTelemetrySnapshot& snapshot)
{
	writer.BeginObject();
	writer.KeyDouble("sampleSeconds", snapshot.sampleSeconds);
	writer.KeyDouble("renderFps", snapshot.renderFps);
	writer.KeyDouble("scheduleFps", snapshot.scheduleFps);
	writer.KeyUInt("free", static_cast<uint64_t>(snapshot.freeFrames));
	writer.KeyUInt("completed", static_cast<uint64_t>(snapshot.completedFrames));
	writer.KeyUInt("scheduled", static_cast<uint64_t>(snapshot.scheduledFrames));
	writer.KeyUInt("renderedTotal", snapshot.renderedTotal);
	writer.KeyUInt("scheduledTotal", snapshot.scheduledTotal);
	writer.KeyUInt("completedPollMisses", snapshot.completedPollMisses);
	writer.KeyUInt("scheduleFailures", snapshot.scheduleFailures);
	writer.KeyUInt("completions", snapshot.completions);
	writer.KeyUInt("late", snapshot.displayedLate);
	writer.KeyUInt("dropped", snapshot.dropped);
	writer.KeyUInt("shaderCommitted", snapshot.shaderBuildsCommitted);
	writer.KeyUInt("shaderFailures", snapshot.shaderBuildFailures);
	writer.KeyBool("deckLinkBufferedAvailable", snapshot.deckLinkBufferedAvailable);
	writer.Key("deckLinkBuffered");
	if (snapshot.deckLinkBufferedAvailable)
		writer.UInt(snapshot.deckLinkBuffered);
	else
		writer.Null();
	writer.KeyDouble("scheduleCallMs", snapshot.deckLinkScheduleCallMilliseconds);
	writer.EndObject();
}

inline std::string CadenceTelemetryToJson(const CadenceTelemetrySnapshot& snapshot)
{
	JsonWriter writer;
	WriteCadenceTelemetryJson(writer, snapshot);
	return writer.StringValue();
}
}
