#include "../../../include/asset_mgr.hpp"
#include "../../../include/shared.hpp"
#include <PSX/Auxiliary/types.hpp>
#include <PSX/Periphery/periphery.hpp>
#include <PSX/System/syscalls.hpp>
#include <PSX/Timer/frame_timer.hpp>
#include <FontWriter/fonts.hpp>
#include <string.hpp>

namespace BIOSInfo {
    using namespace JabyEngine;
    static constexpr auto TextOffset = Make::PositionI16(16, 16);

    using NameColorPair = pair<const char*, GPU::Color24>;

    struct FontSlider {
        static constexpr auto MoveTimeout = static_cast<uint8_t>(300_ms);
        static constexpr auto WaitTimeout = static_cast<uint8_t>(1000_ms);

        int16_t                count;
        int16_t                max;
        int8_t                 delta;
        IntervalTimer<uint8_t> wait_timer;

        static FontSlider create_for(const FontWriter::FontInfo& font_info, const char* str) {
            const auto max = static_cast<int16_t>((strlen(str)*font_info.get_kern_size().width) - GPU::Display::Width + (TextOffset.x << 1));
            return FontSlider{
                .count      = 0, 
                .max        = max, 
                .delta      = static_cast<int8_t>(max < 0 ? 0 : font_info.get_kern_size().width/2), 
                .wait_timer = IntervalTimer<uint8_t>::create(FontSlider::MoveTimeout)
            };
        }

        void advance() {
            if(this->wait_timer.is_expired()) {
                this->wait_timer.reset();
                this->count += delta;

                if(this->count <= 0 || this->count >= this->max) {
                    this->delta *= -1;
                    this->wait_timer.set_interval(FontSlider::WaitTimeout);
                }

                else {
                    this->wait_timer.set_interval(FontSlider::MoveTimeout);
                }
            }
        }
    };

    static struct {
        using BIOSStringOffset = const char*const (BIOS::Version::*);

        const BIOSStringOffset bios_str_offset;
        const char*const       display_str;
        FontSlider             font_slider;
    } BIOSStringInfo[] = {
        {.bios_str_offset = &BIOS::Version::kernel_maker, .display_str = "Kernel-Maker"},
        {.bios_str_offset = &BIOS::Version::version_str,  .display_str = "Version"},
        {.bios_str_offset = &BIOS::Version::gui_version,  .display_str = "GUI-Version"},
        {.bios_str_offset = &BIOS::Version::copyright,    .display_str = "Copyright"},
    };

    static GPU::TILE::Linked border_tiles[2] = {
        Make::TILE(Make::AreaI16(0,                                  0, TextOffset.x, GPU::Display::Height - 32), GPU::Color24::Black()).linked(),
        Make::TILE(Make::AreaI16(GPU::Display::Width - TextOffset.x, 0, TextOffset.x, GPU::Display::Height - 32), GPU::Color24::Black()).linked()
    };

    static NameColorPair bios_name;
    static FontSlider    bios_name_slider;

    static NameColorPair get_bios_name() {
        switch(BIOS::version.type) {
            case BIOS::Version::Devboard:
                return {"DevBoard", GPU::Color24::Green()};
            case BIOS::Version::PS1:
                return {"PS1", GPU::Color24::Red()};
            case BIOS::Version::PS2:
                return {"PS2", GPU::Color24::Blue()};
            case BIOS::Version::PS3:
                return {"PS3", GPU::Color24::Yellow()};
            case BIOS::Version::PSCompatible:
                return {"Unkown PS compatible BIOS", GPU::Color24::Grey()};
            case BIOS::Version::No$psx:
                return {"NO$PSX", GPU::Color24::Purple()};
            case BIOS::Version::XEBRA:
                return {"XEBRA", GPU::Color24::Turquoise()};
            default:
                return {"Unkown", GPU::Color24::White()};
        }
    }

    static void setup() {
        bios_name = get_bios_name();
        Shared::back_menu.reset();
        for(auto& bios_str_info : BIOSStringInfo) {
            bios_str_info.font_slider = FontSlider::create_for(FontWriter::BIOSFont::Info, BIOS::version.*(bios_str_info.bios_str_offset));
        }
        bios_name_slider = FontSlider::create_for(FontWriter::BIOSFont::Info, bios_name.first);
        border_tiles[0].concat(border_tiles[1]);
    }

    static bool update_or_exit() {
        static const auto move_cursor = [](JabyEngine::Cursor& cursor, int16_t dx, int16_t old_x) -> JabyEngine::Cursor& {
            cursor.pos.x = (old_x - dx);
            return cursor;
        };

        Periphery::query_controller();
        if(Shared::back_menu.update(Make::PositionI16(0, GPU::Display::Height - 32))) {
            return true;
        }

        auto cursor = FontWriter::update(TextOffset);
        FontWriter::bios_font_writer.write(cursor, "BIOS INFORMATION\n----------------\nDate (day/month/year):\n%i/%i/%i\n", BIOS::version.date.day, BIOS::version.date.month, BIOS::version.date.year);
        
        const auto old_pos_x = cursor.pos.x;
        for(auto& bios_str_info : BIOSStringInfo) {
            bios_str_info.font_slider.advance();

            FontWriter::bios_font_writer.write(move_cursor(cursor, 0, old_pos_x), "%s:\n", bios_str_info.display_str);
            FontWriter::bios_font_writer.write(move_cursor(cursor, bios_str_info.font_slider.count, old_pos_x), "%s\n", BIOS::version.*(bios_str_info.bios_str_offset));
        }
        FontWriter::bios_font_writer.write(move_cursor(cursor, 0, old_pos_x), "BIOS Type:\n");
        FontWriter::bios_font_writer.write(move_cursor(cursor, bios_name_slider.count, old_pos_x), "%s\n", bios_name.second, bios_name.first);
        FontWriter::bios_font_writer.write(move_cursor(cursor, 0, old_pos_x), "----------------\n");

        return false;
    }
    
    static void render() {
        Shared::back_menu.render();
        FontWriter::bios_font_writer.render();
        GPU::render(border_tiles[0]);
    }

    void main() {
        setup();
        while(true) {
            if(update_or_exit()) {
                break;
            }
            GPU::swap_buffers_vsync(1);
            render();
        }
    }
}