#include "RuntimeLayerModel.h"

#include <utility>

namespace RenderCadenceCompositor
{
RuntimeLayerModelSnapshot RuntimeLayerModel::Snapshot() const
{
	RuntimeLayerModelSnapshot snapshot;
	snapshot.compileSucceeded = true;

	for (const Layer& layer : mLayers)
	{
		snapshot.displayLayers.push_back(ToReadModel(layer));
		if (!layer.message.empty() && snapshot.compileMessage.empty())
			snapshot.compileMessage = layer.message;
		if (layer.buildState == RuntimeLayerBuildState::Failed)
			snapshot.compileSucceeded = false;
		if (layer.renderReady)
		{
			RuntimeRenderLayerModel renderLayer;
			renderLayer.id = layer.id;
			renderLayer.bypass = layer.bypass;
			renderLayer.artifact = layer.artifact;
			renderLayer.shaderId = renderLayer.artifact.shaderId.empty() ? layer.shaderId : renderLayer.artifact.shaderId;
			if (layer.buildState == RuntimeLayerBuildState::Ready)
				renderLayer.artifact.parameterValues = layer.parameterValues;
			renderLayer.artifact.fontAtlases.clear();
			snapshot.renderLayers.push_back(std::move(renderLayer));
		}
	}

	if (snapshot.compileMessage.empty())
		snapshot.compileMessage = mLayers.empty() ? "Runtime shader build disabled." : "Runtime shader build has not completed yet.";
	return snapshot;
}

RuntimeLayerReadModel RuntimeLayerModel::ToReadModel(const Layer& layer)
{
	RuntimeLayerReadModel readModel;
	readModel.id = layer.id;
	readModel.shaderId = layer.shaderId;
	readModel.shaderName = layer.shaderName;
	readModel.bypass = layer.bypass;
	readModel.buildState = layer.buildState;
	readModel.message = layer.message;
	readModel.renderReady = layer.renderReady;
	readModel.parameterDefinitions = layer.parameterDefinitions;
	readModel.parameterValues = layer.parameterValues;
	return readModel;
}
}
