/*
 * 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_test_macros.hpp>

#include "Image/ImageLoaderPnm.hpp"
#include "Scene/DataFormat.hpp"

#include <fstream>

using namespace OpenVulkano;
using namespace OpenVulkano::Image;

TEST_CASE("ImageLoaderPnm - Load P1 (ASCII Black & White)")
{
	std::vector<uint8_t> p1Buffer = { 'P', '1', '\n', '4', ' ', '2', '\n', '1', ' ', '0', ' ', '1',
		                              ' ', '0', '\n', '0', ' ', '1', ' ',  '0', ' ', '1', '\n' };
	ImageLoaderPnm loader;
	auto image = loader.loadFromMemory(p1Buffer);

	REQUIRE(image->resolution.x == 4);
	REQUIRE(image->resolution.y == 2);
	REQUIRE(image->dataFormat == DataFormat::Format::R8_UINT);
	REQUIRE(image->data == Array<uint8_t> {  0, 255, 0, 255, 255, 0, 255, 0 });
}

TEST_CASE("ImageLoaderPnm - Load P2 (ASCII Grayscale)")
{
	std::vector<uint8_t> p2Buffer = { 'P', '2', '\n', '4', ' ', '2', '\n', '2', '5', '5', '\n', '0', ' ',
		                              '6', '4', ' ',  '1', '2', '8', ' ',  '2', '5', '5', '\n', '2', '5',
		                              '5', ' ', '1',  '2', '8', ' ', '6',  '4', ' ', '0', '\n' };
	ImageLoaderPnm loader;
	auto image = loader.loadFromMemory(p2Buffer);

	REQUIRE(image->resolution.x == 4);
	REQUIRE(image->resolution.y == 2);
	REQUIRE(image->dataFormat == DataFormat::Format::R8_UINT);
	REQUIRE(image->data == Array<uint8_t> { 255, 128, 64, 0, 0, 64, 128, 255 });
}

TEST_CASE("ImageLoaderPnm - Load P3 (ASCII RGB)")
{
	std::vector<uint8_t> p3Buffer = { 'P', '3', '\n', '2', ' ', '2', '\n', '2', '5', '5', '\n',
		'2', '5', '5', ' ', '0', ' ', '0', ' ', '0', ' ', '2', '5', '5', ' ', '0', '\n',
		'0', ' ', '0', ' ', '2', '5', '5', ' ', '2', '5', '5', ' ', '2', '5', '5', ' ',  '2', '5', '5', '\n' };
	ImageLoaderPnm loader;
	auto image = loader.loadFromMemory(p3Buffer);

	REQUIRE(image->resolution.x == 2);
	REQUIRE(image->resolution.y == 2);
	REQUIRE(image->dataFormat == DataFormat::Format::R8G8B8_UINT);
	REQUIRE(image->data == Array<uint8_t> { 0, 0, 255, 255, 255, 255, 255, 0, 0, 0, 255, 0 });
}

TEST_CASE("ImageLoaderPnm - Load P4 (Binary Black & White)")
{
	std::vector<uint8_t> p4Buffer = { 'P', '4', '\n', '8', ' ', '2', '\n', 0b10101010, 0b01010101 };
	ImageLoaderPnm loader;
	auto image = loader.loadFromMemory(p4Buffer);

	REQUIRE(image->resolution.x == 8);
	REQUIRE(image->resolution.y == 2);
	REQUIRE(image->dataFormat == DataFormat::Format::R8_UINT);
	REQUIRE(image->data
	        == Array<uint8_t> {
	        0, 255, 0, 255, 0, 255, 0, 255, // Row 1
	        255, 0, 255, 0, 255, 0, 255, 0 // Row 2
	        });
}

TEST_CASE("ImageLoaderPnm - Load P5 (Binary Grayscale)")
{
	std::vector<uint8_t> p5Buffer = { 'P',  '5', '\n', '4', ' ', '2', '\n', '2', '5', '5',
		                              '\n', 0,   64,   128, 255, 255, 128,  64,  0 };
	ImageLoaderPnm loader;
	auto image = loader.loadFromMemory(p5Buffer);

	REQUIRE(image->resolution.x == 4);
	REQUIRE(image->resolution.y == 2);
	REQUIRE(image->dataFormat == DataFormat::Format::R8_UINT);
	REQUIRE(image->data == Array<uint8_t> { 255, 128, 64, 0, 0, 64, 128, 255 });
}

TEST_CASE("ImageLoaderPnm - Load P6 (Binary RGB)")
{
	std::vector<uint8_t> p6Buffer = { 'P', '6', '\n', '2', ' ', '2', '\n', '2', '5', '5', '\n', 255,
		                              0,   0,   0,    255, 0,   0,   0,    255, 255, 255, 255 };
	ImageLoaderPnm loader;
	auto image = loader.loadFromMemory(p6Buffer);

	REQUIRE(image->resolution.x == 2);
	REQUIRE(image->resolution.y == 2);
	REQUIRE(image->dataFormat == DataFormat::Format::R8G8B8_UINT);
	REQUIRE(image->data == Array<uint8_t> { 0, 0, 255, 255, 255, 255, 255, 0, 0, 0, 255, 0 });
}

TEST_CASE("ImageLoaderPnm - Get Dimensions")
{
	ImageLoaderPnm loader;
	std::string mockFile = "mock.pnm";

	std::ofstream outFile(mockFile);
	outFile << "P6\n2 2\n255\n";
	outFile.close();

	auto dimensions = loader.GetImageDimensions(mockFile);
	REQUIRE(dimensions.x == 2);
	REQUIRE(dimensions.y == 2);

	std::remove(mockFile.c_str());
}