#include "pxl8_gui.h"

#include <stdlib.h>
#include <string.h>

#include "pxl8_gfx.h"

pxl8_gui_state* pxl8_gui_state_create(void) {
    pxl8_gui_state* state = (pxl8_gui_state*)malloc(sizeof(pxl8_gui_state));
    if (!state) return NULL;

    memset(state, 0, sizeof(pxl8_gui_state));
    return state;
}

void pxl8_gui_state_destroy(pxl8_gui_state* state) {
    if (!state) return;
    free(state);
}

void pxl8_gui_begin_frame(pxl8_gui_state* state, pxl8_gfx* gfx) {
    if (!state) return;
    state->hot_id = 0;
    if (gfx) pxl8_gfx_push_target(gfx);
}

void pxl8_gui_end_frame(pxl8_gui_state* state, pxl8_gfx* gfx) {
    if (!state) return;

    if (!state->cursor_down) {
        state->active_id = 0;
    }
    state->cursor_clicked = false;
    if (gfx) pxl8_gfx_pop_target(gfx);
}

void pxl8_gui_cursor_move(pxl8_gui_state* state, i32 x, i32 y) {
    if (!state) return;
    state->cursor_x = x;
    state->cursor_y = y;
}

void pxl8_gui_cursor_down(pxl8_gui_state* state) {
    if (!state) return;
    state->cursor_down = true;
}

void pxl8_gui_cursor_up(pxl8_gui_state* state) {
    if (!state) return;
    state->cursor_down = false;
    state->cursor_clicked = true;
}

static bool is_cursor_over(const pxl8_gui_state* state, i32 x, i32 y, i32 w, i32 h) {
    return state->cursor_x >= x && state->cursor_x < (x + w) &&
           state->cursor_y >= y && state->cursor_y < (y + h);
}

bool pxl8_gui_button(pxl8_gui_state* state, pxl8_gfx* gfx, u32 id, i32 x, i32 y, i32 w, i32 h, const char* label) {
    if (!state || !gfx || !label) return false;

    bool cursor_over = is_cursor_over(state, x, y, w, h);
    bool is_hot = (state->hot_id == id);
    bool is_active = (state->active_id == id);

    if (cursor_over) {
        state->hot_id = id;
    }

    if (cursor_over && state->cursor_down && state->active_id == 0) {
        state->active_id = id;
    }

    bool clicked = is_active && state->cursor_clicked && cursor_over;
    if (clicked) {
        state->active_id = 0;
    }

    u8 bg_color;
    u8 border_color;
    i32 offset_x = 0;
    i32 offset_y = 0;

    if (is_active) {
        bg_color = pxl8_gfx_ui_color(gfx, PXL8_UI_BG3);
        border_color = pxl8_gfx_ui_color(gfx, PXL8_UI_BG2);
        offset_x = 1;
        offset_y = 1;
    } else if (is_hot || cursor_over) {
        bg_color = pxl8_gfx_ui_color(gfx, PXL8_UI_BG3);
        border_color = pxl8_gfx_ui_color(gfx, PXL8_UI_FG0);
    } else {
        bg_color = pxl8_gfx_ui_color(gfx, PXL8_UI_BG2);
        border_color = pxl8_gfx_ui_color(gfx, PXL8_UI_BG3);
    }

    pxl8_2d_rect_fill(gfx, x, y, w, h, bg_color);
    pxl8_2d_rect(gfx, x, y, w, h, border_color);

    i32 text_len = (i32)strlen(label);
    i32 text_x = x + (w / 2) - ((text_len * 8) / 2) + offset_x;
    i32 text_y = y + (h / 2) - 5 + offset_y;
    pxl8_2d_text(gfx, label, text_x, text_y, pxl8_gfx_ui_color(gfx, PXL8_UI_FG1));

    return clicked;
}

void pxl8_gui_window(pxl8_gfx* gfx, i32 x, i32 y, i32 w, i32 h, const char* title) {
    if (!gfx || !title) return;

    u8 title_bg = pxl8_gfx_ui_color(gfx, PXL8_UI_BG1);
    u8 body_bg = pxl8_gfx_ui_color(gfx, PXL8_UI_BG2);
    u8 border = pxl8_gfx_ui_color(gfx, PXL8_UI_BG3);
    u8 title_fg = pxl8_gfx_ui_color(gfx, PXL8_UI_FG0);

    pxl8_2d_rect_fill(gfx, x, y, w, 28, title_bg);
    pxl8_2d_rect_fill(gfx, x, y + 28, w, h - 28, body_bg);
    pxl8_2d_rect(gfx, x, y, w, h, border);
    pxl8_2d_rect_fill(gfx, x, y + 28, w, 1, border);

    i32 title_x = x + 10;
    i32 title_y = y + (28 / 2) - 5;
    pxl8_2d_text(gfx, title, title_x, title_y, title_fg);
}

void pxl8_gui_label(pxl8_gfx* gfx, i32 x, i32 y, const char* text, u8 color) {
    if (!gfx || !text) return;
    pxl8_2d_text(gfx, text, x, y, color);
}

bool pxl8_gui_is_hovering(const pxl8_gui_state* state) {
    if (!state) return false;
    return state->hot_id != 0;
}

void pxl8_gui_get_cursor_pos(const pxl8_gui_state* state, i32* x, i32* y) {
    if (!state) return;
    if (x) *x = state->cursor_x;
    if (y) *y = state->cursor_y;
}
