const std = @import("std");
const r = @import("raylib.zig");
const Walrus = @import("walrus.zig");
const Map = @import("map.zig");
const Menu = @import("menu.zig");

const Screen = enum {
    MainMenu,
    Game,
    PauseMenu,
};

pub fn main() !void {
    r.SetTraceLogLevel(r.LOG_WARNING);
    r.SetConfigFlags(r.FLAG_WINDOW_RESIZABLE | r.FLAG_FULLSCREEN_MODE);
    r.InitWindow(1920, 1080, "Where's the wlarus?");
    r.SetTargetFPS(60);
    r.SetExitKey(0);
    defer r.CloseWindow();

    var screen = Screen.MainMenu;

    var menu = try Menu.init();
    defer menu.deinit();

    var map = try Map.init("write.tmx");
    defer map.deinit();

    var quit = false;

    var rt = r.LoadRenderTexture(1920, 1080);
    defer r.UnloadRenderTexture(rt);

    while (!r.WindowShouldClose() and !quit) {
        {
            switch (screen) {
                .MainMenu => {
                    if (menu.update()) {
                        switch (menu.current) {
                            .Start => screen = .Game,
                            .Options => {},
                            .Credits => {},
                            .Quit => quit = true,
                        }
                    }
                    if (r.IsKeyPressed(r.KEY_ESCAPE)) {
                        quit = true;
                    }
                    r.ClearBackground(r.BLACK);
                    menu.draw();
                    rt = menu.render;
                },
                .Game => {
                    map.update();
                    if (r.IsKeyPressed(r.KEY_ESCAPE)) {
                        screen = .MainMenu;
                    }
                    r.ClearBackground(r.BLUE);
                    map.draw();
                    rt = map.render;
                },
                .PauseMenu => {},
            }
        }
        {
            r.BeginDrawing();
            defer r.EndDrawing();

            const w: f32 = @floatFromInt(r.GetScreenWidth());
            const h: f32 = @floatFromInt(r.GetScreenHeight());

            r.DrawTexturePro(
                rt.texture,
                r.Rectangle{
                    .x = 0,
                    .y = 0,
                    .width = @floatFromInt(rt.texture.width),
                    .height = @floatFromInt(-rt.texture.height),
                },
                r.Rectangle{
                    .x = 0,
                    .y = 0,
                    .width = w,
                    .height = h,
                },
                r.Vector2{ .x = 0, .y = 0 },
                0,
                r.WHITE,
            );
        }
    }
}
