#pragma once
#include <PSX/GPU/gpu.hpp>
#include <PSX/GPU/make_gpu_primitives.hpp>
#include <PSX/System/IOPorts/dma_io.hpp>

namespace ScreenCenter {
    using namespace JabyEngine;
    
    class Frame {
    private:
        struct TopBorder : public GPU::internal::LinkedElementCreator<TopBorder> {
            GPU::TILE top_left[2];
            GPU::TILE top_right[2];

            static constexpr TopBorder::Linked create(GPU::Color24 BaseColor, GPU::SizeI16 Size) {
                TopBorder frame;

                frame.top_left[0] = Make::TILE(Make::AreaI16(0, 0, Size.width, Size.height), BaseColor);
                frame.top_left[1] = Make::TILE(Make::AreaI16(0, 0, Size.height, Size.width), BaseColor);

                frame.top_right[0] = Make::TILE(Make::AreaI16(GPU::Display::Width - Size.width, 0, Size.width, Size.height), BaseColor);
                frame.top_right[1] = Make::TILE(Make::AreaI16(GPU::Display::Width - Size.height, 0, Size.height, Size.width), BaseColor);
                return frame.linked();
            }
        };

        struct BottomBorder : GPU::internal::LinkedElementCreator<BottomBorder> {
            GPU::TILE bottom_left[2];
            GPU::TILE bottom_right[2];

            static constexpr BottomBorder::Linked create(GPU::Color24 BaseColor, GPU::SizeI16 Size) {
                BottomBorder frame;

                frame.bottom_left[0] = Make::TILE(Make::AreaI16(0, GPU::Display::Height - Size.width, Size.height, Size.width), BaseColor);
                frame.bottom_left[1] = Make::TILE(Make::AreaI16(0, GPU::Display::Height - Size.height, Size.width, Size.height), BaseColor);

                frame.bottom_right[0] = Make::TILE(Make::AreaI16(GPU::Display::Width - Size.height, GPU::Display::Height - Size.width, Size.height, Size.width), BaseColor);
                frame.bottom_right[1] = Make::TILE(Make::AreaI16(GPU::Display::Width - Size.width, GPU::Display::Height - Size.height, Size.width, Size.height), BaseColor);
                return frame.linked();
            }
        };

        struct LineBorder : GPU::internal::LinkedElementCreator<LineBorder> {
            GPU::LINE_G_MULTI<5> border;

            static constexpr LineBorder::Linked create(int16_t offset = 0) {
                const auto get_color = [](size_t idx, int16_t offset) -> GPU::Color24 {
                    const GPU::Color24 Colors[4] = {GPU::Color24::Red(), GPU::Color24::Green(), GPU::Color24::Blue(), GPU::Color24::Yellow()};
                
                    return Colors[(idx + offset)%4];
                };
                const int16_t origin    = 0 + offset;
                const int16_t width     = GPU::Display::Width  - 1 - offset;
                const int16_t height    = GPU::Display::Height - 1 - offset;
                LineBorder    frame;
                
                frame.border = Make::LINE_G(
                    GPU::ColorVertex{get_color(0, offset), Make::Vertex(origin, origin)},
                    GPU::ColorVertex{get_color(1, offset), Make::Vertex(origin, height)},
                    GPU::ColorVertex{get_color(2, offset), Make::Vertex(width,  height)},
                    GPU::ColorVertex{get_color(3, offset), Make::Vertex(width,  origin)},
                    GPU::ColorVertex{get_color(4, offset), Make::Vertex(origin, origin)}
                );
                return frame.linked();
            }
        };

        struct LineCross : GPU::internal::LinkedElementCreator<LineCross> {
            GPU::LINE_G_SINGLE cross[2];

            static constexpr LineCross::Linked create(const decltype(LineBorder::border)& border) {
                LineCross frame;

                frame.cross[0] = Make::LINE_G(border[0], border[2]);
                frame.cross[1] = Make::LINE_G(border[3], border[1]);
                return frame.linked();
            }
        };

        TopBorder::Linked    top_border;
        BottomBorder::Linked bottom_border;
        LineBorder::Linked   line_border[2];
        LineCross::Linked    line_cross;
    public:
        static constexpr Frame create() {
            constexpr auto BaseColor = GPU::Color24::from_rgb(0x1D, 0xA0, 0xA3);
            constexpr auto Size      = Make::SizeI16(64, 16);

            Frame frame;

            frame.top_border     = TopBorder::create(BaseColor, Size);
            frame.bottom_border  = BottomBorder::create(BaseColor, Size);
            frame.line_border[0] = LineBorder::create();
            frame.line_border[1] = LineBorder::create(1);
            frame.line_cross     = LineCross::create(frame.line_border[0]->border);
            return frame;
        }

        void setup() {
            this->top_border.concat(this->bottom_border.concat(this->line_border[0].concat(this->line_border[1].concat(this->line_cross))));
        }

        void render() const {
            GPU::render(this->top_border);
        }
    };
}