#pragma once

#include "RuntimeJson.h"
#include "RuntimeShaderArtifact.h"
#include "SupportedShaderCatalog.h"

#include <chrono>
#include <cstdint>
#include <map>
#include <utility>
#include <string>
#include <vector>

namespace RenderCadenceCompositor
{
enum class RuntimeLayerBuildState
{
	Pending,
	Ready,
	Failed
};

struct RuntimeLayerReadModel
{
	std::string id;
	std::string shaderId;
	std::string shaderName;
	bool bypass = false;
	RuntimeLayerBuildState buildState = RuntimeLayerBuildState::Pending;
	std::string message;
	bool renderReady = false;
	std::vector<ShaderParameterDefinition> parameterDefinitions;
	std::map<std::string, ShaderParameterValue> parameterValues;
};

struct RuntimeRenderLayerModel
{
	std::string id;
	std::string shaderId;
	bool bypass = false;
	RuntimeShaderArtifact artifact;
};

struct RuntimeLayerModelSnapshot
{
	bool compileSucceeded = true;
	std::string compileMessage;
	std::vector<RuntimeLayerReadModel> displayLayers;
	std::vector<RuntimeRenderLayerModel> renderLayers;
};

class RuntimeLayerModel
{
public:
	bool InitializeSingleLayer(const SupportedShaderCatalog& shaderCatalog, const std::string& shaderId, std::string& error);
	bool InitializeFromRuntimeState(const SupportedShaderCatalog& shaderCatalog, const JsonValue& runtimeState, std::string& error);
	void Clear();

	bool AddLayer(const SupportedShaderCatalog& shaderCatalog, const std::string& shaderId, std::string& layerId, std::string& error);
	bool RemoveLayer(const std::string& layerId, std::string& error);
	bool ReorderLayer(const std::string& layerId, int targetIndex, std::string& error);
	bool SetLayerBypass(const std::string& layerId, bool bypass, std::string& error);
	bool SetLayerShader(const SupportedShaderCatalog& shaderCatalog, const std::string& layerId, const std::string& shaderId, std::string& error);
	bool UpdateParameter(const std::string& layerId, const std::string& parameterId, const JsonValue& value, std::string& error);
	bool ResetParameters(const std::string& layerId, std::string& error);
	bool ReloadFromCatalog(const SupportedShaderCatalog& shaderCatalog, std::vector<std::pair<std::string, std::string>>& buildsToStart, std::string& error);
	bool MarkBuildStarted(const std::string& layerId, const std::string& message, std::string& error, bool preserveExistingRenderArtifact = false);
	bool MarkBuildReady(const RuntimeShaderArtifact& artifact, std::string& error);
	bool MarkBuildFailedForShader(const std::string& shaderId, const std::string& message);
	bool MarkBuildFailed(const std::string& layerId, const std::string& message, std::string& error);
	bool MarkRenderCommitFailed(const std::string& layerId, const std::string& message, std::string& error);

	RuntimeLayerModelSnapshot Snapshot() const;
	std::string FirstLayerId() const;
	std::vector<std::pair<std::string, std::string>> PendingLayerBuilds() const;

private:
	struct Layer
	{
		std::string id;
		std::string shaderId;
		std::string packageFingerprint;
		std::string shaderName;
		bool bypass = false;
		RuntimeLayerBuildState buildState = RuntimeLayerBuildState::Pending;
		std::string message;
		bool renderReady = false;
		bool preserveRenderDuringBuild = false;
		std::vector<ShaderParameterDefinition> parameterDefinitions;
		std::map<std::string, ShaderParameterValue> parameterValues;
		RuntimeShaderArtifact artifact;
	};

	Layer* FindLayer(const std::string& layerId);
	const Layer* FindLayer(const std::string& layerId) const;
	Layer* FindFirstLayerForShader(const std::string& shaderId);
	static void InitializeDefaultParameterValues(Layer& layer, const ShaderPackage& shaderPackage);
	static const ShaderParameterDefinition* FindParameterDefinition(const Layer& layer, const std::string& parameterId);
	std::string AllocateLayerId();
	static RuntimeLayerReadModel ToReadModel(const Layer& layer);
	double RuntimeElapsedSeconds() const;

	std::vector<Layer> mLayers;
	uint64_t mNextLayerNumber = 1;
	std::chrono::steady_clock::time_point mStartTime = std::chrono::steady_clock::now();
};
}
