#include "../../../include/shared.hpp"
#include "include/gpu_test_assets.hpp"
#include <PSX/GPU/gpu.hpp>
#include <PSX/GPU/make_gpu_primitives.hpp>
#include <PSX/Periphery/periphery.hpp>
#include <PSX/Timer/high_res_timer.hpp>

namespace GPUTest {
    using namespace JabyEngine;

    // Some default values for the objects
    static constexpr auto TriangleColor = GPU::Color24::from_rgb(0x0, 0xFF, 0xFF);
    static constexpr auto TriangleArea  = Make::AreaI16(Make::PositionI16(0, 0), Make::SizeI16(64, 64));
    static constexpr auto TriangleTPage = Make::TPage(TexPageTIM.get_texture_position(), GPU::SemiTransparency::B_Half_add_F_Half, GPU::TextureColorMode::clut4);
    static constexpr auto TriangleClut  = Make::PageClut(TexPageTIM.get_clut_position());

    static constexpr auto RectangleColor = GPU::Color24::from_rgb(0x80, 0x80, 0xFF);
    static constexpr auto RectangleArea  = Make::AreaI16(Make::PositionI16(0, TriangleArea.size.height), Make::SizeI16(80, 80));
    static constexpr auto RectangleTPage = Make::TPage(IconTIM.get_texture_position(), GPU::SemiTransparency::B_Half_add_F_Half, GPU::TextureColorMode::clut4);
    static constexpr auto RectangleClut  = Make::PageClut(IconTIM.get_clut_position());

    static constexpr auto LineColor = GPU::Color24::from_rgb(0xFF, 0x0, 0x0);

    static constexpr const auto triangle1 = Make::POLY_F3({
            Make::Vertex(TriangleArea.position.x, TriangleArea.position.y),
            Make::Vertex(TriangleArea.size.width, TriangleArea.size.height),
            Make::Vertex(TriangleArea.position.x, TriangleArea.size.height)
        }, TriangleColor
    );
    static constexpr const auto triangle2 = Make::POLY_FT3({
            Make::Vertex(TriangleArea.position.x, TriangleArea.position.y), 
            Make::Vertex(TriangleArea.size.width, TriangleArea.position.y), 
            Make::Vertex(TriangleArea.size.width, TriangleArea.size.height)
        },{
        // Texture
            Make::PageOffset(TriangleArea.position.x, TriangleArea.position.y), 
            Make::PageOffset(TriangleArea.size.width, TriangleArea.position.y), 
            Make::PageOffset(TriangleArea.size.width, TriangleArea.size.height)
        }, TriangleTPage, TriangleClut, GPU::Color24::Grey()
    );
    static constexpr const auto triangle3 = Make::POLY_G3({
        {triangle1.vertex0.move(TriangleArea.size.width, 0), GPU::Color24::Red()},
        {triangle1.vertex1.move(TriangleArea.size.width, 0), GPU::Color24::Green()},
        {triangle1.vertex2.move(TriangleArea.size.width, 0), GPU::Color24::Blue()}}
    );
    static constexpr const auto triangle4 = Make::POLY_GT3({
        {triangle2.vertex0.move(TriangleArea.size.width, 0), triangle2.tex_offset0, GPU::Color24::Red()},
        {triangle2.vertex1.move(TriangleArea.size.width, 0), triangle2.tex_offset1, GPU::Color24::Blue()},
        {triangle2.vertex2.move(TriangleArea.size.width, 0), triangle2.tex_offset2, GPU::Color24::Green()}},
        TriangleTPage,
        TriangleClut
    );

    static constexpr const auto rectangle1 = Make::POLY_F4(RectangleArea, RectangleColor);
    static constexpr const auto rectangle2 = Make::POLY_FT4(Make::AreaI16(
        RectangleArea.position.move(RectangleArea.size.width, 0), RectangleArea.size), Make::PageOffset(0, 0), 
        RectangleTPage, 
        RectangleClut, 
        GPU::Color24::Grey()
    );
    static constexpr const auto rectangle3 = Make::POLY_G4(
        {RectangleArea.position.move(RectangleArea.size.width*2, 0), RectangleArea.size}, {
        GPU::Color24::Red(),
        GPU::Color24::Blue(),
        GPU::Color24::Green(),
        GPU::Color24::White()});
    static constexpr const auto rectangle4 = Make::POLY_GT4(Make::AreaI16(
        RectangleArea.position.move(RectangleArea.size.width*3, 0), RectangleArea.size), Make::PageOffset(0, 0),
        RectangleTPage, 
        RectangleClut, {
        GPU::Color24::Red(),
        GPU::Color24::Blue(),
        GPU::Color24::Green(),
        GPU::Color24::White()}
    );
    static constexpr const auto rectangle5 = Make::POLY_GT4(Make::AreaI16(
        RectangleArea.position.move(0, RectangleArea.size.height), RectangleArea.size), Make::PageOffset(0, 0),
        RectangleTPage, 
        RectangleClut, {
        GPU::Color24::Red(),
        GPU::Color24::Blue(),
        GPU::Color24::Green(),
        GPU::Color24::White()}
    ).set_semi_transparent(true);

    static constexpr const auto line1 = Make::LINE_F(LineColor,
        Make::Vertex(0,                               0),
        Make::Vertex(GPU::Display::Width, GPU::Display::Height)
    );
    static constexpr const auto line2 = Make::LINE_F(LineColor.invert(), 
        Make::Vertex(0,   0),
        Make::Vertex(16,  0),
        Make::Vertex(16, 16),
        Make::Vertex(0,   0)
    );
    static constexpr const auto line3 = Make::LINE_G(
        GPU::ColorVertex{LineColor,          Make::Vertex(GPU::Display::Width, 0)},
        GPU::ColorVertex{LineColor.invert(), Make::Vertex(0, GPU::Display::Height)}
    );
    static constexpr const auto line4 = Make::LINE_G(
        GPU::ColorVertex{GPU::Color24::Red(),   Make::Vertex(0,   0)},
        GPU::ColorVertex{GPU::Color24::Green(), Make::Vertex(0,  16)},
        GPU::ColorVertex{GPU::Color24::Blue(),  Make::Vertex(16, 16)},
        GPU::ColorVertex{GPU::Color24::White(), Make::Vertex(0,   0)}
    );

    static constexpr const auto rect1 = Make::TILE(Make::AreaI16(Make::PositionI16(GPU::Display::Width - 32, GPU::Display::Height - 32), Make::SizeI16(32, 32)), GPU::Color24::Green());
    static constexpr const auto rect2 = Make::TILE_16(Make::PositionI16(GPU::Display::Width - 16, GPU::Display::Height - 16),                                    GPU::Color24::Blue());
    static constexpr const auto rect3 = Make::TILE_8(Make::PositionI16(GPU::Display::Width -  8, GPU::Display::Height -  8),                                     GPU::Color24::Yellow());
    static constexpr const auto rect4 = Make::TILE_1(Make::PositionI16(GPU::Display::Width -  1, GPU::Display::Height -  1),                                     GPU::Color24::Red());

    static constexpr const auto texpage = Make::TexPage(TexPageTIM.get_texture_position(), GPU::TextureColorMode::clut4);
    static constexpr const auto rect5   = Make::SPRT(Make::AreaI16(Make::PositionI16(0, GPU::Display::Height - 32), Make::SizeI16(32, 32)), {Make::PageOffset(0, 0), TriangleClut}, GPU::Color24::Green());
    static constexpr const auto rect6   = Make::SPRT_16(Make::Vertex(0, GPU::Display::Height - 16), Make::OffsetPageWithClut(Make::PageOffset(0, 0), TriangleClut), GPU::Color24::Blue());
    static constexpr const auto rect7   = Make::SPRT_8(Make::Vertex(0, GPU::Display::Height   - 8), Make::OffsetPageWithClut(Make::PageOffset(0, 0), TriangleClut), GPU::Color24::Yellow());
    static constexpr const auto rect8   = Make::SPRT_1(Make::Vertex(0, GPU::Display::Height   - 1), Make::OffsetPageWithClut(Make::PageOffset(0, 0), TriangleClut), GPU::Color24::Red());

    static auto rect9  = Make::SPRT(Make::AreaI16(Make::PositionI16(GPU::Display::Width/2, GPU::Display::Height/2),      Make::SizeI16(32, 32)).centered(), Make::OffsetPageWithClut(Make::PageOffset(0, 0), TriangleClut), GPU::Color24::Grey()).linked();
    static auto rect10 = Make::SPRT(Make::AreaI16(Make::PositionI16(GPU::Display::Width/2, GPU::Display::Height/2 - 32), Make::SizeI16(32, 32)).centered(), Make::OffsetPageWithClut(Make::PageOffset(0, 0), TriangleClut), GPU::Color24::Grey()).linked();

    void main() {
        rect9.concat(rect10);
        Shared::back_menu.reset();
        HighResTime::enable();

        auto start_time  = HighResTime::get_time_stamp();
        auto time_passed = 0;
        while(true) {
            // Update Phase
            Periphery::query_controller();
            if(Shared::back_menu.update(Make::PositionI16(0, GPU::Display::Height - 32))) {
                break;
            }

            auto cursor = FontWriter::update(Make::PositionI16((GPU::Display::Width - 160)/2, GPU::Display::Height - 32));
            FontWriter::bios_font_writer.write(cursor, "Time: %ims", GPU::Color24::Blue(), time_passed);

            GPU::swap_buffers_vsync(1);
            const auto end_time = HighResTime::get_time_stamp();
            time_passed = start_time.milliseconds_to(end_time);
            start_time = end_time;

            GPU::render(triangle1);
            GPU::render(triangle2);
            GPU::render(triangle3);
            GPU::render(triangle4);

            GPU::render(rectangle1);
            GPU::render(rectangle2);
            GPU::render(rectangle3);
            GPU::render(rectangle4);
            GPU::render(rectangle5);

            GPU::render(rect1);
            GPU::render(rect2);
            GPU::render(rect3);
            GPU::render(rect4);
            GPU::render(texpage);
            GPU::render(rect5);
            GPU::render(rect6);
            GPU::render(rect7);
            GPU::render(rect8);

            GPU::render(line1);
            GPU::render(line2);
            GPU::render(line3);
            GPU::render(line4);

            GPU::render(rect9);
            Shared::back_menu.render();
        }
        HighResTime::disable();
    }
}
