/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#include <catch2/catch_all.hpp>

#include "Extensions/RymlConverters.hpp"

using namespace OpenVulkano;
using namespace OpenVulkano::Math;
using namespace c4;

TEST_CASE("Vector2 to_chars and from_chars", "[RymlConverters]")
{
	Vector2<float> vec2 { 1.1f, 2.2f };
	char buffer[20] = {};

	size_t written = to_chars(ryml::substr(buffer), vec2);
	REQUIRE(written != ryml::yml::npos);
	REQUIRE(std::string(buffer) == "(1.1,2.2)");

	Vector2<float> parsed_vec2;
	REQUIRE(from_chars("(1.1,2.2)", &parsed_vec2));
	REQUIRE(parsed_vec2.x == 1.1f);
	REQUIRE(parsed_vec2.y == 2.2f);
}

TEST_CASE("Vector3 to_chars and from_chars", "[RymlConverters]")
{
	Vector3<float> vec3 { 1.1f, 2.2f, 3.3f };
	char buffer[30] = {};

	size_t written = to_chars(ryml::substr(buffer), vec3);
	REQUIRE(written != ryml::yml::npos);
	REQUIRE(std::string(buffer) == "(1.1,2.2,3.3)");

	Vector3<float> parsed_vec3;
	REQUIRE(from_chars("(1.1,2.2,3.3)", &parsed_vec3));
	REQUIRE(parsed_vec3.x == 1.1f);
	REQUIRE(parsed_vec3.y == 2.2f);
	REQUIRE(parsed_vec3.z == 3.3f);
}

TEST_CASE("Vector4 to_chars and from_chars", "[RymlConverters]")
{
	Vector4<float> vec4 { 1.1f, 2.2f, 3.3f, 4.4f };
	char buffer[30] = {};

	size_t written = to_chars(ryml::substr(buffer), vec4);
	REQUIRE(written != ryml::yml::npos);
	REQUIRE(std::string(buffer) == "(1.1,2.2,3.3,4.4)");

	Vector4<float> parsed_vec4;
	REQUIRE(from_chars("(1.1,2.2,3.3,4.4)", &parsed_vec4));
	REQUIRE(parsed_vec4.x == 1.1f);
	REQUIRE(parsed_vec4.y == 2.2f);
	REQUIRE(parsed_vec4.z == 3.3f);
	REQUIRE(parsed_vec4.w == 4.4f);
}

TEST_CASE("Matrix3 to_chars and from_chars", "[RymlConverters]")
{
	Matrix3<float> mat3 { { 1.1f, 2.2f, 3.3f }, { 4.4f, 5.5f, 6.6f }, { 7.7f, 8.8f, 9.9f } };
	char buffer[100] = {};

	size_t written = to_chars(ryml::substr(buffer), mat3);
	REQUIRE(written != ryml::yml::npos);
	REQUIRE(std::string(buffer) == "(1.1,4.4,7.7),(2.2,5.5,8.8),(3.3,6.6,9.9)");

	Matrix3<float> parsed_mat3;
	REQUIRE(from_chars("(1.1,4.4,7.7),(2.2,5.5,8.8),(3.3,6.6,9.9)", &parsed_mat3));
	REQUIRE(parsed_mat3[0][0] == 1.1f);
	REQUIRE(parsed_mat3[0][1] == 2.2f);
	REQUIRE(parsed_mat3[0][2] == 3.3f);
	REQUIRE(parsed_mat3[1][0] == 4.4f);
	REQUIRE(parsed_mat3[1][1] == 5.5f);
	REQUIRE(parsed_mat3[1][2] == 6.6f);
	REQUIRE(parsed_mat3[2][0] == 7.7f);
	REQUIRE(parsed_mat3[2][1] == 8.8f);
	REQUIRE(parsed_mat3[2][2] == 9.9f);
}

TEST_CASE("Matrix4 to_chars and from_chars", "[RymlConverters]")
{
	Matrix4<float> mat4 { { 1.1f, 2.2f, 3.3f, 4.4f },
		                  { 5.5f, 6.6f, 7.7f, 8.8f },
		                  { 9.9f, 10.10f, 11.11f, 12.12f },
		                  { 13.13f, 14.14f, 15.15f, 16.16f } };
	char buffer[200] = {};

	size_t written = to_chars(ryml::substr(buffer), mat4);
	REQUIRE(written != ryml::yml::npos);
	REQUIRE(std::string(buffer) == "(1.1,5.5,9.9,13.13),(2.2,6.6,10.1,14.14),(3.3,7.7,11.11,15.15),(4.4,8.8,12.12,16.16)");

	Matrix4<float> parsed_mat4;
	REQUIRE(from_chars("(1.1,5.5,9.9,13.13),(2.2,6.6,10.1,14.14),(3.3,7.7,11.11,15.15),(4.4,8.8,12.12,16.16)", &parsed_mat4));

	REQUIRE(parsed_mat4[0][0] == 1.1f);
	REQUIRE(parsed_mat4[0][1] == 2.2f);
	REQUIRE(parsed_mat4[0][2] == 3.3f);
	REQUIRE(parsed_mat4[0][3] == 4.4f);

	REQUIRE(parsed_mat4[1][0] == 5.5f);
	REQUIRE(parsed_mat4[1][1] == 6.6f);
	REQUIRE(parsed_mat4[1][2] == 7.7f);
	REQUIRE(parsed_mat4[1][3] == 8.8f);

	REQUIRE(parsed_mat4[2][0] == 9.9f);
	REQUIRE(parsed_mat4[2][1] == 10.10f);
	REQUIRE(parsed_mat4[2][2] == 11.11f);
	REQUIRE(parsed_mat4[2][3] == 12.12f);

	REQUIRE(parsed_mat4[3][0] == 13.13f);
	REQUIRE(parsed_mat4[3][1] == 14.14f);
	REQUIRE(parsed_mat4[3][2] == 15.15f);
	REQUIRE(parsed_mat4[3][3] == 16.16f);
}

TEST_CASE("AABB to_chars and from_chars", "[RymlConverters]")
{
	AABB bbox { Vector3<float> { 1.0f, 2.0f, 3.0f }, Vector3<float> { 4.0f, 5.0f, 6.0f } };
	char buffer[50] = {};

	size_t written = to_chars(ryml::substr(buffer), bbox);
	REQUIRE(written != ryml::yml::npos);
	REQUIRE(std::string(buffer) == "(1,2,3),(4,5,6)");

	AABB parsed_bbox;
	REQUIRE(from_chars("(1,2,3),(4,5,6)", &parsed_bbox));
	REQUIRE(parsed_bbox.min.x == 1.0f);
	REQUIRE(parsed_bbox.min.y == 2.0f);
	REQUIRE(parsed_bbox.min.z == 3.0f);
	REQUIRE(parsed_bbox.max.x == 4.0f);
	REQUIRE(parsed_bbox.max.y == 5.0f);
	REQUIRE(parsed_bbox.max.z == 6.0f);
}

TEST_CASE("Pose to_chars and from_chars", "[RymlConverters]")
{
	Pose<float> pose;
	pose.SetPosition(Vector3<float> { 1.1f, 2.2f, 3.3f });
	pose.SetOrientation(QuaternionF(7.7f, 4.4f, 5.5f, 6.6f));
	char buffer[70] = {};

	size_t written = to_chars(ryml::substr(buffer), pose);
	REQUIRE(written != ryml::yml::npos);
	REQUIRE(std::string(buffer) == "(1.1,2.2,3.3),(4.4,5.5,6.6,7.7)");

	Pose<float> parsed_pose;
	REQUIRE(from_chars("(1.1,2.2,3.3),(4.4,5.5,6.6,7.7)", &parsed_pose));
	REQUIRE(parsed_pose.GetPosition().x == 1.1f);
	REQUIRE(parsed_pose.GetPosition().y == 2.2f);
	REQUIRE(parsed_pose.GetPosition().z == 3.3f);
	REQUIRE(parsed_pose.GetOrientation().x == 4.4f);
	REQUIRE(parsed_pose.GetOrientation().y == 5.5f);
	REQUIRE(parsed_pose.GetOrientation().z == 6.6f);
	REQUIRE(parsed_pose.GetOrientation().w == 7.7f);
}

TEST_CASE("UUID to_chars and from_chars", "[RymlConverters]")
{
	UUID uuid("123e4567-e89b-12d3-a456-426655440000");
	char buffer[40] = {};

	size_t written = to_chars(ryml::substr(buffer), uuid);
	REQUIRE(written != ryml::yml::npos);
	REQUIRE(std::string(buffer) == "123e4567-e89b-12d3-a456-426655440000");

	UUID parsed_uuid;
	REQUIRE(from_chars("123e4567-e89b-12d3-a456-426655440000", &parsed_uuid));
	REQUIRE(parsed_uuid.string() == uuid.string());
}

TEST_CASE("DenseVector3i to_chars and from_chars", "[RymlConverters]")
{
	using MyDenseVector3i = DenseVector3i<int64_t, false, true, -1>;
	MyDenseVector3i vec(12, -5, 7);
	char buffer[64];

	size_t len = to_chars(ryml::substr(buffer, sizeof(buffer)), vec);
	REQUIRE(len > 0);

	std::string expected = "(12,-5,7)";
	REQUIRE(std::string(buffer, len) == expected);

	std::string serialized = "(12,-5,7)";
	MyDenseVector3i deserialized_vec(0, 0, 0);

	bool success = from_chars(ryml::csubstr(serialized.c_str(), serialized.size()), &deserialized_vec);
	REQUIRE(success);

	REQUIRE(deserialized_vec.X() == 12);
	REQUIRE(deserialized_vec.Y() == -5);
	REQUIRE(deserialized_vec.Z() == 7);

	len = to_chars(ryml::substr(buffer, sizeof(buffer)), vec);

	MyDenseVector3i roundtrip_vec(0, 0, 0);
	success = from_chars(ryml::csubstr(buffer, len), &roundtrip_vec);
	REQUIRE(success);

	REQUIRE(roundtrip_vec.X() == vec.X());
	REQUIRE(roundtrip_vec.Y() == vec.Y());
	REQUIRE(roundtrip_vec.Z() == vec.Z());
}

TEST_CASE("int24 to_chars and from_chars", "[RymlConverters]")
{
	char buffer[20];
	int24 originalValue = 1234567;
	c4::substr buf(buffer);

	size_t written = to_chars(buf, originalValue);
	REQUIRE(written > 0);
	REQUIRE(std::string(buffer, written) == "1234567");

	{
		int24 parsedValue;
		c4::csubstr buf("1234567");
		bool success = from_chars(buf, &parsedValue);
		REQUIRE(success);
		REQUIRE(static_cast<int>(parsedValue) == 1234567);
	}

	originalValue = -7654321;
	written = to_chars(buf, originalValue);
	REQUIRE(written > 0);
	REQUIRE(std::string(buffer, written) == "-7654321");

	{
		int24 parsedValue;
		c4::csubstr buf("-7654321");
		bool success = from_chars(buf, &parsedValue);
		REQUIRE(success);
		REQUIRE(static_cast<int>(parsedValue) == -7654321);
	}
}

TEST_CASE("Range serialization and deserialization", "[RymlConverters]")
{
	char buffer[30];
	c4::substr buf(buffer);

	SECTION("Serialization")
	{
		Range<int> range(5, 15);
		size_t written = to_chars(buf, range);
		REQUIRE(written > 0);
		REQUIRE(std::string(buffer, written) == "[5,15]");
	}

	SECTION("Deserialization")
	{
		Range<int> range;
		c4::csubstr buf("[5, 15]");
		bool success = from_chars(buf, &range);
		REQUIRE(success);
		REQUIRE(range.GetMin() == 5);
		REQUIRE(range.GetMax() == 15);
	}

	SECTION("Negative Range")
	{
		Range<int> range(-10, -2);
		size_t written = to_chars(buf, range);
		REQUIRE(written > 0);
		REQUIRE(std::string(buffer, written) == "[-10,-2]");

		Range<int> parsedRange;
		c4::csubstr negBuf("[-10,-2]");
		bool success = from_chars(negBuf, &parsedRange);
		REQUIRE(success);
		REQUIRE(parsedRange.GetMin() == -10);
		REQUIRE(parsedRange.GetMax() == -2);
	}

	SECTION("Floating Point Range")
	{
		Range<double> range(1.5, 3.75);
		size_t written = to_chars(buf, range);
		REQUIRE(written > 0);
		REQUIRE(std::string(buffer, written) == "[1.5,3.75]");

		Range<double> parsedRange;
		c4::csubstr floatBuf("[1.5,3.75]");
		bool success = from_chars(floatBuf, &parsedRange);
		REQUIRE(success);
		REQUIRE(parsedRange.GetMin() == 1.5);
		REQUIRE(parsedRange.GetMax() == 3.75);
	}
}

TEST_CASE("RGB10A2 serialization and deserialization", "[RymlConverters]")
{
	char buffer[50];
	c4::substr buf(buffer);

	SECTION("Serialization")
	{
		RGB10A2<uint32_t> color(0);
		color.r = 512;
		color.g = 768;
		color.b = 256;
		color.a = 3;

		size_t written = to_chars(buf, color);
		REQUIRE(written > 0);
		REQUIRE(std::string(buffer, written) == "[512,768,256,3]");
	}

	SECTION("Deserialization")
	{
		RGB10A2<uint32_t> color;
		c4::csubstr buf("[512,768,256,3]");
		bool success = from_chars(buf, &color);
		REQUIRE(success);
		REQUIRE(color.r == 512);
		REQUIRE(color.g == 768);
		REQUIRE(color.b == 256);
		REQUIRE(color.a == 3);
	}

	SECTION("Boundary Values")
	{
		RGB10A2<uint32_t> color;
		color.r = RGB10A2<uint32_t>::MAX_VALUE;
		color.g = RGB10A2<uint32_t>::MAX_VALUE;
		color.b = RGB10A2<uint32_t>::MAX_VALUE;
		color.a = RGB10A2<uint32_t>::MAX_ALPHA_VALUE;

		size_t written = to_chars(buf, color);
		REQUIRE(written > 0);
		REQUIRE(std::string(buffer, written)
		        == "[" + std::to_string(RGB10A2<uint32_t>::MAX_VALUE) + ","
		           + std::to_string(RGB10A2<uint32_t>::MAX_VALUE) + "," + std::to_string(RGB10A2<uint32_t>::MAX_VALUE)
		           + "," + std::to_string(RGB10A2<uint32_t>::MAX_ALPHA_VALUE) + "]");

		RGB10A2<uint32_t> parsedColor;
		c4::csubstr boundaryBuf(buffer, written);
		bool success = from_chars(boundaryBuf, &parsedColor);
		REQUIRE(success);
		REQUIRE(parsedColor.r == RGB10A2<uint32_t>::MAX_VALUE);
		REQUIRE(parsedColor.g == RGB10A2<uint32_t>::MAX_VALUE);
		REQUIRE(parsedColor.b == RGB10A2<uint32_t>::MAX_VALUE);
		REQUIRE(parsedColor.a == RGB10A2<uint32_t>::MAX_ALPHA_VALUE);
	}

	SECTION("Negative Values (Signed Type)")
	{
		RGB10A2<int32_t> color;
		color.r = -512;
		color.g = -256;
		color.b = -128;
		color.a = -1;

		size_t written = to_chars(buf, color);
		REQUIRE(written > 0);
		REQUIRE(std::string(buffer, written) == "[-512,-256,-128,-1]");

		RGB10A2<int32_t> parsedColor;
		c4::csubstr negBuf(buffer, written);
		bool success = from_chars(negBuf, &parsedColor);
		REQUIRE(success);
		REQUIRE(parsedColor.r == -512);
		REQUIRE(parsedColor.g == -256);
		REQUIRE(parsedColor.b == -128);
		REQUIRE(parsedColor.a == -1);
	}
}

TEST_CASE("RGB565 Serialization and Deserialization", "[RymlConverters]")
{
	Vector3<uint8_t> color { 255, 128, 64 };
	RGB565 rgb(color);

	char buffer[50];
	c4::substr buf(buffer);
	size_t written = to_chars(buffer, rgb);
	REQUIRE(written > 0);

	RGB565 rgbDeserialized;
	bool success = from_chars(buffer, &rgbDeserialized);
	REQUIRE(success);

	REQUIRE(rgb.GetR() == rgbDeserialized.GetR());
	REQUIRE(rgb.GetG() == rgbDeserialized.GetG());
	REQUIRE(rgb.GetB() == rgbDeserialized.GetB());
}

TEST_CASE("RGBA5551 serialization and deserialization", "[RymlConverters]")
{
	char buffer[50];
	c4::substr buf(buffer);

	SECTION("Serialization")
	{
		RGBA5551 color;
		color.r = 31;
		color.g = 31;
		color.b = 31;
		color.a = 1;

		size_t written = to_chars(buf, color);
		REQUIRE(written > 0);
		REQUIRE(std::string(buffer, written) == "[31,31,31,1]");
	}

	SECTION("Deserialization")
	{
		RGBA5551 color;
		c4::csubstr buf("[31,31,31,1]");
		bool success = from_chars(buf, &color);
		REQUIRE(success);
		REQUIRE(color.r == 31);
		REQUIRE(color.g == 31);
		REQUIRE(color.b == 31);
		REQUIRE(color.a == 1);
	}
}

TEST_CASE("Timestamp serialization and deserialization", "[RymlConverters]")
{
	char buffer[50];
	c4::substr buf(buffer);

	SECTION("Serialization")
	{
		Timestamp ts((uint64_t)123);
		size_t written = to_chars(buf, ts);
		REQUIRE(written > 0);
		REQUIRE(std::string(buffer, written) == "123");
	}

	SECTION("Deserialization")
	{
		Timestamp ts;
		c4::csubstr buf("123");
		bool success = from_chars(buf, &ts);
		REQUIRE(success);
		REQUIRE(ts.GetNanos() == 123);
	}
}