/*
 * 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/YamlCppConverters.hpp"
#include "Math/Math.hpp"

using namespace OpenVulkano;
using namespace OpenVulkano::Math;

TEST_CASE("UUID encoding and decoding", "[YamlCppConverters]")
{
	UUID uuid("123e4567-e89b-12d3-a456-426614174000");
	YAML::Node node = YAML::convert<UUID>::encode(uuid);
	REQUIRE(node.IsScalar());
	REQUIRE(node.as<std::string>() == uuid.string());

	UUID decodedUuid;
	REQUIRE(YAML::convert<UUID>::decode(node, decodedUuid));
	REQUIRE(decodedUuid == uuid);
}

TEST_CASE("AABB encoding and decoding", "[YamlCppConverters]")
{
	Math::AABB bbox { { 1.0f, 2.0f, 3.0f }, { 4.0f, 5.0f, 6.0f } };
	YAML::Node node = YAML::convert<Math::AABB>::encode(bbox);
	REQUIRE(node.IsScalar());

	Math::AABB decodedBbox;
	REQUIRE(YAML::convert<Math::AABB>::decode(node, decodedBbox));
	REQUIRE(decodedBbox.min == bbox.min);
	REQUIRE(decodedBbox.max == bbox.max);
}

TEST_CASE("PoseF encoding and decoding", "[YamlCppConverters]")
{
	Math::PoseF pose;
	pose.SetPosition({1.0f, 2.0f, 3.0f});
	pose.SetOrientation({1.0f, 2.0f, 3.0f, 1.0f});
	YAML::Node node = YAML::convert<Math::PoseF>::encode(pose);
	REQUIRE(node.IsScalar());

	Math::PoseF decodedPose;
	REQUIRE(YAML::convert<Math::PoseF>::decode(node, decodedPose));
	REQUIRE(decodedPose.GetPosition() == pose.GetPosition());
	REQUIRE(decodedPose.GetOrientation() == pose.GetOrientation());
}

TEST_CASE("Vector2 encoding and decoding", "[YamlCppConverters]")
{
	Math::Vector2<float> vec { 1.0f, 2.0f };
	YAML::Node node = YAML::convert<Math::Vector2<float>>::encode(vec);
	REQUIRE(node.IsScalar());

	Math::Vector2<float> decodedVec;
	REQUIRE(YAML::convert<Math::Vector2<float>>::decode(node, decodedVec));
	REQUIRE(decodedVec == vec);
}

TEST_CASE("Vector3 encoding and decoding", "[YamlCppConverters]")
{
	Math::Vector3<float> vec { 1.0f, 2.0f, 3.0f };
	YAML::Node node = YAML::convert<Math::Vector3<float>>::encode(vec);
	REQUIRE(node.IsScalar());

	Math::Vector3<float> decodedVec;
	REQUIRE(YAML::convert<Math::Vector3<float>>::decode(node, decodedVec));
	REQUIRE(decodedVec == vec);
}

TEST_CASE("Vector4 encoding and decoding", "[YamlCppConverters]")
{
	Math::Vector4<float> vec { 1.0f, 2.0f, 3.0f, 4.0f };
	YAML::Node node = YAML::convert<Math::Vector4<float>>::encode(vec);
	REQUIRE(node.IsScalar());

	Math::Vector4<float> decodedVec;
	REQUIRE(YAML::convert<Math::Vector4<float>>::decode(node, decodedVec));
	REQUIRE(decodedVec == vec);
}

TEST_CASE("Matrix3 encoding and decoding", "[YamlCppConverters]")
{
	Math::Matrix3<float> mat { { 1.0f, 2.0f, 3.0f }, { 4.0f, 5.0f, 6.0f }, { 7.0f, 8.0f, 9.0f } };
	YAML::Node node = YAML::convert<Math::Matrix3<float>>::encode(mat);
	REQUIRE(node.IsScalar());

	Math::Matrix3<float> decodedMat;
	REQUIRE(YAML::convert<Math::Matrix3<float>>::decode(node, decodedMat));
	REQUIRE(decodedMat == mat);
}

TEST_CASE("Matrix4 encoding and decoding", "[YamlCppConverters]")
{
	Math::Matrix4<float> mat { { 1.0f, 2.0f, 3.0f, 4.0f },
		                       { 5.0f, 6.0f, 7.0f, 8.0f },
		                       { 9.0f, 10.0f, 11.0f, 12.0f },
		                       { 13.0f, 14.0f, 15.0f, 16.0f } };
	YAML::Node node = YAML::convert<Math::Matrix4<float>>::encode(mat);
	REQUIRE(node.IsScalar());

	Math::Matrix4<float> decodedMat;
	REQUIRE(YAML::convert<Math::Matrix4<float>>::decode(node, decodedMat));
	REQUIRE(decodedMat == mat);
}

TEST_CASE("Vector2_SIMD encoding and decoding", "[YamlCppConverters]")
{
	Math::Vector2_SIMD<float> vec { 1.0f, 2.0f };
	YAML::Node node = YAML::convert<Math::Vector2_SIMD<float>>::encode(vec);
	REQUIRE(node.IsScalar());

	Math::Vector2_SIMD<float> decodedVec;
	REQUIRE(YAML::convert<Math::Vector2_SIMD<float>>::decode(node, decodedVec));
	REQUIRE(decodedVec == vec);
}

TEST_CASE("Vector3_SIMD encoding and decoding", "[YamlCppConverters]")
{
	Math::Vector3_SIMD<float> vec { 1.0f, 2.0f, 3.0f };
	YAML::Node node = YAML::convert<Math::Vector3_SIMD<float>>::encode(vec);
	REQUIRE(node.IsScalar());

	Math::Vector3_SIMD<float> decodedVec;
	REQUIRE(YAML::convert<Math::Vector3_SIMD<float>>::decode(node, decodedVec));
	REQUIRE(decodedVec == vec);
}

TEST_CASE("Vector4_SIMD encoding and decoding", "[YamlCppConverters]")
{
	Math::Vector4_SIMD<float> vec { 1.0f, 2.0f, 3.0f, 4.0f };
	YAML::Node node = YAML::convert<Math::Vector4_SIMD<float>>::encode(vec);
	REQUIRE(node.IsScalar());

	Math::Vector4_SIMD<float> decodedVec;
	REQUIRE(YAML::convert<Math::Vector4_SIMD<float>>::decode(node, decodedVec));
	REQUIRE(decodedVec == vec);
}

TEST_CASE("Matrix3_SIMD encoding and decoding", "[YamlCppConverters]")
{
	Math::Matrix3_SIMD<float> mat { { 1.0f, 2.0f, 3.0f }, { 4.0f, 5.0f, 6.0f }, { 7.0f, 8.0f, 9.0f } };
	YAML::Node node = YAML::convert<Math::Matrix3_SIMD<float>>::encode(mat);
	REQUIRE(node.IsScalar());

	Math::Matrix3_SIMD<float> decodedMat;
	REQUIRE(YAML::convert<Math::Matrix3_SIMD<float>>::decode(node, decodedMat));
	REQUIRE(decodedMat == mat);
}

TEST_CASE("Matrix4_SIMD encoding and decoding", "[YamlCppConverters]")
{
	Math::Matrix4_SIMD<float> mat { { 1.0f, 2.0f, 3.0f, 4.0f },
		                            { 5.0f, 6.0f, 7.0f, 8.0f },
		                            { 9.0f, 10.0f, 11.0f, 12.0f },
		                            { 13.0f, 14.0f, 15.0f, 16.0f } };
	YAML::Node node = YAML::convert<Math::Matrix4_SIMD<float>>::encode(mat);
	REQUIRE(node.IsScalar());

	Math::Matrix4_SIMD<float> decodedMat;
	REQUIRE(YAML::convert<Math::Matrix4_SIMD<float>>::decode(node, decodedMat));
	REQUIRE(decodedMat == mat);
}

TEST_CASE("DenseVector3i encoding and decoding", "[YamlCppConverters]")
{
    OpenVulkano::Math::DenseVector3i<int, true, true, 8> vec { 1, 2, 3 };
    YAML::Node node = YAML::convert<OpenVulkano::Math::DenseVector3i<int, true, true, 8>>::encode(vec);
    REQUIRE(node.IsScalar());

    OpenVulkano::Math::DenseVector3i<int, true, true, 8> decodedVec(0, 0, 0);
    REQUIRE(YAML::convert<OpenVulkano::Math::DenseVector3i<int, true, true, 8>>::decode(node, decodedVec));
    REQUIRE(decodedVec == vec);
}

TEST_CASE("int24 encoding and decoding", "[YamlCppConverters]")
{
    OpenVulkano::int24 value(42);
    YAML::Node node = YAML::convert<OpenVulkano::int24>::encode(value);
    REQUIRE(node.IsScalar());

    OpenVulkano::int24 decodedValue;
    REQUIRE(YAML::convert<OpenVulkano::int24>::decode(node, decodedValue));
    REQUIRE(decodedValue == value);
}

TEST_CASE("Range encoding and decoding", "[YamlCppConverters]")
{
    OpenVulkano::Math::Range<int> range(10, 20);
    YAML::Node node = YAML::convert<OpenVulkano::Math::Range<int>>::encode(range);
    REQUIRE(node.IsScalar());

    OpenVulkano::Math::Range<int> decodedRange;
    REQUIRE(YAML::convert<OpenVulkano::Math::Range<int>>::decode(node, decodedRange));
    REQUIRE(decodedRange.GetMin() == range.GetMin());
    REQUIRE(decodedRange.GetMax() == range.GetMax());
}

TEST_CASE("RGB10A2 encoding and decoding", "[YamlCppConverters]")
{
    OpenVulkano::Math::RGB10A2<int> color;
    color.Set(OpenVulkano::Math::Vector4i(1023, 512, 256, 3));

    YAML::Node node = YAML::convert<OpenVulkano::Math::RGB10A2<int>>::encode(color);
    REQUIRE(node.IsScalar());

    OpenVulkano::Math::RGB10A2<int> decodedColor;
    REQUIRE(YAML::convert<OpenVulkano::Math::RGB10A2<int>>::decode(node, decodedColor));
    REQUIRE(decodedColor == color);
}

TEST_CASE("RGB565 encoding and decoding", "[YamlCppConverters]")
{
    OpenVulkano::Math::RGB565 color;
    color.Set(OpenVulkano::Math::Vector3i(31, 63, 31));

    YAML::Node node = YAML::convert<OpenVulkano::Math::RGB565>::encode(color);
    REQUIRE(node.IsScalar());

    OpenVulkano::Math::RGB565 decodedColor;
    REQUIRE(YAML::convert<OpenVulkano::Math::RGB565>::decode(node, decodedColor));
    REQUIRE(decodedColor == color);
}

TEST_CASE("RGBA5551 encoding and decoding", "[YamlCppConverters]")
{
    OpenVulkano::Math::RGBA5551 color(OpenVulkano::Math::Vector4i(31, 31, 31, 1));

    YAML::Node node = YAML::convert<OpenVulkano::Math::RGBA5551>::encode(color);
    REQUIRE(node.IsScalar());

    OpenVulkano::Math::RGBA5551 decodedColor;
    REQUIRE(YAML::convert<OpenVulkano::Math::RGBA5551>::decode(node, decodedColor));
    REQUIRE(decodedColor == color);
}

TEST_CASE("Timestamp encoding and decoding", "[YamlCppConverters]")
{
    OpenVulkano::Math::Timestamp timestamp(uint64_t(1234567890));
    YAML::Node node = YAML::convert<OpenVulkano::Math::Timestamp>::encode(timestamp);
    REQUIRE(node.IsScalar());

    OpenVulkano::Math::Timestamp decodedTimestamp;
    REQUIRE(YAML::convert<OpenVulkano::Math::Timestamp>::decode(node, decodedTimestamp));
    REQUIRE(decodedTimestamp == timestamp);
}