#define DVX_WIDGET_IMPL
// widgetScrollbar.c  -- Shared scrollbar painting and hit-testing
//
// These are not widgets themselves -- they are stateless rendering and
// hit-testing utilities shared by ScrollPane, TreeView, TextArea,
// ListBox, and ListView. Each owning widget stores its own scroll
// position; these functions are purely geometric.
//
// The scrollbar model uses three parts: two arrow buttons (one at each
// end) and a proportional thumb in the track between them. Thumb size
// is proportional to (visibleSize / totalSize), clamped to SB_MIN_THUMB
// to remain grabbable even when content is very large. Thumb position
// maps linearly from scrollPos to track position.
//
// Arrow triangles are drawn with simple loop-based scanlines (4 rows),
// producing 7-pixel-wide arrow glyphs. This avoids any font or bitmap
// dependency for the scrollbar chrome.
//
// The minimum scrollbar length guard (sbW < barW * 3) ensures
// there is at least room for both arrow buttons plus a minimal track.
// If the container is too small, the scrollbar is simply not drawn
// rather than rendering a corrupted mess.
//
// The *Ex variants accept a caller-supplied bar thickness for widgets
// (such as ScrollPane) that need a non-default scrollbar width. The
// plain variants forward to the Ex variants using WGT_SB_W.

#include "dvxWgtP.h"


void widgetDrawScrollbarH(DisplayT *d, const BlitOpsT *ops, const ColorSchemeT *colors, int32_t sbX, int32_t sbY, int32_t sbW, int32_t totalSize, int32_t visibleSize, int32_t scrollPos) {
    widgetDrawScrollbarHEx(d, ops, colors, sbX, sbY, sbW, totalSize, visibleSize, scrollPos, WGT_SB_W);
}


void widgetDrawScrollbarHEx(DisplayT *d, const BlitOpsT *ops, const ColorSchemeT *colors, int32_t sbX, int32_t sbY, int32_t sbW, int32_t totalSize, int32_t visibleSize, int32_t scrollPos, int32_t barW) {
    if (sbW < barW * 3) {
        return;
    }

    // Trough background
    BevelStyleT troughBevel = BEVEL_TROUGH(colors);
    drawBevel(d, ops, sbX, sbY, sbW, barW, &troughBevel);

    // Left arrow button
    BevelStyleT btnBevel = BEVEL_SB_BUTTON(colors);
    drawBevel(d, ops, sbX, sbY, barW, barW, &btnBevel);

    // Left arrow triangle
    {
        int32_t  cx = sbX + barW / 2;
        int32_t  cy = sbY + barW / 2;
        uint32_t fg = colors->scrollbarFg;

        for (int32_t i = 0; i < 4; i++) {
            drawVLine(d, ops, cx - 2 + i, cy - i, 1 + i * 2, fg);
        }
    }

    // Right arrow button
    int32_t rightX = sbX + sbW - barW;
    drawBevel(d, ops, rightX, sbY, barW, barW, &btnBevel);

    // Right arrow triangle
    {
        int32_t  cx = rightX + barW / 2;
        int32_t  cy = sbY + barW / 2;
        uint32_t fg = colors->scrollbarFg;

        for (int32_t i = 0; i < 4; i++) {
            drawVLine(d, ops, cx + 2 - i, cy - i, 1 + i * 2, fg);
        }
    }

    // Thumb
    int32_t trackLen = sbW - barW * 2;

    if (trackLen > 0 && totalSize > 0) {
        int32_t thumbPos;
        int32_t thumbSize;
        widgetScrollbarThumb(trackLen, totalSize, visibleSize, scrollPos, &thumbPos, &thumbSize);

        drawBevel(d, ops, sbX + barW + thumbPos, sbY, thumbSize, barW, &btnBevel);
    }
}


void widgetDrawScrollbarV(DisplayT *d, const BlitOpsT *ops, const ColorSchemeT *colors, int32_t sbX, int32_t sbY, int32_t sbH, int32_t totalSize, int32_t visibleSize, int32_t scrollPos) {
    widgetDrawScrollbarVEx(d, ops, colors, sbX, sbY, sbH, totalSize, visibleSize, scrollPos, WGT_SB_W);
}


void widgetDrawScrollbarVEx(DisplayT *d, const BlitOpsT *ops, const ColorSchemeT *colors, int32_t sbX, int32_t sbY, int32_t sbH, int32_t totalSize, int32_t visibleSize, int32_t scrollPos, int32_t barW) {
    if (sbH < barW * 3) {
        return;
    }

    // Trough background
    BevelStyleT troughBevel = BEVEL_TROUGH(colors);
    drawBevel(d, ops, sbX, sbY, barW, sbH, &troughBevel);

    // Up arrow button
    BevelStyleT btnBevel = BEVEL_SB_BUTTON(colors);
    drawBevel(d, ops, sbX, sbY, barW, barW, &btnBevel);

    // Up arrow triangle
    {
        int32_t  cx = sbX + barW / 2;
        int32_t  cy = sbY + barW / 2;
        uint32_t fg = colors->scrollbarFg;

        for (int32_t i = 0; i < 4; i++) {
            drawHLine(d, ops, cx - i, cy - 2 + i, 1 + i * 2, fg);
        }
    }

    // Down arrow button
    int32_t downY = sbY + sbH - barW;
    drawBevel(d, ops, sbX, downY, barW, barW, &btnBevel);

    // Down arrow triangle
    {
        int32_t  cx = sbX + barW / 2;
        int32_t  cy = downY + barW / 2;
        uint32_t fg = colors->scrollbarFg;

        for (int32_t i = 0; i < 4; i++) {
            drawHLine(d, ops, cx - i, cy + 2 - i, 1 + i * 2, fg);
        }
    }

    // Thumb
    int32_t trackLen = sbH - barW * 2;

    if (trackLen > 0 && totalSize > 0) {
        int32_t thumbPos;
        int32_t thumbSize;
        widgetScrollbarThumb(trackLen, totalSize, visibleSize, scrollPos, &thumbPos, &thumbSize);

        drawBevel(d, ops, sbX, sbY + barW + thumbPos, barW, thumbSize, &btnBevel);
    }
}


// Axis-agnostic hit test. The caller converts (vx,vy) into a 1D
// position along the scrollbar axis (relPos) and the scrollbar
// length (sbLen). Returns which zone was hit: arrow buttons,
// page-up/page-down trough regions, or the thumb itself.
// This factoring lets all scrollbar-owning widgets share the same
// logic without duplicating per-axis code.
ScrollHitE widgetScrollbarHitTest(int32_t sbLen, int32_t relPos, int32_t totalSize, int32_t visibleSize, int32_t scrollPos) {
    if (relPos < WGT_SB_W) {
        return ScrollHitArrowDecE;
    }

    if (relPos >= sbLen - WGT_SB_W) {
        return ScrollHitArrowIncE;
    }

    int32_t trackLen = sbLen - WGT_SB_W * 2;

    if (trackLen > 0) {
        int32_t thumbPos;
        int32_t thumbSize;
        widgetScrollbarThumb(trackLen, totalSize, visibleSize, scrollPos, &thumbPos, &thumbSize);

        int32_t trackRel = relPos - WGT_SB_W;

        if (trackRel < thumbPos) {
            return ScrollHitPageDecE;
        }

        if (trackRel >= thumbPos + thumbSize) {
            return ScrollHitPageIncE;
        }

        return ScrollHitThumbE;
    }

    return ScrollHitNoneE;
}
