/*
    * desktop_builtin.cpp
    * Shared builtin applet metadata and dispatch helpers
    * Copyright (c) 2026 Daniel Hammer
*/

#include "desktop_internal.hpp"
#include <montauk/user.h>

static const DesktopBuiltinEntry builtin_entries[] = {
    { DESKTOP_BUILTIN_FILES, "Files", 0, false, nullptr, "0:/icons/system-file-manager.svg" },
    { DESKTOP_BUILTIN_DESKTOP_SETTINGS, "Desktop Settings", -1, false, "desktop-settings",
      "0:/icons/preferences-desktop-apps.svg" },
    { DESKTOP_BUILTIN_SYSTEM_CONFIGURATION, "System Configuration", -1, true, nullptr,
      "0:/icons/preferences-system.svg" },
    { DESKTOP_BUILTIN_USER_MANAGEMENT, "User Management", -1, false, "user-management",
      "0:/icons/user-home.svg" },
    { DESKTOP_BUILTIN_LOCK_SCREEN, "Lock Screen", -1, true, nullptr, nullptr },
    { DESKTOP_BUILTIN_LOG_OUT, "Log Out", -1, true, nullptr, nullptr },
    { DESKTOP_BUILTIN_SLEEP, "Sleep", -1, true, nullptr, nullptr },
    { DESKTOP_BUILTIN_REBOOT, "Reboot", -1, true, nullptr, nullptr },
    { DESKTOP_BUILTIN_SHUTDOWN, "Shutdown", -1, true, nullptr, nullptr },
};

static constexpr int builtin_entry_count = sizeof(builtin_entries) / sizeof(builtin_entries[0]);

const DesktopBuiltinEntry* desktop_builtin_registry(int* count) {
    if (count) *count = builtin_entry_count;
    return builtin_entries;
}

const DesktopBuiltinEntry* desktop_builtin_entry(DesktopBuiltinId id) {
    for (int i = 0; i < builtin_entry_count; i++) {
        if (builtin_entries[i].id == id) return &builtin_entries[i];
    }
    return nullptr;
}

SvgIcon* desktop_builtin_menu_icon(DesktopState* ds, DesktopBuiltinId id) {
    if (!ds) return nullptr;

    switch (id) {
    case DESKTOP_BUILTIN_FILES: return &ds->icon_filemanager;
    case DESKTOP_BUILTIN_DESKTOP_SETTINGS: return &ds->icon_settings;
    case DESKTOP_BUILTIN_SYSTEM_CONFIGURATION:
        ensure_filemanager_icons_loaded(ds);
        return &ds->icon_system_configuration_menu;
    case DESKTOP_BUILTIN_LOCK_SCREEN: return &ds->icon_lock;
    case DESKTOP_BUILTIN_LOG_OUT: return &ds->icon_logout;
    case DESKTOP_BUILTIN_SLEEP: return &ds->icon_sleep;
    case DESKTOP_BUILTIN_REBOOT: return &ds->icon_reboot;
    case DESKTOP_BUILTIN_SHUTDOWN: return &ds->icon_shutdown;
    default: return nullptr;
    }
}

void desktop_lock_screen(DesktopState* ds) {
    if (!ds) return;

    ds->screen_locked = true;
    ds->lock_password[0] = '\0';
    ds->lock_password_len = 0;
    ds->lock_error[0] = '\0';
    ds->lock_show_error = false;
    ds->app_menu_open = false;
    desktop_close_launcher(ds);
    ds->ctx_menu_open = false;
    ds->net_popup_open = false;
    ds->vol_popup_open = false;

    // Cache display name for lock screen rendering
    montauk::strncpy(ds->lock_display_name, ds->current_user, sizeof(ds->lock_display_name));
    montauk::user::UserInfo users[16];
    int count = montauk::user::load_users(users, 16);
    for (int i = 0; i < count; i++) {
        if (montauk::streq(users[i].username, ds->current_user)) {
            if (users[i].display_name[0]) {
                montauk::strncpy(ds->lock_display_name, users[i].display_name,
                                 sizeof(ds->lock_display_name));
            }
            break;
        }
    }
}

void desktop_launch_builtin(DesktopState* ds, DesktopBuiltinId id) {
    if (!ds) return;

    switch (id) {
    case DESKTOP_BUILTIN_FILES: open_filemanager(ds); break;
    case DESKTOP_BUILTIN_DESKTOP_SETTINGS: open_settings(ds); break;
    case DESKTOP_BUILTIN_SYSTEM_CONFIGURATION: open_system_configuration(ds); break;
    case DESKTOP_BUILTIN_USER_MANAGEMENT: open_user_manager(ds); break;
    case DESKTOP_BUILTIN_REBOOT: open_reboot_dialog(ds); break;
    case DESKTOP_BUILTIN_SHUTDOWN: open_shutdown_dialog(ds); break;
    case DESKTOP_BUILTIN_LOG_OUT: montauk::exit(0); break;
    case DESKTOP_BUILTIN_LOCK_SCREEN: desktop_lock_screen(ds); break;
    case DESKTOP_BUILTIN_SLEEP: open_sleep_dialog(ds); break;
    default: break;
    }
}
