#include "assets/image_pixels.h"
#include "test_harness.h"

#include <array>
#include <cstddef>
#include <cstdint>
#include <span>
#include <string_view>
#include <vector>

using pp::assets::decode_png_rgba8;
using pp::assets::decode_jpeg_rgba8;
using pp::assets::encode_jpeg_rgba8;
using pp::assets::encode_png_rgba8;
using pp::assets::inject_gpano_xmp_into_jpeg;
using pp::foundation::StatusCode;

namespace {

constexpr std::array<std::byte, 68> transparent_png_1x1 {
    std::byte { 0x89 }, std::byte { 0x50 }, std::byte { 0x4e }, std::byte { 0x47 },
    std::byte { 0x0d }, std::byte { 0x0a }, std::byte { 0x1a }, std::byte { 0x0a },
    std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x0d },
    std::byte { 0x49 }, std::byte { 0x48 }, std::byte { 0x44 }, std::byte { 0x52 },
    std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x01 },
    std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x01 },
    std::byte { 0x08 }, std::byte { 0x06 }, std::byte { 0x00 }, std::byte { 0x00 },
    std::byte { 0x00 }, std::byte { 0x1f }, std::byte { 0x15 }, std::byte { 0xc4 },
    std::byte { 0x89 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 },
    std::byte { 0x0b }, std::byte { 0x49 }, std::byte { 0x44 }, std::byte { 0x41 },
    std::byte { 0x54 }, std::byte { 0x78 }, std::byte { 0x9c }, std::byte { 0x63 },
    std::byte { 0x60 }, std::byte { 0x00 }, std::byte { 0x02 }, std::byte { 0x00 },
    std::byte { 0x00 }, std::byte { 0x05 }, std::byte { 0x00 }, std::byte { 0x01 },
    std::byte { 0x7a }, std::byte { 0x5e }, std::byte { 0xab }, std::byte { 0x3f },
    std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 },
    std::byte { 0x49 }, std::byte { 0x45 }, std::byte { 0x4e }, std::byte { 0x44 },
    std::byte { 0xae }, std::byte { 0x42 }, std::byte { 0x60 }, std::byte { 0x82 },
};

void decodes_png_to_rgba8_pixels(pp::tests::Harness& h)
{
    const auto image = decode_png_rgba8(transparent_png_1x1);

    PP_EXPECT(h, image.ok());
    PP_EXPECT(h, image.value().width == 1U);
    PP_EXPECT(h, image.value().height == 1U);
    PP_EXPECT(h, image.value().pixels.size() == 4U);
    PP_EXPECT(h, image.value().pixels[0] == 0U);
    PP_EXPECT(h, image.value().pixels[1] == 0U);
    PP_EXPECT(h, image.value().pixels[2] == 0U);
    PP_EXPECT(h, image.value().pixels[3] == 0U);
}

void rejects_corrupt_png_payload(pp::tests::Harness& h)
{
    auto corrupt = transparent_png_1x1;
    corrupt[0] = std::byte { 0x00 };

    const auto image = decode_png_rgba8(corrupt);

    PP_EXPECT(h, !image.ok());
    PP_EXPECT(h, image.status().code == StatusCode::invalid_argument);
}

void encodes_rgba8_pixels_to_decodable_png(pp::tests::Harness& h)
{
    const std::vector<std::uint8_t> pixels {
        255, 0, 0, 255,
        0, 255, 0, 128,
    };

    const auto encoded = encode_png_rgba8(2, 1, pixels);

    PP_EXPECT(h, encoded.ok());
    const auto decoded = decode_png_rgba8(encoded.value());
    PP_EXPECT(h, decoded.ok());
    PP_EXPECT(h, decoded.value().width == 2U);
    PP_EXPECT(h, decoded.value().height == 1U);
    PP_EXPECT(h, decoded.value().pixels == pixels);
}

void encodes_rgba8_pixels_to_decodable_jpeg(pp::tests::Harness& h)
{
    const std::vector<std::uint8_t> pixels {
        255, 0, 0, 255,
        0, 255, 0, 255,
        0, 0, 255, 255,
        255, 255, 255, 255,
    };

    const auto encoded = encode_jpeg_rgba8(2, 2, pixels, 95);

    PP_EXPECT(h, encoded.ok());
    PP_EXPECT(h, encoded.value().size() > 4U);
    PP_EXPECT(h, encoded.value()[0] == std::byte { 0xff });
    PP_EXPECT(h, encoded.value()[1] == std::byte { 0xd8 });
    const auto decoded = decode_jpeg_rgba8(encoded.value());
    PP_EXPECT(h, decoded.ok());
    PP_EXPECT(h, decoded.value().width == 2U);
    PP_EXPECT(h, decoded.value().height == 2U);
    PP_EXPECT(h, decoded.value().pixels.size() == pixels.size());
}

void injects_gpano_xmp_into_jpeg(pp::tests::Harness& h)
{
    const std::vector<std::uint8_t> pixels {
        10, 20, 30, 255,
        40, 50, 60, 255,
    };
    const auto encoded = encode_jpeg_rgba8(2, 1, pixels, 90);
    PP_EXPECT(h, encoded);
    if (!encoded) {
        return;
    }

    const auto with_xmp = inject_gpano_xmp_into_jpeg(encoded.value());
    PP_EXPECT(h, with_xmp);
    if (!with_xmp) {
        return;
    }

    PP_EXPECT(h, with_xmp.value().size() > encoded.value().size());
    PP_EXPECT(h, with_xmp.value()[0] == std::byte { 0xff });
    PP_EXPECT(h, with_xmp.value()[1] == std::byte { 0xd8 });
    PP_EXPECT(h, with_xmp.value()[2] == std::byte { 0xff });
    PP_EXPECT(h, with_xmp.value()[3] == std::byte { 0xe1 });

    const std::string_view text(
        reinterpret_cast<const char*>(with_xmp.value().data()),
        with_xmp.value().size());
    PP_EXPECT(h, text.find("GPano:ProjectionType") != std::string_view::npos);
    PP_EXPECT(h, text.find("equirectangular") != std::string_view::npos);

    const auto decoded = decode_jpeg_rgba8(with_xmp.value());
    PP_EXPECT(h, decoded);
    if (decoded) {
        PP_EXPECT(h, decoded.value().width == 2U);
        PP_EXPECT(h, decoded.value().height == 1U);
    }
}

void rejects_invalid_png_encode_inputs(pp::tests::Harness& h)
{
    const std::vector<std::uint8_t> pixels { 0, 0, 0, 0 };

    const auto no_size = encode_png_rgba8(0, 1, pixels);
    const auto wrong_payload_size = encode_png_rgba8(2, 1, pixels);

    PP_EXPECT(h, !no_size.ok());
    PP_EXPECT(h, no_size.status().code == StatusCode::invalid_argument);
    PP_EXPECT(h, !wrong_payload_size.ok());
    PP_EXPECT(h, wrong_payload_size.status().code == StatusCode::invalid_argument);
}

void rejects_invalid_jpeg_inputs(pp::tests::Harness& h)
{
    const std::vector<std::uint8_t> pixels { 0, 0, 0, 255 };
    const std::byte corrupt[] { std::byte { 0x00 }, std::byte { 0x01 } };

    const auto no_size = encode_jpeg_rgba8(0, 1, pixels, 90);
    const auto bad_quality = encode_jpeg_rgba8(1, 1, pixels, 0);
    const auto wrong_payload_size = encode_jpeg_rgba8(2, 1, pixels, 90);
    const auto corrupt_decode = decode_jpeg_rgba8(corrupt);
    const auto corrupt_xmp = inject_gpano_xmp_into_jpeg(corrupt);

    PP_EXPECT(h, !no_size.ok());
    PP_EXPECT(h, no_size.status().code == StatusCode::invalid_argument);
    PP_EXPECT(h, !bad_quality.ok());
    PP_EXPECT(h, bad_quality.status().code == StatusCode::out_of_range);
    PP_EXPECT(h, !wrong_payload_size.ok());
    PP_EXPECT(h, wrong_payload_size.status().code == StatusCode::invalid_argument);
    PP_EXPECT(h, !corrupt_decode.ok());
    PP_EXPECT(h, corrupt_decode.status().code == StatusCode::invalid_argument);
    PP_EXPECT(h, !corrupt_xmp.ok());
    PP_EXPECT(h, corrupt_xmp.status().code == StatusCode::invalid_argument);
}

}

int main()
{
    pp::tests::Harness harness;
    harness.run("decodes_png_to_rgba8_pixels", decodes_png_to_rgba8_pixels);
    harness.run("rejects_corrupt_png_payload", rejects_corrupt_png_payload);
    harness.run("encodes_rgba8_pixels_to_decodable_png", encodes_rgba8_pixels_to_decodable_png);
    harness.run("encodes_rgba8_pixels_to_decodable_jpeg", encodes_rgba8_pixels_to_decodable_jpeg);
    harness.run("injects_gpano_xmp_into_jpeg", injects_gpano_xmp_into_jpeg);
    harness.run("rejects_invalid_png_encode_inputs", rejects_invalid_png_encode_inputs);
    harness.run("rejects_invalid_jpeg_inputs", rejects_invalid_jpeg_inputs);
    return harness.finish();
}
