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

#include "settings_internal.hpp"

namespace settings_app {

void apply_ui_scale(int scale) {
    switch (scale) {
    case 0: fonts::UI_SIZE = 14; fonts::TITLE_SIZE = 14; fonts::TERM_SIZE = 14; fonts::LARGE_SIZE = 22; break;
    case 2: fonts::UI_SIZE = 22; fonts::TITLE_SIZE = 22; fonts::TERM_SIZE = 22; fonts::LARGE_SIZE = 34; break;
    default: fonts::UI_SIZE = 18; fonts::TITLE_SIZE = 18; fonts::TERM_SIZE = 18; fonts::LARGE_SIZE = 28; break;
    }
}

void settings_draw_display(Canvas& c, SettingsState* st) {
    DesktopSettings& s = st->desktop->settings;
    mtk::Theme theme = settings_theme(st);
    int x = 16;
    int y = 20;
    int btn_w = 60;
    int btn_h = 28;
    int bx = x + 180;

    c.text(x, y + 6, "Window Shadows", theme.text);
    mtk::draw_button(c, {bx, y, btn_w, btn_h}, "On", mtk::BUTTON_SECONDARY,
                     mtk::widget_state(s.show_shadows), theme);
    mtk::draw_button(c, {bx + btn_w + 8, y, btn_w, btn_h}, "Off", mtk::BUTTON_SECONDARY,
                     mtk::widget_state(!s.show_shadows), theme);
    y += btn_h + 20;

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

    c.text(x, y + 6, "Clock Format", theme.text);
    mtk::draw_button(c, {bx, y, btn_w, btn_h}, "24h", mtk::BUTTON_SECONDARY,
                     mtk::widget_state(s.clock_24h), theme);
    mtk::draw_button(c, {bx + btn_w + 8, y, btn_w, btn_h}, "12h", mtk::BUTTON_SECONDARY,
                     mtk::widget_state(!s.clock_24h), theme);
    y += btn_h + 20;

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

    c.text(x, y + 6, "UI Scale", theme.text);
    int sbw = 68;
    mtk::draw_button(c, {bx, y, sbw, btn_h}, "Small", mtk::BUTTON_SECONDARY,
                     mtk::widget_state(s.ui_scale == 0), theme);
    mtk::draw_button(c, {bx + sbw + 8, y, sbw, btn_h}, "Default", mtk::BUTTON_SECONDARY,
                     mtk::widget_state(s.ui_scale == 1), theme);
    mtk::draw_button(c, {bx + (sbw + 8) * 2, y, sbw, btn_h}, "Large", mtk::BUTTON_SECONDARY,
                     mtk::widget_state(s.ui_scale == 2), theme);
}

bool settings_handle_display_click(SettingsState* st, int mx, int cy) {
    DesktopSettings& s = st->desktop->settings;
    int x = 16;
    int y = 20;
    int btn_w = 60;
    int btn_h = 28;
    int bx = x + 180;

    Rect shadows_on = {bx, y, btn_w, btn_h};
    Rect shadows_off = {bx + btn_w + 8, y, btn_w, btn_h};
    if (shadows_on.contains(mx, cy)) {
        s.show_shadows = true;
        settings_persist(st);
        return true;
    }
    if (shadows_off.contains(mx, cy)) {
        s.show_shadows = false;
        settings_persist(st);
        return true;
    }
    y += btn_h + 20 + 16;

    Rect clock_24 = {bx, y, btn_w, btn_h};
    Rect clock_12 = {bx + btn_w + 8, y, btn_w, btn_h};
    if (clock_24.contains(mx, cy)) {
        s.clock_24h = true;
        settings_persist(st);
        return true;
    }
    if (clock_12.contains(mx, cy)) {
        s.clock_24h = false;
        settings_persist(st);
        return true;
    }
    y += btn_h + 20 + 16;

    int sbw = 68;
    Rect scale_small = {bx, y, sbw, btn_h};
    Rect scale_default = {bx + sbw + 8, y, sbw, btn_h};
    Rect scale_large = {bx + (sbw + 8) * 2, y, sbw, btn_h};
    if (scale_small.contains(mx, cy)) {
        s.ui_scale = 0;
        apply_ui_scale(0);
        montauk::win_setscale(0);
        settings_persist(st);
        return true;
    }
    if (scale_default.contains(mx, cy)) {
        s.ui_scale = 1;
        apply_ui_scale(1);
        montauk::win_setscale(1);
        settings_persist(st);
        return true;
    }
    if (scale_large.contains(mx, cy)) {
        s.ui_scale = 2;
        apply_ui_scale(2);
        montauk::win_setscale(2);
        settings_persist(st);
        return true;
    }

    return false;
}

} // namespace settings_app
