#include "include/font_writer.hpp"
#include <FontWriter/fonts.hpp>
#include <PSX/File/Processor/cd_file_processor.hpp> //< only for __heap_start D:
#include <PSX/Timer/frame_timer.hpp>

namespace FontWriter {
    using namespace JabyEngine;

    static constexpr auto LibraryFontTIM = SimpleTIM::create(320, 0, 320, DefaultFont::Info.texture_size.height);

    static FontPrimitive   font_buffer[2*256];
    Wiggle                 wiggle           = {Make::PositionI8(0, 0), Make::PositionI8(1, -2), Make::PositionI8(0, -4), Make::PositionI8(-1, -2), Make::PositionI8(0, 0), Make::PositionI8(1, 2), Make::PositionI8(0, 4), Make::PositionI8(-1, 2)};
    JabyEngine::FontWriter new_font_writer  = JabyEngine::FontWriter::empty();
    JabyEngine::FontWriter bios_font_writer = JabyEngine::FontWriter::empty();
    static SimpleTimer<uint8_t>   timer;
    uint8_t                wiggle_count     = 0;

    void setup() {
        JabyEngine::DefaultFont::load(&__heap_start, LibraryFontTIM);
        JabyEngine::GlobalFontPrimitivePool::setup(font_buffer);

        new_font_writer.setup(LibraryFontTIM, JabyEngine::DefaultFont::Info);
        bios_font_writer.setup(JabyEngine::BIOSFont::TIM, JabyEngine::BIOSFont::Info);
        timer.reset();
    }

    Cursor update(const GPU::PositionI16& start) {
        if(timer.is_expired_for(50_ms)) {
            timer.reset();
            wiggle_count++;
        }

        return Cursor::create(start, wiggle_count);
    }
}