#include <print>

#include <raylib.h>
#include <freetype/freetype.h>
#include <hb.h>

#include "font.h"
#include "misc.h"

#include <stb_rect_pack.h>
#define KB_TEXT_SHAPE_IMPLEMENTATION
#include <kb_text_shape.h>
#include <tracy/Tracy.hpp>

float dt          = 1.0 / 200.0;
float t           = 0;
float gt          = 0;
float accumulator = 0;
float k           = 1;
int32_t frame     = 0;

int main() {
    ZoneScoped

    std::println("Hello, Sailor!");

    if (FT_Init_FreeType(&ft_lib)) {

    }

    const int32_t screenwidth = 1280;
    const int32_t screenheight = 720;

    InitWindow(screenwidth, screenheight, "raylib");

    MyFont vollkorn("assets/fonts/Vollkorn/static/Vollkorn-Regular.ttf", 42);
    MyFont arabic("assets/fonts/Amiri/Amiri-Regular.ttf", 28);
    shader = LoadShader(nullptr, "assets/text.frag");

    float old_time = GetTime();

    while (!WindowShouldClose()) {
        float new_time = GetTime();
        // const new_time : f32 = @floatCast(rl.getTime());
        float frame_time = new_time - old_time;
        old_time = new_time;

        if (frame_time > 0.25)
            frame_time = 0.25;

        t += frame_time;
        accumulator += frame_time * k;

        PollInputEvents();

        while (accumulator > dt * k) {
            // update(dt * cast(float) k);
            accumulator -= dt * k;
            gt += dt * k;
        }

        auto [x, y] = GetWindowScaleDPI();
        BeginDrawing();
        ClearBackground(SKYBLUE);
        //DrawText("Hello, Sailor!", 150, 150, 20, RAYWHITE);

        // vollkorn.render_text("The night is long and cold outside", {0 ,0}, WHITE);

        arabic.render_text("الليل طويل وبارد في الخارج", Vector2{ .x = 0, .y = 100}, WHITE);

        misc::drawFPS(0, 0, frame_time);

        EndDrawing();

        {
            ZoneScopedN("SwapScreenBuffer")
            SwapScreenBuffer();
        }

        frame++;

        FrameMark;
    }

    vollkorn.deinit();

    CloseWindow();
    FT_Done_FreeType(ft_lib);

    return 0;
}
