/*
    * users.cpp
    * User management view for the embedded desktop user management applet
    * Copyright (c) 2026 Daniel Hammer
*/

#include "usermanager_internal.hpp"

namespace usermanager_app {

static mtk::WidgetState button_state(const UserManagerState* st,
                                     const Rect& rect,
                                     bool enabled,
                                     bool active = false) {
    return mtk::widget_state(active, rect.contains(st->mouse_x, st->mouse_y), enabled);
}

void usermanager_reload(UserManagerState* st) {
    st->user_count = montauk::user::load_users(st->users, 16);
    st->users_loaded = true;
}

bool usermanager_status_visible(UserManagerState* st) {
    return st->status_msg[0] &&
           (montauk::get_milliseconds() - st->status_time < 4000);
}

void usermanager_set_status(UserManagerState* st, const char* msg) {
    montauk::strncpy(st->status_msg, msg ? msg : "", (int)sizeof(st->status_msg));
    st->status_time = montauk::get_milliseconds();
}

int user_row_height() {
    return system_font_height() * 2 + 12;
}

void usermanager_update_mouse(UserManagerState* st, int mx, int my) {
    if (!st) return;
    st->mouse_x = mx;
    st->mouse_y = my;
}

void usermanager_draw(Canvas& c, UserManagerState* st) {
    if (!st->users_loaded) usermanager_reload(st);

    mtk::Theme theme = usermanager_theme(st);
    int x = 16;
    int y = 20;
    int sfh = system_font_height();
    int content_w = c.w - 2 * x;
    int row_h = user_row_height();

    c.fill(theme.window_bg);

    if (!st->desktop->is_admin) {
        c.text(x, y, "Admin access required to manage users.", theme.text_muted);
        return;
    }

    for (int i = 0; i < st->user_count; i++) {
        bool selected = (i == st->selected_user);
        bool is_current = montauk::streq(st->users[i].username, st->desktop->current_user);
        Rect row = {x, y, content_w, row_h};
        mtk::draw_list_row(c, row, selected, (i % 2) == 0, theme);

        Color text_color = selected ? theme.text_inverse : theme.text;
        Color sub_color = selected ? Color::from_rgb(0xE3, 0xEA, 0xFF) : theme.text_muted;

        int text_y = y + 4;
        c.text(x + 12, text_y, st->users[i].display_name, text_color);

        char sub[48];
        if (is_current) {
            snprintf(sub, sizeof(sub), "@%s (you)", st->users[i].username);
        } else {
            snprintf(sub, sizeof(sub), "@%s", st->users[i].username);
        }
        c.text(x + 12, text_y + sfh + 2, sub, sub_color);

        const char* role = st->users[i].role;
        int badge_w = mtk::badge_width(role, theme);
        int badge_h = mtk::badge_height();
        int badge_x = c.w - x - badge_w - 8;
        int badge_y = y + (row_h - badge_h) / 2;
        if (selected) {
            mtk::draw_badge(c, badge_x, badge_y, role, colors::WHITE, theme.accent, theme);
        } else if (montauk::streq(role, "admin")) {
            mtk::draw_badge(c, badge_x, badge_y, role,
                            theme.badge_admin_bg, theme.badge_admin_fg, theme);
        } else {
            mtk::draw_badge(c, badge_x, badge_y, role,
                            theme.badge_user_bg, theme.badge_user_fg, theme);
        }

        y += row_h + 4;
    }

    y += 8;
    mtk::draw_separator(c, x, y, content_w, theme);
    y += 16;

    if (usermanager_status_visible(st)) {
        c.text(x, y, st->status_msg, theme.accent);
        y += sfh + 8;
    }

    Rect add_btn = {x, y, 90, USER_BTN_H};
    Rect change_btn = {x + 98, y, USER_BTN_W, USER_BTN_H};
    Rect delete_btn = {x + 98 + USER_BTN_W + 8, y, 70, USER_BTN_H};
    bool has_selection = st->selected_user >= 0;

    mtk::draw_button(c, add_btn, "Add User", mtk::BUTTON_PRIMARY,
                     button_state(st, add_btn, true), theme);
    mtk::draw_button(c, change_btn, "Change Pwd", mtk::BUTTON_SECONDARY,
                     button_state(st, change_btn, has_selection), theme);
    mtk::draw_button(c, delete_btn, "Delete", mtk::BUTTON_DANGER,
                     button_state(st, delete_btn, has_selection), theme);
}

bool usermanager_handle_click(Window* win, UserManagerState* st, int mx, int my) {
    if (!st->desktop->is_admin) return false;
    if (!st->users_loaded) usermanager_reload(st);

    int x = 16;
    int sfh = system_font_height();
    int y = 20;
    int content_w = win->content_w - 2 * x;
    int row_h = user_row_height();

    for (int i = 0; i < st->user_count; i++) {
        Rect row = {x, y, content_w, row_h};
        if (row.contains(mx, my)) {
            st->selected_user = (st->selected_user == i) ? -1 : i;
            return true;
        }
        y += row_h + 4;
    }

    y += 8 + 16;
    if (usermanager_status_visible(st)) {
        y += sfh + 8;
    }

    Rect add_btn = {x, y, 90, USER_BTN_H};
    Rect change_btn = {x + 98, y, USER_BTN_W, USER_BTN_H};
    Rect delete_btn = {x + 98 + USER_BTN_W + 8, y, 70, USER_BTN_H};
    if (add_btn.contains(mx, my)) {
        open_add_user_dialog(st);
        return true;
    }
    if (change_btn.contains(mx, my)) {
        if (st->selected_user >= 0) open_change_pwd_dialog(st);
        return true;
    }
    if (delete_btn.contains(mx, my)) {
        if (st->selected_user >= 0) open_delete_user_dialog(st);
        return true;
    }

    return false;
}

} // namespace usermanager_app
