const std = @import("std");
const r = @import("raylib.zig");
const assets = @import("assets.zig");

const Menu = @This();

const MenuOptions = enum(u32) {
    Start,
    Options,
    Credits,
    Quit,
};

pub fn init() !Menu {
    return Menu{
        .render = r.LoadRenderTexture(1920, 1080),
        .menutex = try assets.getTexture("menu.qoi"),
        .messages = &[_][]const u8{
            "start",
            "options",
            "credits",
            "quit",
        },
        .font = try assets.getFont("Shantell_Sans-Informal_Regular.otf", 64),
        .boldfont = try assets.getFont("Shantell_Sans-Informal_Bold.otf", 64),
    };
}

render: r.RenderTexture2D,
menutex: r.Texture2D,
current: MenuOptions = .Start,
messages: []const []const u8,
font: r.Font,
boldfont: r.Font,

pub fn deinit(self: *@This()) void {
    r.UnloadTexture(self.menutex);
    r.UnloadFont(self.font);
    r.UnloadFont(self.boldfont);
    r.UnloadRenderTexture(self.render);
}

pub fn update(self: *@This()) bool {
    const max = std.meta.fields(MenuOptions).len - 1;
    if (r.IsKeyPressed(r.KEY_UP)) {
        if (@intFromEnum(self.current) > 0) {
            self.current = @enumFromInt(@intFromEnum(self.current) - 1);
        } else {
            self.current = @enumFromInt(max);
        }
    } else if (r.IsKeyPressed(r.KEY_DOWN)) {
        if (@intFromEnum(self.current) < max) {
            self.current = @enumFromInt(@intFromEnum(self.current) + 1);
        } else {
            self.current = @enumFromInt(0);
        }
    }
    var clicked = false;
    var i: usize = 0;
    const fontsize: f32 = @floatFromInt(self.font.baseSize);
    const sw: f32 = @floatFromInt(self.render.texture.width);
    const sh: f32 = @floatFromInt(self.render.texture.height);
    while (i < self.messages.len) : (i += 1) {
        const font = if (i == @intFromEnum(self.current)) self.boldfont else self.font;
        const ts = r.MeasureTextEx(font, self.messages[i].ptr, fontsize, 0);
        const x = sw / 2 - ts.x / 2;
        const y = sh * 0.4 + fontsize * 1.25 * @as(f32, @floatFromInt(i));
        const mpos = r.GetMousePosition();
        if (x <= mpos.x and mpos.x <= x + ts.x and
            y <= mpos.y and mpos.y <= y + ts.y)
        {
            self.current = @enumFromInt(i);
            if (r.IsMouseButtonPressed(0)) {
                clicked = true;
            }
        }
    }
    return r.IsKeyPressed(r.KEY_ENTER) or clicked;
}

pub fn draw(self: *@This()) void {
    r.BeginTextureMode(self.render);
    defer r.EndTextureMode();

    const fontsize: f32 = @floatFromInt(self.font.baseSize);
    const sw: f32 = @floatFromInt(self.render.texture.width);
    const sh: f32 = @floatFromInt(self.render.texture.height);
    const tw: f32 = @floatFromInt(self.menutex.width);
    //const th: f32 = @floatFromInt(self.menutex.height);
    r.DrawTextureEx(self.menutex, .{ .x = 0, .y = 0 }, 0, sw / tw, r.WHITE);

    var i: usize = 0;
    while (i < self.messages.len) : (i += 1) {
        const font = if (i == @intFromEnum(self.current)) self.boldfont else self.font;
        const ts = r.MeasureTextEx(font, self.messages[i].ptr, fontsize, 0);
        const x = sw / 2 - ts.x / 2;
        const y = sh * 0.4 + fontsize * 1.25 * @as(f32, @floatFromInt(i));
        r.DrawTextEx(
            font,
            self.messages[i].ptr,
            r.Vector2{ .x = x, .y = y },
            fontsize,
            0,
            if (i == @intFromEnum(self.current)) r.Color{ .r = 0x80, .g = 0, .b = 0xFF, .a = 0xFF } else r.WHITE,
        );
    }
}
