const zgl = @import("zgl");
const zgui = @import("zgui");
const zm = @import("zmath");

const glfw = @import("zglfw");
const Window = glfw.Window;

const glfw_helper = @import("root.zig");
const AppState = glfw_helper.AppState;

pub fn cursorPosCallback(window: *Window, xpos: f64, ypos: f64) callconv(.c) void {
    const app_state = window.getUserPointer(AppState).?;

    if (!app_state.render_gui) {
        app_state.camera.updateAngle(@floatCast(xpos), @floatCast(ypos));
        glfw.setCursorPos(window, @as(f32, @floatFromInt(app_state.width)) / 2, @as(f32, @floatFromInt(app_state.height)) / 2);
    }
}

pub fn keyCallback(window: *Window, key: glfw.Key, _: c_int, action: glfw.Action, _: glfw.Mods) callconv(.c) void {
    const app_state = window.getUserPointer(AppState).?;

    if (action == .press) {
        switch (key) {
            .tab => {
                app_state.render_gui = !app_state.render_gui;
                if (app_state.render_gui) {
                    window.setInputMode(.cursor, .normal) catch {};
                } else {
                    window.setInputMode(.cursor, .disabled) catch {};
                    app_state.camera.updateAngle(@as(f32, @floatFromInt(app_state.width)) / 2, @as(f32, @floatFromInt(app_state.height)) / 2);
                }
                glfw.setCursorPos(window, @as(f32, @floatFromInt(app_state.width)) / 2, @as(f32, @floatFromInt(app_state.height)) / 2);
            },
            .escape => window.setShouldClose(true),
            else => {},
        }
    }

    if (!app_state.render_gui) {
        app_state.camera.updateControls(key, action);
    }
}

pub fn messageCallback(source: zgl.DebugSource, msg_type: zgl.DebugMessageType, id: usize, severity: zgl.DebugSeverity, message: []const u8) void {
    const print = @import("std").debug.print;

    print("{t}, {t}, {t}, {d}: {s}\n", .{ source, msg_type, severity, id, message });
}
