// listHelp.c -- Shared list/dropdown helper functions
//
// Implements dropdown arrow drawing, item length scanning, keyboard
// navigation, and popup list painting used by ListBox, Dropdown,
// ComboBox, ListView, and TreeView widgets.

#include "listHelp.h"
#include "../texthelp/textHelp.h"

#include <string.h>


// ============================================================
// widgetDrawDropdownArrow
// ============================================================
//
// Draws a small downward-pointing filled triangle (7, 5, 3, 1 pixels
// wide across 4 rows) centered at the given position. Used by both
// Dropdown and ComboBox for the drop button arrow glyph.

void widgetDrawDropdownArrow(DisplayT *d, const BlitOpsT *ops, int32_t centerX, int32_t centerY, uint32_t color) {
    for (int32_t i = 0; i < 4; i++) {
        drawHLine(d, ops, centerX - 3 + i, centerY + i, 7 - i * 2, color);
    }
}


// ============================================================
// widgetMaxItemLen
// ============================================================
//
// Scans an array of string items and returns the maximum strlen.
// Shared by ListBox, Dropdown, and ComboBox to cache the widest
// item length for calcMinSize without duplicating the loop.

int32_t widgetMaxItemLen(const char **items, int32_t count) {
    int32_t maxLen = 0;

    for (int32_t i = 0; i < count; i++) {
        int32_t slen = (int32_t)strlen(items[i]);

        if (slen > maxLen) {
            maxLen = slen;
        }
    }

    return maxLen;
}


// ============================================================
// widgetNavigateIndex
// ============================================================
//
// Shared keyboard navigation for list-like widgets (ListBox, Dropdown,
// ListView, etc.). Encapsulates the Up/Down/Home/End/PgUp/PgDn logic
// so each widget doesn't have to reimplement index clamping.
//
// Key values use the 0x100 flag to mark extended scan codes (arrow
// keys, Home, End, etc.) -- this is the DVX convention for passing
// scan codes through the same int32_t channel as ASCII values.
//
// Returns -1 for unrecognized keys so callers can check whether the
// key was consumed.

int32_t widgetNavigateIndex(int32_t key, int32_t current, int32_t count, int32_t pageSize) {
    if (key == (0x50 | 0x100)) {
        // Down arrow
        if (current < count - 1) {
            return current + 1;
        }

        return current < 0 ? 0 : current;
    }

    if (key == (0x48 | 0x100)) {
        // Up arrow
        if (current > 0) {
            return current - 1;
        }

        return current < 0 ? 0 : current;
    }

    if (key == (0x47 | 0x100)) {
        // Home
        return 0;
    }

    if (key == (0x4F | 0x100)) {
        // End
        return count - 1;
    }

    if (key == (0x51 | 0x100)) {
        // Page Down
        int32_t n = current + pageSize;
        return n >= count ? count - 1 : n;
    }

    if (key == (0x49 | 0x100)) {
        // Page Up
        int32_t n = current - pageSize;
        return n < 0 ? 0 : n;
    }

    return -1;
}


// ============================================================
// widgetPaintPopupList
// ============================================================
//
// Shared popup list painting for Dropdown and ComboBox.

void widgetPaintPopupList(DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors, int32_t popX, int32_t popY, int32_t popW, int32_t popH, const char **items, int32_t itemCount, int32_t hoverIdx, int32_t scrollPos) {
    // Draw popup border
    BevelStyleT bevel;
    bevel.highlight = colors->windowHighlight;
    bevel.shadow    = colors->windowShadow;
    bevel.face      = colors->contentBg;
    bevel.width     = 2;
    drawBevel(d, ops, popX, popY, popW, popH, &bevel);

    // Draw items
    int32_t visibleItems = popH / font->charHeight;
    int32_t textX        = popX + TEXT_INPUT_PAD;
    int32_t textY        = popY + 2;
    int32_t textW        = popW - TEXT_INPUT_PAD * 2 - 4;

    for (int32_t i = 0; i < visibleItems && (scrollPos + i) < itemCount; i++) {
        int32_t  idx = scrollPos + i;
        int32_t  iy  = textY + i * font->charHeight;
        uint32_t ifg = colors->contentFg;
        uint32_t ibg = colors->contentBg;

        if (idx == hoverIdx) {
            ifg = colors->menuHighlightFg;
            ibg = colors->menuHighlightBg;
            rectFill(d, ops, popX + 2, iy, textW + TEXT_INPUT_PAD * 2, font->charHeight, ibg);
        }

        drawText(d, ops, font, textX, iy, items[idx], ifg, ibg, false);
    }

    // Draw scroll indicators if the list extends beyond visible area
    if (itemCount > visibleItems) {
        int32_t cx      = popX + popW / 2;
        uint32_t arrowC = colors->menuHighlightBg;

        // Up triangle (point at top, wide at bottom)
        if (scrollPos > 0) {
            int32_t ty = popY + 2;

            for (int32_t i = 0; i < 3; i++) {
                drawHLine(d, ops, cx - i, ty + i, 1 + i * 2, arrowC);
            }
        }

        // Down triangle (wide at top, point at bottom)
        if (scrollPos + visibleItems < itemCount) {
            int32_t by = popY + popH - 4;

            for (int32_t i = 0; i < 3; i++) {
                drawHLine(d, ops, cx - i, by - i, 1 + i * 2, arrowC);
            }
        }
    }
}


// ============================================================
// widgetDropdownPopupRect
// ============================================================
//
// Calculates the screen rectangle for a dropdown/combobox popup list.
// Shared between Dropdown and ComboBox since they have identical
// popup positioning logic.

void widgetDropdownPopupRect(WidgetT *w, const BitmapFontT *font, int32_t contentH, int32_t itemCount, int32_t *popX, int32_t *popY, int32_t *popW, int32_t *popH) {
    int32_t visibleItems = itemCount;

    if (visibleItems > DROPDOWN_MAX_VISIBLE) {
        visibleItems = DROPDOWN_MAX_VISIBLE;
    }

    if (visibleItems < 1) {
        visibleItems = 1;
    }

    *popX = w->x;
    *popW = w->w;
    *popH = visibleItems * font->charHeight + 4;

    if (w->y + w->h + *popH <= contentH) {
        *popY = w->y + w->h;
    } else {
        *popY = w->y - *popH;

        if (*popY < 0) {
            *popY = 0;
        }
    }
}
