#pragma once

#include "RuntimeStoreReadModels.h"
#include "ShaderTypes.h"

#include <atomic>
#include <cstdint>
#include <string>
#include <vector>

class RuntimeStore;

struct RuntimeSnapshotVersions
{
	uint64_t renderStateVersion = 0;
	uint64_t parameterStateVersion = 0;
};

class RenderSnapshotBuilder
{
public:
	explicit RenderSnapshotBuilder(RuntimeStore& runtimeStore);

	bool BuildLayerPassFragmentShaderSources(const std::string& layerId, std::vector<ShaderPassBuildSource>& passSources, std::string& error) const;
	unsigned GetMaxTemporalHistoryFrames() const;
	RuntimeSnapshotVersions GetVersions() const;
	void AdvanceFrame();
	void BuildLayerRenderStates(unsigned outputWidth, unsigned outputHeight, std::vector<RuntimeRenderState>& states) const;
	bool TryBuildLayerRenderStates(unsigned outputWidth, unsigned outputHeight, std::vector<RuntimeRenderState>& states) const;
	bool TryRefreshLayerParameters(std::vector<RuntimeRenderState>& states) const;
	void RefreshDynamicRenderStateFields(std::vector<RuntimeRenderState>& states) const;
	void MarkRenderStateDirty();
	void MarkParameterStateDirty();

private:
	void BuildLayerRenderStates(unsigned outputWidth, unsigned outputHeight, const RenderSnapshotReadModel& readModel, std::vector<RuntimeRenderState>& states) const;
	void RefreshLayerParameters(const std::vector<LayerStackStore::LayerPersistentState>& layers, std::vector<RuntimeRenderState>& states) const;
	void RefreshDynamicRenderStateFields(const RenderTimingSnapshot& timing, std::vector<RuntimeRenderState>& states) const;

	RuntimeStore& mRuntimeStore;
	std::atomic<uint64_t> mFrameCounter{ 0 };
	std::atomic<uint64_t> mRenderStateVersion{ 0 };
	std::atomic<uint64_t> mParameterStateVersion{ 0 };
};
