/*
    * appearance.cpp
    * Appearance tab for the embedded desktop settings app
    * Copyright (c) 2026 Daniel Hammer
*/

#include "settings_internal.hpp"

namespace settings_app {

void settings_draw_appearance(Canvas& c, SettingsState* st) {
    DesktopSettings& s = st->desktop->settings;
    mtk::Theme theme = settings_theme(st);
    int x = 16;
    int y = 12;
    int sfh = system_font_height();
    int line_h = sfh + 10;

    c.text(x, y, "Background", theme.text);
    y += line_h;

    bool mode_grad = s.bg_gradient && !s.bg_image;
    bool mode_solid = !s.bg_gradient && !s.bg_image;
    bool mode_image = s.bg_image;

    Rect grad_option = {x, y, 100, 20};
    Rect solid_option = {x + 120, y, 90, 20};
    Rect image_option = {x + 220, y, 100, 20};
    mtk::draw_radio(c, grad_option, "Gradient", mode_grad, theme);
    mtk::draw_radio(c, solid_option, "Solid", mode_solid, theme);
    mtk::draw_radio(c, image_option, "Image", mode_image, theme);
    y += line_h + 4;

    if (mode_image) {
        if (!st->wp_scanned) {
            wallpaper_scan_home(st->desktop->home_dir, &st->wp_files);
            st->wp_scanned = true;
        }

        c.text(x, y, "Wallpapers", theme.text_muted);
        y += sfh + 6;

        if (st->wp_files.count == 0) {
            c.text(x + 8, y, "No .jpg files found", theme.text_muted);
            y += WP_ITEM_H;
        } else {
            for (int i = 0; i < st->wp_files.count && i < 8; i++) {
                char fullpath[256];
                montauk::strcpy(fullpath, st->desktop->home_dir);
                str_append(fullpath, "/", 256);
                str_append(fullpath, st->wp_files.names[i], 256);

                bool selected = s.bg_image &&
                    montauk::streq(s.bg_image_path, fullpath);

                Rect row = {x, y, c.w - 2 * x, WP_ITEM_H};
                mtk::draw_list_row(c, row, selected, (i % 2) == 0, theme);

                char label[40];
                int nlen = montauk::slen(st->wp_files.names[i]);
                if (nlen > 35) {
                    montauk::strncpy(label, st->wp_files.names[i], 32);
                    label[32] = '.';
                    label[33] = '.';
                    label[34] = '.';
                    label[35] = '\0';
                } else {
                    montauk::strncpy(label, st->wp_files.names[i], 39);
                }

                c.text(x + 8, y + (WP_ITEM_H - sfh) / 2, label,
                       selected ? theme.text_inverse : theme.text);
                y += WP_ITEM_H;
            }
        }
        y += 6;
    } else if (mode_grad) {
        c.text(x, y + 4, "Top", theme.text_muted);
        mtk::draw_swatch_row(c, x + 70, y, bg_palette, SWATCH_COUNT,
                             find_swatch(bg_palette, s.bg_grad_top), theme,
                             SWATCH_SIZE, SWATCH_GAP);
        y += SWATCH_SIZE + 14;

        c.text(x, y + 4, "Bottom", theme.text_muted);
        mtk::draw_swatch_row(c, x + 70, y, bg_palette, SWATCH_COUNT,
                             find_swatch(bg_palette, s.bg_grad_bottom), theme,
                             SWATCH_SIZE, SWATCH_GAP);
        y += SWATCH_SIZE + 14;
    } else {
        c.text(x, y + 4, "Color", theme.text_muted);
        mtk::draw_swatch_row(c, x + 70, y, bg_palette, SWATCH_COUNT,
                             find_swatch(bg_palette, s.bg_solid), theme,
                             SWATCH_SIZE, SWATCH_GAP);
        y += SWATCH_SIZE + 14;
    }

    mtk::draw_separator(c, x, y, c.w - 2 * x, theme);
    y += 12;

    c.text(x, y + 4, "Panel Color", theme.text);
    mtk::draw_swatch_row(c, x + 110, y, panel_palette, SWATCH_COUNT,
                         find_swatch(panel_palette, s.panel_color), theme,
                         SWATCH_SIZE, SWATCH_GAP);
    y += SWATCH_SIZE + 14;

    mtk::draw_separator(c, x, y, c.w - 2 * x, theme);
    y += 12;

    c.text(x, y + 4, "Accent Color", theme.text);
    mtk::draw_swatch_row(c, x + 110, y, accent_palette, SWATCH_COUNT,
                         find_swatch(accent_palette, s.accent_color), theme,
                         SWATCH_SIZE, SWATCH_GAP);
}

bool settings_handle_appearance_click(Window* win, SettingsState* st, int mx, int cy) {
    DesktopSettings& s = st->desktop->settings;
    int x = 16;
    int sfh = system_font_height();
    int line_h = sfh + 10;
    int y = 12;

    bool mode_grad = s.bg_gradient && !s.bg_image;
    bool mode_image = s.bg_image;

    y += line_h;

    Rect grad_option = {x, y, 100, 20};
    Rect solid_option = {x + 120, y, 90, 20};
    Rect image_option = {x + 220, y, 100, 20};
    if (grad_option.contains(mx, cy)) {
        s.bg_gradient = true;
        s.bg_image = false;
        desktop_mark_background_dirty(st->desktop);
        settings_persist(st);
        return true;
    }
    if (solid_option.contains(mx, cy)) {
        s.bg_gradient = false;
        s.bg_image = false;
        desktop_mark_background_dirty(st->desktop);
        settings_persist(st);
        return true;
    }
    if (image_option.contains(mx, cy)) {
        s.bg_image = true;
        s.bg_gradient = false;
        if (!st->wp_scanned) {
            wallpaper_scan_home(st->desktop->home_dir, &st->wp_files);
            st->wp_scanned = true;
        }
        desktop_mark_background_dirty(st->desktop);
        settings_persist(st);
        return true;
    }
    y += line_h + 4;

    if (mode_image) {
        y += sfh + 6;
        for (int i = 0; i < st->wp_files.count && i < 8; i++) {
            Rect row = {x, y, win->content_w - 2 * x, WP_ITEM_H};
            if (row.contains(mx, cy)) {
                char fullpath[256];
                montauk::strcpy(fullpath, st->desktop->home_dir);
                str_append(fullpath, "/", 256);
                str_append(fullpath, st->wp_files.names[i], 256);
                wallpaper_load(&s, fullpath,
                               st->desktop->screen_w, st->desktop->screen_h);
                desktop_mark_background_dirty(st->desktop);
                settings_persist(st);
                return true;
            }
            y += WP_ITEM_H;
        }
        if (st->wp_files.count == 0) y += WP_ITEM_H;
        y += 6;
    } else if (mode_grad) {
        int idx = mtk::hit_swatch_row(mx, cy, x + 70, y, SWATCH_COUNT, SWATCH_SIZE, SWATCH_GAP);
        if (idx >= 0) {
            s.bg_grad_top = bg_palette[idx];
            desktop_mark_background_dirty(st->desktop);
            settings_persist(st);
            return true;
        }
        y += SWATCH_SIZE + 14;

        idx = mtk::hit_swatch_row(mx, cy, x + 70, y, SWATCH_COUNT, SWATCH_SIZE, SWATCH_GAP);
        if (idx >= 0) {
            s.bg_grad_bottom = bg_palette[idx];
            desktop_mark_background_dirty(st->desktop);
            settings_persist(st);
            return true;
        }
        y += SWATCH_SIZE + 14;
    } else {
        int idx = mtk::hit_swatch_row(mx, cy, x + 70, y, SWATCH_COUNT, SWATCH_SIZE, SWATCH_GAP);
        if (idx >= 0) {
            s.bg_solid = bg_palette[idx];
            desktop_mark_background_dirty(st->desktop);
            settings_persist(st);
            return true;
        }
        y += SWATCH_SIZE + 14;
    }

    y += 12;
    int idx = mtk::hit_swatch_row(mx, cy, x + 110, y, SWATCH_COUNT, SWATCH_SIZE, SWATCH_GAP);
    if (idx >= 0) {
        s.panel_color = panel_palette[idx];
        desktop_mark_background_dirty(st->desktop);
        settings_persist(st);
        return true;
    }

    y += SWATCH_SIZE + 14;
    y += 12;
    idx = mtk::hit_swatch_row(mx, cy, x + 110, y, SWATCH_COUNT, SWATCH_SIZE, SWATCH_GAP);
    if (idx >= 0) {
        s.accent_color = accent_palette[idx];
        settings_persist(st);
        return true;
    }

    return false;
}

} // namespace settings_app
