' Dialog List — demonstrates list selection dialog simulation, no string arrays
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL choice AS LONG
    LOCAL pageSize AS LONG
    LOCAL currentPage AS LONG
    LOCAL totalPages AS LONG
    LOCAL startIdx AS LONG
    LOCAL endIdx AS LONG
    LOCAL selection AS STRING
    LOCAL inputStr AS STRING
    LOCAL tempStr AS STRING
    LOCAL highlighted AS LONG
    LOCAL itemVal AS LONG
    LOCAL itemCount AS LONG
    LOCAL filterStr AS STRING
    LOCAL filteredCount AS LONG
    LOCAL selectCount AS LONG
    LOCAL prevCat AS STRING
    LOCAL catCount AS LONG
    LOCAL lineStr AS STRING
    PRINT "============================================"
    PRINT "        LIST DIALOG DEMONSTRATION"
    PRINT "============================================"
    PRINT ""
    PRINT "Creating list of items..."
    PRINT ""
    itemCount = 25
    FOR i = 0 TO itemCount - 1
        itemVal = (i * 17 + 5) MOD 100
        PRINT "  " + STR$(i + 1) + ". Item " + STR$(i + 1) + " Value=" + STR$(itemVal)
    NEXT i
    PRINT ""
    PRINT "Enter item number to select (1-" + STR$(itemCount) + "):"
    PRINT ">"
    LINE INPUT inputStr
    choice = VAL(inputStr)
    IF choice >= 1 AND choice <= itemCount THEN
        itemVal = (choice - 1) * 17 + 5
        itemVal = itemVal MOD 100
        PRINT "Selected: Item " + STR$(choice) + " Value=" + STR$(itemVal)
    ELSE
        PRINT "Invalid selection"
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "     PAGED LIST VIEW"
    PRINT "============================================"
    PRINT ""
    pageSize = 5
    totalPages = (itemCount + pageSize - 1) / pageSize
    PRINT "Paged list (" + STR$(pageSize) + " items per page, " + STR$(totalPages) + " pages):"
    PRINT ""
    FOR currentPage = 0 TO totalPages - 1
        startIdx = currentPage * pageSize
        endIdx = startIdx + pageSize - 1
        IF endIdx >= itemCount THEN
            endIdx = itemCount - 1
        END IF
        PRINT "--- Page " + STR$(currentPage + 1) + " ---"
        FOR i = startIdx TO endIdx
            itemVal = (i * 17 + 5) MOD 100
            PRINT "  " + STR$(i + 1) + ". Item " + STR$(i + 1) + " Value=" + STR$(itemVal)
        NEXT i
    NEXT currentPage
    PRINT ""
    PRINT "============================================"
    PRINT "     FILTERED LIST"
    PRINT "============================================"
    PRINT ""
    filterStr = "1"
    PRINT "Filter: items containing '" + filterStr + "'"
    PRINT ""
    filteredCount = 0
    FOR i = 0 TO itemCount - 1
        itemVal = (i * 17 + 5) MOD 100
        IF INSTR(STR$(itemVal), filterStr) > 0 THEN
            filteredCount = filteredCount + 1
            PRINT "  " + STR$(filteredCount) + ". Item " + STR$(i + 1) + " Value=" + STR$(itemVal)
        END IF
    NEXT i
    IF filteredCount = 0 THEN
        PRINT "No items match filter"
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "     MULTI-SELECT LIST"
    PRINT "============================================"
    PRINT ""
    PRINT "Items: 1, 3, 5, 7, 9"
    PRINT ""
    selectCount = 5
    PRINT "Selected items:"
    choice = 1
    itemVal = (0 * 17 + 5) MOD 100
    PRINT "  " + STR$(choice) + ". Item " + STR$(choice) + " Value=" + STR$(itemVal)
    choice = 3
    itemVal = (2 * 17 + 5) MOD 100
    PRINT "  " + STR$(choice) + ". Item " + STR$(choice) + " Value=" + STR$(itemVal)
    choice = 5
    itemVal = (4 * 17 + 5) MOD 100
    PRINT "  " + STR$(choice) + ". Item " + STR$(choice) + " Value=" + STR$(itemVal)
    choice = 7
    itemVal = (6 * 17 + 5) MOD 100
    PRINT "  " + STR$(choice) + ". Item " + STR$(choice) + " Value=" + STR$(itemVal)
    choice = 9
    itemVal = (8 * 17 + 5) MOD 100
    PRINT "  " + STR$(choice) + ". Item " + STR$(choice) + " Value=" + STR$(itemVal)
    PRINT ""
    PRINT "============================================"
    PRINT "     SORTED LIST (by computed value)"
    PRINT "============================================"
    PRINT ""
    LOCAL sorted(100) AS LONG
    LOCAL indices(100) AS LONG
    LOCAL tmpIdx AS LONG
    LOCAL tmpVal AS LONG
    FOR i = 0 TO itemCount - 1
        indices(i) = i
        sorted(i) = (i * 17 + 5) MOD 100
    NEXT i
    FOR i = 0 TO itemCount - 2
        FOR j = 0 TO itemCount - 2 - i
            IF sorted(j) > sorted(j + 1) THEN
                tmpVal = sorted(j)
                sorted(j) = sorted(j + 1)
                sorted(j + 1) = tmpVal
                tmpIdx = indices(j)
                indices(j) = indices(j + 1)
                indices(j + 1) = tmpIdx
            END IF
        NEXT j
    NEXT i
    PRINT "Sorted by value:"
    PRINT ""
    FOR i = 0 TO itemCount - 1
        PRINT "  " + STR$(i + 1) + ". Item " + STR$(indices(i) + 1) + " Value=" + STR$(sorted(i))
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "     LIST WITH HIGHLIGHTED ITEM"
    PRINT "============================================"
    PRINT ""
    highlighted = 7
    PRINT "List (item " + STR$(highlighted) + " highlighted):"
    PRINT ""
    FOR i = 0 TO itemCount - 1
        itemVal = (i * 17 + 5) MOD 100
        IF i + 1 = highlighted THEN
            PRINT "  >> " + STR$(i + 1) + ". Item " + STR$(i + 1) + " Value=" + STR$(itemVal) + " <<"
        ELSE
            PRINT "     " + STR$(i + 1) + ". Item " + STR$(i + 1) + " Value=" + STR$(itemVal)
        END IF
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "     CATEGORY LIST"
    PRINT "============================================"
    PRINT ""
    catCount = 5
    prevCat = ""
    IF prevCat <> "Fruits" THEN
        PRINT "--- Fruits ---"
        prevCat = "Fruits"
    END IF
    PRINT "  Apple"
    PRINT "  Banana"
    PRINT "--- Vegetables ---"
    prevCat = "Vegetables"
    PRINT "  Carrot"
    PRINT "  Date"
    PRINT "--- Grains ---"
    prevCat = "Grains"
    PRINT "  Einkorn"
    PRINT ""
    PRINT "List dialog demonstration complete"
    FUNCTION = 0
END FUNCTION
