/*
* 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 <cmath>

#include "Math/RGB565.hpp"

using namespace OpenVulkano::Math;

namespace
{
	bool almostEqual(float a, float b, float epsilon = 0.001f) { return std::fabs(a - b) < epsilon; }
}

TEST_CASE("test_rgb565_default_ctor", "[RGB565]")
{
	RGB565 color;
	REQUIRE(color.GetR() == 0);
	REQUIRE(color.GetG() == 0);
	REQUIRE(color.GetB() == 0);
}

TEST_CASE("test_rgb565_creation", "[RGB565]")
{
	RGB565 color1(Vector4uc(255, 255, 255, 129));
	REQUIRE(color1.r == 31);
	REQUIRE(color1.g == 63);
	REQUIRE(color1.b == 31);

	RGB565 color2(Vector4f(1.0f, 1.0f, 1.0f, 1.0f));
	REQUIRE(color2.r == 31);
	REQUIRE(color2.g == 63);
	REQUIRE(color2.b == 31);

	RGB565 color3(Vector4i(128, 128, 128, 0));
	REQUIRE(color3.r == 15);
	REQUIRE(color3.g == 31);
	REQUIRE(color3.b == 15);
}

TEST_CASE("test_rgb565_conversion_to_vector3", "[RGB565]")
{
	RGB565 color(Vector4i(255, 0, 0, 1));
	Vector3f vec = color.Get3Normalized<float>();
	REQUIRE(almostEqual(vec.x, 1.0f));
	REQUIRE(almostEqual(vec.y, 0.0f));
	REQUIRE(almostEqual(vec.z, 0.0f));

	Vector3f vec3 { 0.1f, 0.5f, 1.0f };
	RGB565 color2(vec3);
	REQUIRE(almostEqual(color2.GetR_Normalized(), 0.096));
	REQUIRE(almostEqual(color2.GetG_Normalized(), 0.492));
	REQUIRE(almostEqual(color2.GetB_Normalized(), 1.0));
}

TEST_CASE("test_rgb565_conversion_to_vector4", "[RGB565]")
{
	RGB565 color(Vector4i(255, 0, 0, 129));
	Vector4f vec = color.Get4Normalized<float>();
	REQUIRE(almostEqual(vec.x, 1.0f));
	REQUIRE(almostEqual(vec.y, 0.0f));
	REQUIRE(almostEqual(vec.z, 0.0f));
	REQUIRE(almostEqual(vec.w, 1.0f));
}

TEST_CASE("test_rgb565_conversion_from_vector3", "[RGB565]")
{
	Vector3f vec(1.0f, 0.5f, 0.25f);
	RGB565 color(vec);
	REQUIRE(color.r == 31);
	REQUIRE(color.g == 31);
	REQUIRE(color.b == 7);
}

TEST_CASE("test_rgb565_conversion_from_vector4", "[RGB565]")
{
	Vector4f vec(0.0f, 1.0f, 0.5f, 0.0f);
	RGB565 color(vec);
	REQUIRE(color.r == 0);
	REQUIRE(color.g == 63);
	REQUIRE(color.b == 15);
}

TEST_CASE("test_rgb565_cast_to_vector4", "[RGB565]")
{
	RGB565 color;
	color.r = 31;
	color.g = 31;
	color.b = 7;
	Vector4f casted = (Vector4f)color;
	REQUIRE(almostEqual(casted.r, 1.0));
	REQUIRE(almostEqual(casted.g, 0.492));
	REQUIRE(almostEqual(casted.b, 0.225));
}

TEST_CASE("test_rgb565_getters_setters", "[RGB565]")
{
	RGB565 color;
	color.SetR_Normalized(0.1f);
	color.SetG_Normalized(0.5f);
	color.SetB_Normalized(1.0f);

	REQUIRE(almostEqual(color.GetR_Normalized(), 0.096));
	REQUIRE(almostEqual(color.GetG_Normalized(), 0.492));
	REQUIRE(almostEqual(color.GetB_Normalized(), 1.0));

	color = RGB565(Vector3<uint8_t> { 255, 127, 63 });
	Vector3<uint8_t> vec3 = color;

	REQUIRE(vec3.r == 255);
	REQUIRE(vec3.g == 125);
	REQUIRE(vec3.b == 57);
}

TEST_CASE("test_rgb565_comparison", "[RGB565]")
{
	RGB565 color1(Vector4<uint8_t>{255, 127, 63, 255});
    RGB565 color2(Vector4<uint8_t>{255, 127, 63, 255});
    RGB565 color3(Vector4<uint8_t>{63, 127, 255, 255});

	REQUIRE(color1 == color2);
	REQUIRE(color1 != color3);

	{
		RGB565 color4 = color1;
        REQUIRE(color4 == color1);
	}

	Vector3<uint8_t> vec3 { 255, 127, 63 };
	color1 = vec3;
	REQUIRE(color1.GetR() == 255);
	REQUIRE(color1.GetG() == 125);
	REQUIRE(color1.GetB() == 57);

	Vector4<float> vec4 { 0.1f, 0.5f, 1.0f, 0.7f };
	color1 = vec4;
	REQUIRE(almostEqual(color1.GetR_Normalized(), 0.096));
	REQUIRE(almostEqual(color1.GetG_Normalized(), 0.492));
	REQUIRE(almostEqual(color1.GetB_Normalized(), 1.0));
}

TEST_CASE("test_rgb565_operators", "[RGB565]")
{
	RGB565 rgb1, rgb2;

	rgb1.SetR(255);
	rgb1.SetG(127);
	rgb1.SetB(63);

	rgb2.SetR(255);
	rgb2.SetG(127);
	rgb2.SetB(63);

	REQUIRE(rgb1 == rgb2);

	rgb2.SetB(0);
	REQUIRE(rgb1 != rgb2);

	rgb1.SetR(255);
	rgb1.SetG(127);
	rgb1.SetB(63);

	rgb2 = rgb1;
	REQUIRE(rgb1 == rgb2);

	Vector3i delta(10, 20, 30);
	rgb1.SetR(100);
	rgb1.SetG(50);
	rgb1.SetB(25);

	rgb1 += delta;

	REQUIRE(rgb1.GetR() == 106);
	REQUIRE(rgb1.GetG() == 64);
	REQUIRE(rgb1.GetB() == 49);

	rgb1.SetR(100);
	rgb1.SetG(50);
	rgb1.SetB(25);

	rgb1 -= delta;

	uint8_t r = rgb1.GetR();
	uint8_t g = rgb1.GetG();
	uint8_t b = rgb1.GetB();

	REQUIRE(rgb1.GetR() == 82);
	REQUIRE(rgb1.GetG() == 24);
	REQUIRE(rgb1.GetB() == 0); // Expect to clamp here
}