#include "RuntimeShaderParams.h"

#include <cstring>
#include <iostream>
#include <string>
#include <vector>

namespace
{
void Expect(bool condition, const char* message)
{
	if (!condition)
	{
		std::cerr << "FAILED: " << message << "\n";
		std::exit(1);
	}
}

float ReadFloat(const std::vector<unsigned char>& buffer, std::size_t offset)
{
	float value = 0.0f;
	std::memcpy(&value, buffer.data() + offset, sizeof(value));
	return value;
}

int ReadInt(const std::vector<unsigned char>& buffer, std::size_t offset)
{
	int value = 0;
	std::memcpy(&value, buffer.data() + offset, sizeof(value));
	return value;
}

ShaderParameterDefinition FloatParam(const std::string& id, double defaultValue)
{
	ShaderParameterDefinition definition;
	definition.id = id;
	definition.label = id;
	definition.type = ShaderParameterType::Float;
	definition.defaultNumbers = { defaultValue };
	return definition;
}

ShaderParameterDefinition Vec2Param()
{
	ShaderParameterDefinition definition;
	definition.id = "offset";
	definition.label = "Offset";
	definition.type = ShaderParameterType::Vec2;
	definition.defaultNumbers = { 2.0, 3.0 };
	return definition;
}

ShaderParameterDefinition ColorParam()
{
	ShaderParameterDefinition definition;
	definition.id = "tint";
	definition.label = "Tint";
	definition.type = ShaderParameterType::Color;
	definition.defaultNumbers = { 0.25, 0.5, 0.75, 1.0 };
	return definition;
}

ShaderParameterDefinition BoolParam()
{
	ShaderParameterDefinition definition;
	definition.id = "enabled";
	definition.label = "Enabled";
	definition.type = ShaderParameterType::Boolean;
	definition.defaultBoolean = true;
	return definition;
}

ShaderParameterDefinition EnumParam()
{
	ShaderParameterDefinition definition;
	definition.id = "mode";
	definition.label = "Mode";
	definition.type = ShaderParameterType::Enum;
	definition.defaultEnumValue = "hard";
	definition.enumOptions = { { "soft", "Soft" }, { "hard", "Hard" } };
	return definition;
}

ShaderParameterDefinition TriggerParam()
{
	ShaderParameterDefinition definition;
	definition.id = "drop";
	definition.label = "Drop";
	definition.type = ShaderParameterType::Trigger;
	return definition;
}
}

int main()
{
	RuntimeShaderArtifact artifact;
	artifact.parameterDefinitions.push_back(FloatParam("gain", 0.5));
	artifact.parameterDefinitions.push_back(Vec2Param());
	artifact.parameterDefinitions.push_back(ColorParam());
	artifact.parameterDefinitions.push_back(BoolParam());
	artifact.parameterDefinitions.push_back(EnumParam());
	artifact.parameterDefinitions.push_back(TriggerParam());

	ShaderParameterValue triggerValue;
	triggerValue.numberValues = { 3.0, 1.25 };
	artifact.parameterValues["drop"] = triggerValue;

	const std::vector<unsigned char> buffer = BuildRuntimeShaderGlobalParamsStd140(artifact, 120, 1920, 1080);

	Expect(buffer.size() == 112, "runtime shader params block keeps expected std140 size");
	Expect(ReadFloat(buffer, 0) == 2.0f, "time is derived from frame index at 60 fps");
	Expect(ReadFloat(buffer, 8) == 1920.0f, "input width is packed after std140 vec2 alignment");
	Expect(ReadFloat(buffer, 12) == 1080.0f, "input height is packed after std140 vec2 alignment");
	Expect(ReadFloat(buffer, 60) == 0.5f, "float default parameter is packed");
	Expect(ReadFloat(buffer, 64) == 2.0f, "vec2 default x is packed");
	Expect(ReadFloat(buffer, 68) == 3.0f, "vec2 default y is packed");
	Expect(ReadFloat(buffer, 80) == 0.25f, "color default red is packed on vec4 alignment");
	Expect(ReadFloat(buffer, 92) == 1.0f, "color default alpha is packed");
	Expect(ReadInt(buffer, 96) == 1, "bool default is packed as int");
	Expect(ReadInt(buffer, 100) == 1, "enum default is packed as selected option index");
	Expect(ReadInt(buffer, 104) == 3, "trigger count is packed as int");
	Expect(ReadFloat(buffer, 108) == 1.25f, "trigger time is packed after trigger count");

	std::cout << "RenderCadenceCompositorRuntimeShaderParams tests passed.\n";
	return 0;
}
