/*
 * render.cpp
 * Drawing for the Wikipedia client
 * Copyright (c) 2026 Daniel Hammer
 */

#include "wikipedia.h"

void draw_display_toggle_button(Canvas& canvas,
                                const Rect& bounds,
                                const mtk::WidgetState& state,
                                const mtk::Theme& theme) {
    mtk::ButtonColors colors = mtk::resolve_button_colors(mtk::BUTTON_SECONDARY, state, theme);
    mtk::draw_rounded_frame(canvas, bounds, theme.radius_md, colors.bg, colors.border);

    const SvgIcon& icon = (state.active && g_display_icon_active.pixels)
                        ? g_display_icon_active
                        : g_display_icon;
    if (icon.pixels) {
        int ix = bounds.x + (bounds.w - icon.width) / 2;
        int iy = bounds.y + (bounds.h - icon.height) / 2;
        canvas.icon(ix, iy, icon);
        return;
    }

    int glyph_w = gui_min(bounds.w - 10, 16);
    int glyph_h = 12;
    int gx = bounds.x + (bounds.w - glyph_w) / 2;
    int gy = bounds.y + (bounds.h - glyph_h) / 2;
    canvas.fill_rect(gx, gy, glyph_w - 4, 2, colors.fg);
    canvas.fill_rect(gx, gy + 5, glyph_w, 2, colors.fg);
    canvas.fill_rect(gx, gy + 10, glyph_w - 6, 2, colors.fg);
    canvas.fill_rect(gx + glyph_w - 3, gy - 1, 3, 4, colors.fg);
}

void render_message(Canvas& canvas, const Rect& content, const char* text, Color color) {
    if (!text || !text[0]) return;
    Rect column = article_column_rect();
    canvas.text(column.x, content.y + 16, text, color);
}

void render(Canvas& canvas) {
    mtk::Theme theme = wiki_theme();
    ReaderToolbarLayout lo = toolbar_layout();
    Rect toolbar = {0, 0, g_win_w, TOOLBAR_H};
    Rect content = content_rect();
    Rect column = document_column_rect();
    bool searching = (g_phase == AppPhase::LOADING);

    canvas.fill(theme.window_bg);

    // ---- Toolbar ----
    canvas.fill_rect(toolbar.x, toolbar.y, toolbar.w, toolbar.h, theme.surface_alt);
    mtk::draw_text_field(canvas, lo.search_field, g_query,
                         g_query_input.cursor,
                         g_search_focused, false, theme,
                         g_query_input.selection_anchor);
    draw_display_toggle_button(canvas, lo.display_button,
                               mtk::widget_state(g_display_controls_open,
                                                 mouse_in_rect(lo.display_button), true),
                               theme);
    mtk::draw_button(canvas, lo.search_button, searching ? "Searching..." : "Search",
                     mtk::BUTTON_PRIMARY,
                     mtk::widget_state(false, mouse_in_rect(lo.search_button), !searching),
                     theme);

    if (g_display_controls_open) {
        int row_top = reader_row_y() - 4;
        canvas.hline(8, row_top, gui_max(g_win_w - 16, 0), theme.border);

        if (lo.show_labels) {
            canvas.text(lo.text_size_label.x, lo.text_size_label.y, "Text", theme.text_muted);
            canvas.text(lo.font_face_label.x, lo.font_face_label.y, "Font", theme.text_muted);
        }

        for (int i = 0; i < READER_TEXT_SIZE_COUNT; i++) {
            mtk::draw_button(canvas, lo.text_size_buttons[i], g_text_size_labels[i], mtk::BUTTON_SECONDARY,
                             mtk::widget_state((int)g_reader_text_size == i,
                                               mouse_in_rect(lo.text_size_buttons[i]), true),
                             theme);
        }
        for (int i = 0; i < READER_FONT_FACE_COUNT; i++) {
            mtk::draw_button(canvas, lo.font_buttons[i], g_font_face_labels[i], mtk::BUTTON_SECONDARY,
                             mtk::widget_state((int)g_reader_font_face == i,
                                               mouse_in_rect(lo.font_buttons[i]), true),
                             theme);
        }

        int sep_y = reader_row_y() + 3;
        int sep_h = gui_max(reader_button_h() - 6, 1);
        canvas.vline(lo.sep_x, sep_y, sep_h, theme.border);
    }
    mtk::draw_separator(canvas, 0, TOOLBAR_H, g_win_w, theme);

    // ---- Content area ----
    if (g_phase == AppPhase::IDLE) {
        render_message(canvas, content,
                       "Type a topic and press Enter or click Search.",
                       theme.text_muted);
    } else if (g_phase == AppPhase::LOADING) {
        render_message(canvas, content, "Searching Wikipedia...", theme.text_muted);
    } else if (g_phase == AppPhase::ERR) {
        render_message(canvas, content, g_status, theme.danger);
    } else if (document_scrollable() && g_line_count > 0) {
        int y = content.y + document_top_pad();

        for (int i = g_scroll_y; i < g_line_count && y < g_win_h; i++) {
            WikiLine& l  = g_lines[i];
            TrueTypeFont* line_font = l.font ? l.font : current_body_font();
            int lh = line_font ? (line_font->get_line_height(l.font_size) + 4) : g_line_h;
            if (y + lh > g_win_h) break;
            if (l.text[0] != '\0' && line_font) {
                line_font->draw_to_buffer(canvas.pixels, canvas.w, canvas.h,
                                          column.x + l.indent, y, l.text, l.color, l.font_size);
            }
            y += lh;
        }

        Rect track = scrollbar_track_rect();
        if (!track.empty()) {
            Rect thumb = scrollbar_thumb_rect();
            mtk::draw_scrollbar(canvas, track, thumb,
                                mouse_in_rect(track),
                                g_scroll_dragging,
                                theme);
        }
    }

    mtk::draw_text_input_context_menu(
        canvas, g_query_input, theme,
        mtk::text_input_has_selection(g_query_input,
                                      g_query, (int)sizeof(g_query)));
}
