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

#include <print>

#include <external/fix_win32_compatibility.h>
#include <freetype/freetype.h>
#include <hb.h>
#include <enet/enet.h>
#include <raylib.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;

Vector2 DPI = {};

constexpr int32_t screenwidth = 1280;
constexpr int32_t screenheight = 720;

int main() {
    ZoneScoped

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

    if (FT_Init_FreeType(&ft_lib)) {

    }

    if (enet_initialize() != 0) {
        fprintf (stderr, "An error occurred while initializing ENet.\n");
        return EXIT_FAILURE;
    }

    SetConfigFlags(FLAG_WINDOW_HIGHDPI);
    InitWindow(screenwidth, screenheight, "raylib");
    DPI = GetWindowScaleDPI();

    MyFont vollkorn("assets/fonts/Vollkorn/static/Vollkorn-Regular.ttf", 68);
    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();
        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("SwapScreenBufferZone")
            SwapScreenBuffer();
        }

        frame++;

        FrameMark;
    }

    vollkorn.deinit();

    CloseWindow();
    FT_Done_FreeType(ft_lib);
    enet_deinitialize();

    return 0;
}
