' Console Progress Bar — simulated progress with INKEY$, percentage display
#COMPILE EXE
#DIM ALL

SUB DrawProgressBar(BYVAL progress AS LONG, BYVAL total AS LONG, BYVAL label AS STRING)
    LOCAL pct AS LONG
    LOCAL filled AS LONG
    LOCAL empty AS LONG
    LOCAL bar AS STRING
    LOCAL i AS LONG
    IF total <= 0 THEN EXIT SUB
    pct = (progress * 100) / total
    filled = (progress * 40) / total
    empty = 40 - filled
    bar = "["
    FOR i = 1 TO filled
        bar = bar & "#"
    NEXT i
    FOR i = 1 TO empty
        bar = bar & "."
    NEXT i
    bar = bar & "] " & STR$(pct) & "%"
    IF LEN(label) > 0 THEN bar = label & " " & bar
    PRINT bar
END SUB

SUB SimulatedTask1()
    LOCAL i AS LONG
    LOCAL total AS LONG
    total = 20
    PRINT "Task 1: Loading resources..."
    FOR i = 1 TO total
        CALL DrawProgressBar(i, total, "")
        SLEEP 50
    NEXT i
    PRINT "Task 1 complete!"
    PRINT
END SUB

SUB SimulatedTask2()
    LOCAL i AS LONG
    LOCAL total AS LONG
    total = 30
    PRINT "Task 2: Processing data..."
    FOR i = 1 TO total
        CALL DrawProgressBar(i, total, "")
        SLEEP 30
    NEXT i
    PRINT "Task 2 complete!"
    PRINT
END SUB

SUB SimulatedTask3()
    LOCAL i AS LONG
    LOCAL total AS LONG
    LOCAL k AS STRING
    total = 50
    PRINT "Task 3: Writing output..."
    PRINT "  (press any key to pause/resume)"
    FOR i = 1 TO total
        CALL DrawProgressBar(i, total, "")
        k = INKEY$
        IF k <> "" THEN
            PRINT "  PAUSED - press key to continue..."
            DO
                k = INKEY$
                SLEEP 10
            LOOP WHILE k = ""
        END IF
        SLEEP 20
    NEXT i
    PRINT "Task 3 complete!"
    PRINT
END SUB

SUB TimedProgress(BYVAL totalSec AS DOUBLE)
    LOCAL startTime AS LONG
    LOCAL elapsed AS LONG
    LOCAL totalMs AS LONG
    LOCAL current AS LONG
    startTime = TIMER
    totalMs = INT(totalSec * 1000)
    PRINT "Timed progress: " & STR$(totalSec) & " seconds"
    DO
        elapsed = TIMER - startTime
        current = elapsed
        IF current > totalMs THEN current = totalMs
        CALL DrawProgressBar(current, totalMs, "")
        IF current >= totalMs THEN EXIT LOOP
        SLEEP 50
    LOOP
    PRINT "Timed operation complete!"
    PRINT
END SUB

SUB AnimatedSpinner(BYVAL duration AS LONG)
    LOCAL startTime AS LONG
    LOCAL elapsed AS LONG
    LOCAL frame AS LONG
    LOCAL frames AS STRING
    LOCAL ch AS STRING
    frames = "|/-\"
    startTime = TIMER
    PRINT "Animated spinner (" & STR$(duration) & " ms):"
    DO
        elapsed = TIMER - startTime
        IF elapsed >= duration THEN EXIT LOOP
        frame = (elapsed \ 100) MOD 4 + 1
        ch = MID$(frames, frame, 1)
        PRINT ch
        SLEEP 100
    LOOP
    PRINT "Done!"
    PRINT
END SUB

SUB MultiStageProgress()
    LOCAL stage AS LONG
    LOCAL i AS LONG
    PRINT "Multi-stage build progress:"
    FOR stage = 1 TO 4
        PRINT "Stage " & STR$(stage) & "/4 started..."
        FOR i = 1 TO 10
            CALL DrawProgressBar(i, 10, "")
            SLEEP 25
        NEXT i
        PRINT "Stage " & STR$(stage) & " complete."
    NEXT stage
    PRINT "Build complete!"
    PRINT
END SUB

FUNCTION PBMAIN() AS LONG
    LOCAL k AS STRING
    PRINT "============================="
    PRINT " Console Progress Bar Demo "
    PRINT "============================="
    PRINT
    PRINT "Demonstrating various progress bar styles."
    PRINT
    CALL SimulatedTask1()
    CALL SimulatedTask2()
    CALL TimedProgress(1.5)
    CALL AnimatedSpinner(1500)
    CALL MultiStageProgress()
    PRINT "All demonstrations complete."
    PRINT "Press any key to exit..."
    SLEEP 3000
    FUNCTION = 0
END FUNCTION
