/*
    * desktop.hpp
    * MontaukOS desktop state and compositor declarations
    * Copyright (c) 2026 Daniel Hammer
*/

#pragma once
#include "gui/gui.hpp"
#include "gui/framebuffer.hpp"
#include "gui/svg.hpp"
#include "gui/window.hpp"
#include "gui/widgets.hpp"
#include "gui/terminal.hpp"
#include <Api/Syscall.hpp>

namespace gui {

static constexpr int MAX_WINDOWS = 8;
static constexpr int PANEL_HEIGHT = 32;
static constexpr int MAX_EXTERNAL_APPS = 32;

// External app discovered from 0:/apps/ manifest
struct ExternalApp {
    char name[48];
    char binary_path[128];
    char category[24];
    SvgIcon icon;
    bool menu_visible;
    bool launch_with_home;
};

struct DesktopSettings {
    // Background
    bool bg_gradient;       // true = gradient, false = solid
    bool bg_image;          // true = JPEG wallpaper
    Color bg_solid;         // solid background color
    Color bg_grad_top;      // gradient top color
    Color bg_grad_bottom;   // gradient bottom color

    // Wallpaper (valid when bg_image == true)
    char bg_image_path[128];   // VFS path to current wallpaper
    uint32_t* bg_wallpaper;    // scaled ARGB pixel buffer (screen-sized)
    int bg_wallpaper_w;        // scaled width
    int bg_wallpaper_h;        // scaled height

    // Panel
    Color panel_color;      // panel background color

    // Accent
    Color accent_color;     // buttons, highlights, active indicators

    // Display
    bool show_shadows;      // window shadows on/off
    bool clock_24h;         // 24-hour clock format
    int ui_scale;           // 0=Small, 1=Default, 2=Large
    int tz_offset_minutes;  // timezone offset from UTC in minutes
};

struct DesktopState {
    Framebuffer fb;
    Window windows[MAX_WINDOWS];
    int window_count;
    int focused_window;

    // Current user context
    char current_user[32];
    char home_dir[128];         // "0:/users/<username>"
    char user_config_dir[128];  // "0:/users/<username>/config"
    bool is_admin;

    Montauk::MouseState mouse;
    uint8_t prev_buttons;

    bool app_menu_open;

    SvgIcon icon_terminal;
    SvgIcon icon_filemanager;
    SvgIcon icon_sysinfo;
    SvgIcon icon_appmenu;
    SvgIcon icon_folder;
    SvgIcon icon_file;
    SvgIcon icon_network;
    SvgIcon icon_calculator;
    SvgIcon icon_go_up;
    SvgIcon icon_go_back;
    SvgIcon icon_go_forward;
    SvgIcon icon_home;
    SvgIcon icon_exec;

    SvgIcon icon_folder_lg;
    SvgIcon icon_file_lg;
    SvgIcon icon_exec_lg;
    SvgIcon icon_drive;
    SvgIcon icon_drive_lg;
    SvgIcon icon_delete;

    SvgIcon icon_settings;
    SvgIcon icon_reboot;
    SvgIcon icon_shutdown;
    SvgIcon icon_sleep;
    SvgIcon icon_logout;

    SvgIcon icon_volume;

    // External apps discovered from 0:/apps/ manifests
    ExternalApp external_apps[MAX_EXTERNAL_APPS];
    int external_app_count;

    bool ctx_menu_open;
    int ctx_menu_x, ctx_menu_y;

    bool net_popup_open;
    Montauk::NetCfg cached_net_cfg;
    uint64_t net_cfg_last_poll;
    Rect net_icon_rect;

    bool vol_popup_open;
    Rect vol_icon_rect;
    int vol_level;            // 0-100
    bool vol_muted;
    int vol_pre_mute;         // volume before mute
    bool vol_dragging;        // slider drag in progress
    uint64_t vol_last_poll;

    int screen_w, screen_h;

    // IDs of external windows we've sent a close event to but that haven't
    // been destroyed yet by their owning process.  Prevents the poll loop
    // from re-creating them at the default position (visible flicker).
    static constexpr int MAX_CLOSING = 8;
    int closing_ext_ids[MAX_CLOSING];
    int closing_ext_count;

    DesktopSettings settings;

    // Lock screen state
    bool screen_locked;
    char lock_password[64];
    int lock_password_len;
    char lock_error[128];
    bool lock_show_error;
    char lock_display_name[64];
    SvgIcon icon_lock;
};

// Forward declarations - implemented in main.cpp
void desktop_init(DesktopState* ds);
void desktop_run(DesktopState* ds);
void desktop_compose(DesktopState* ds);
int  desktop_create_window(DesktopState* ds, const char* title, int x, int y, int w, int h);
void desktop_close_window(DesktopState* ds, int idx);
void desktop_raise_window(DesktopState* ds, int idx);
void desktop_draw_panel(DesktopState* ds);
void desktop_draw_window(DesktopState* ds, int idx);
void desktop_handle_mouse(DesktopState* ds);
void desktop_handle_keyboard(DesktopState* ds, const Montauk::KeyEvent& key);

} // namespace gui
