/*
    * settings_internal.hpp
    * Shared declarations for the embedded desktop settings app
    * Copyright (c) 2026 Daniel Hammer
*/

#pragma once

#include "../apps_common.hpp"
#include "../../wallpaper.hpp"
#include <gui/mtk.hpp>
#include <montauk/config.h>

void desktop_mark_background_dirty(DesktopState* ds);

namespace settings_app {

struct SettingsState {
    DesktopState* desktop;
    int active_tab;        // 0=Appearance, 1=Display, 2=About
    Montauk::SysInfo sys_info;
    uint64_t uptime_ms;
    WallpaperFileList wp_files;
    bool wp_scanned;

    char status_msg[128];
    uint64_t status_time;
};

inline constexpr int SWATCH_COUNT = 8;
inline constexpr int SWATCH_SIZE = 24;
inline constexpr int SWATCH_GAP = 6;

// Background colors (light tones)
inline const Color bg_palette[SWATCH_COUNT] = {
    Color::from_rgb(0xD0, 0xD8, 0xE8),  // light blue-gray (default)
    Color::from_rgb(0xE8, 0xDD, 0xCB),  // warm beige
    Color::from_rgb(0xC8, 0xE6, 0xD0),  // mint green
    Color::from_rgb(0xD8, 0xD0, 0xE8),  // lavender
    Color::from_rgb(0xB8, 0xBE, 0xC8),  // slate
    Color::from_rgb(0xF0, 0xF0, 0xF0),  // white
    Color::from_rgb(0xE8, 0xD0, 0xD8),  // soft pink
    Color::from_rgb(0xE8, 0xE0, 0xC8),  // light gold
};

// Panel colors (dark tones)
inline const Color panel_palette[SWATCH_COUNT] = {
    Color::from_rgb(0x2B, 0x3E, 0x50),  // dark blue-gray (default)
    Color::from_rgb(0x2D, 0x2D, 0x2D),  // dark charcoal
    Color::from_rgb(0x1B, 0x2A, 0x4A),  // navy
    Color::from_rgb(0x1A, 0x3A, 0x3A),  // dark teal
    Color::from_rgb(0x1A, 0x3A, 0x1A),  // dark green
    Color::from_rgb(0x30, 0x20, 0x40),  // dark purple
    Color::from_rgb(0x40, 0x1A, 0x1A),  // dark red
    Color::from_rgb(0x10, 0x10, 0x10),  // black
};

// Accent colors
inline const Color accent_palette[SWATCH_COUNT] = {
    Color::from_rgb(0x36, 0x7B, 0xF0),  // blue (default)
    Color::from_rgb(0x00, 0x9B, 0x9B),  // teal
    Color::from_rgb(0x2E, 0x9E, 0x3E),  // green
    Color::from_rgb(0xE0, 0x8A, 0x20),  // orange
    Color::from_rgb(0xD0, 0x3E, 0x3E),  // red
    Color::from_rgb(0x7B, 0x3E, 0xB8),  // purple
    Color::from_rgb(0xD0, 0x5C, 0x9E),  // pink
    Color::from_rgb(0x44, 0x44, 0xCC),  // indigo
};

inline constexpr int TAB_BAR_H = 36;
inline constexpr int TAB_COUNT = 3;
inline constexpr const char* tab_labels[TAB_COUNT] = { "Appearance", "Display", "About" };

inline constexpr int WP_ITEM_H = 24;

inline mtk::Theme settings_theme(const SettingsState* st) {
    return mtk::make_theme(st->desktop->settings.accent_color);
}

bool color_eq(Color a, Color b);
bool settings_status_visible(SettingsState* st);
void settings_set_status(SettingsState* st, const char* msg);
int find_swatch(const Color* palette, Color current);

void apply_ui_scale(int scale);

void settings_draw_appearance(Canvas& c, SettingsState* st);
bool settings_handle_appearance_click(Window* win, SettingsState* st, int mx, int cy);

void settings_draw_display(Canvas& c, SettingsState* st);
bool settings_handle_display_click(SettingsState* st, int mx, int cy);

void settings_draw_about(Canvas& c, SettingsState* st);

void settings_persist(SettingsState* st);

} // namespace settings_app
