' Long operation with progress bar simulation and cancel button behavior
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL j  AS LONG
    LOCAL totalWork  AS LONG
    LOCAL currentWork  AS LONG
    LOCAL progress  AS LONG
    LOCAL percent  AS LONG
    LOCAL prevPercent  AS LONG
    LOCAL cancelled  AS LONG
    LOCAL elapsed  AS LONG
    LOCAL estimated  AS LONG
    LOCAL phase  AS LONG
    LOCAL numPhases  AS LONG
    LOCAL phaseName(10) AS STRING
    LOCAL phaseWeight(10) AS LONG
    LOCAL phaseProgress(10) AS LONG
    LOCAL totalWeight  AS LONG
    LOCAL barLen  AS LONG
    LOCAL barStr AS STRING
    LOCAL s AS STRING
    LOCAL ch AS STRING

    PRINT "=== Progress Dialog ==="
    PRINT

    totalWork = 1000
    currentWork = 0
    cancelled = 0
    barLen = 30

    numPhases = 4
    phaseName(0) = "Initializing            "
    phaseWeight(0) = 100

    phaseName(1) = "Processing files         "
    phaseWeight(1) = 400

    phaseName(2) = "Compiling                "
    phaseWeight(2) = 300

    phaseName(3) = "Finalizing               "
    phaseWeight(3) = 200

    totalWeight = 0
    FOR i = 0 TO numPhases - 1
        totalWeight = totalWeight + phaseWeight(i)
    NEXT i

    PRINT "Operation: Build Project"
    PRINT "Total work units: " & STR$(totalWork)
    PRINT "Phases: " & STR$(numPhases)
    PRINT

    FOR phase = 0 TO numPhases - 1
        IF cancelled = 1 THEN EXIT FOR
        s = TRIM$(phaseName(phase))

        FOR j = 1 TO phaseWeight(phase)
            IF cancelled = 1 THEN EXIT FOR
            currentWork = currentWork + 1
            progress = currentWork * 100 / totalWork
            phaseProgress(phase) = j * 100 / phaseWeight(phase)

            prevPercent = (currentWork - 1) * 100 / totalWork
            IF progress > prevPercent THEN
                barStr = "["
                FOR i = 1 TO barLen
                    IF i * 100 / barLen <= progress THEN
                        barStr = barStr + "#"
                    ELSE
                        barStr = barStr + "."
                    END IF
                NEXT i
                barStr = barStr + "]"

                PRINT "  " & s & ": " & FORMAT$(progress) & "%"
                PRINT "  " & barStr

                IF j MOD 50 = 0 THEN
                    elapsed = currentWork / 10
                    estimated = totalWork / 10
                    PRINT "    Elapsed: " & FORMAT$(elapsed) & "s, ETA: " & FORMAT$(estimated) & "s"
                END IF
            END IF
        NEXT j

        PRINT "  Phase '" & s & "' complete."
        PRINT
    NEXT phase

    IF cancelled = 1 THEN
        PRINT "*** OPERATION CANCELLED ***"
        PRINT "  Completed: " & FORMAT$(currentWork) & "/" & FORMAT$(totalWork) & " work units"
        PRINT "  Progress: " & FORMAT$(currentWork * 100 / totalWork) & "%"
    ELSE
        PRINT "*** OPERATION COMPLETE ***"
        PRINT "  All " & STR$(totalWork) & " work units processed"
        PRINT "  Elapsed: " & FORMAT$(totalWork / 10) & " seconds (simulated)"
    END IF
    PRINT

    PRINT "--- Cancel Behavior ---"
    PRINT "  Click Cancel or press ESC"
    PRINT "  Waits for current work unit to finish"
    PRINT "  Prompts: 'Are you sure you want to cancel?'"
    PRINT "  Yes: rolls back incomplete transaction"
    PRINT "  No: continues operation"
    PRINT

    PRINT "--- Progress Variants ---"
    PRINT "  Determinate: " & FORMAT$(totalWork) & " units known"
    PRINT "  Indeterminate: unknown total (marquee bar)"
    PRINT "  Percentage: 0-100% displayed"
    PRINT "  Time remaining: based on current rate"

    PRINT
    PRINT "Progress dialog simulation complete."

    FUNCTION = 0
END FUNCTION
