' Dialog Confirm — demonstrates confirmation dialog simulation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL inputStr AS STRING
    LOCAL confirmed AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL choice AS LONG
    LOCAL options AS LONG
    LOCAL lineStr AS STRING
    LOCAL msgStr AS STRING
    LOCAL titleStr AS STRING
    LOCAL resultStr AS STRING
    LOCAL testCount AS LONG
    LOCAL passCount AS LONG
    LOCAL failCount AS LONG
    testCount = 0
    passCount = 0
    failCount = 0
    PRINT "============================================"
    PRINT "      CONFIRMATION DIALOG DEMONSTRATION"
    PRINT "============================================"
    PRINT ""
    PRINT "1. Yes/No Confirmation"
    PRINT ""
    msgStr = "Do you want to continue?"
    PRINT msgStr
    PRINT "Enter Y to continue, N to cancel:"
    PRINT ">"
    LINE INPUT inputStr
    IF UCASE$(inputStr) = "Y" OR UCASE$(inputStr) = "YES" THEN
        confirmed = 1
        resultStr = "Confirmed"
    ELSE
        confirmed = 0
        resultStr = "Cancelled"
    END IF
    PRINT "Result: " + resultStr
    testCount = testCount + 1
    passCount = passCount + 1
    PRINT ""
    PRINT "2. Ok/Cancel Confirmation"
    PRINT ""
    msgStr = "File will be deleted. Are you sure?"
    PRINT msgStr
    PRINT "Press Enter to confirm, or type C to cancel:"
    PRINT ">"
    LINE INPUT inputStr
    IF UCASE$(inputStr) = "C" OR UCASE$(inputStr) = "CANCEL" THEN
        resultStr = "Cancelled"
    ELSE
        resultStr = "Confirmed"
    END IF
    PRINT "Result: " + resultStr
    PRINT ""
    PRINT "3. Yes/No/Cancel (3-way confirmation)"
    PRINT ""
    msgStr = "Save changes before closing?"
    PRINT msgStr
    PRINT "Enter Y=Yes, N=No, C=Cancel:"
    PRINT ">"
    LINE INPUT inputStr
    IF UCASE$(inputStr) = "Y" OR UCASE$(inputStr) = "YES" THEN
        resultStr = "Saved"
    ELSE
        IF UCASE$(inputStr) = "N" OR UCASE$(inputStr) = "NO" THEN
            resultStr = "Discarded"
        ELSE
            resultStr = "Cancelled"
        END IF
    END IF
    PRINT "Result: " + resultStr
    PRINT ""
    PRINT "============================================"
    PRINT "     CONFIRMATION WITH WARNING"
    PRINT "============================================"
    PRINT ""
    LOCAL warnings(10) AS STRING
    LOCAL warnCount AS LONG
    warnCount = 3
    PRINT "WARNING: This action has consequences:"
    FOR i = 0 TO warnCount - 1
        IF i = 0 THEN
            lineStr = "  - Data will be permanently deleted"
        END IF
        IF i = 1 THEN
            lineStr = "  - Operation cannot be undone"
        END IF
        IF i = 2 THEN
            lineStr = "  - Requires administrator privileges"
        END IF
        PRINT lineStr
    NEXT i
    PRINT ""
    PRINT "Do you wish to proceed? (Y/N):"
    PRINT ">"
    LINE INPUT inputStr
    IF UCASE$(inputStr) = "Y" OR UCASE$(inputStr) = "YES" THEN
        resultStr = "Proceeding with operation"
        confirmed = 1
    ELSE
        resultStr = "Operation cancelled"
        confirmed = 0
    END IF
    PRINT "Result: " + resultStr
    PRINT ""
    PRINT "============================================"
    PRINT "     DESTRUCTIVE ACTION CONFIRMATION"
    PRINT "============================================"
    PRINT ""
    PRINT "TYPE 'DELETE' to confirm deletion, anything else to cancel:"
    PRINT ">"
    LINE INPUT inputStr
    IF inputStr = "DELETE" THEN
        resultStr = "Deletion confirmed"
        confirmed = 1
    ELSE
        resultStr = "Deletion cancelled"
        confirmed = 0
    END IF
    PRINT "Result: " + resultStr
    PRINT ""
    PRINT "============================================"
    PRINT "     TIMEOUT CONFIRMATION"
    PRINT "============================================"
    PRINT ""
    LOCAL timeoutVal AS LONG
    timeoutVal = 5
    PRINT "This confirmation will auto-accept in " + STR$(timeoutVal) + " seconds"
    PRINT "Continue? (Y/N, or wait for timeout):"
    PRINT ">"
    LINE INPUT inputStr
    IF LEN(inputStr) = 0 THEN
        resultStr = "Auto-accepted (timeout)"
        confirmed = 1
    ELSE
        IF UCASE$(inputStr) = "Y" THEN
            resultStr = "Accepted by user"
            confirmed = 1
        ELSE
            resultStr = "Rejected by user"
            confirmed = 0
        END IF
    END IF
    PRINT "Result: " + resultStr
    PRINT ""
    PRINT "============================================"
    PRINT "     MULTI-OPTION CONFIRMATION"
    PRINT "============================================"
    PRINT ""
    options = 4
    PRINT "Select action:"
    PRINT "  1. Save and Exit"
    PRINT "  2. Save without Exiting"
    PRINT "  3. Exit without Saving"
    PRINT "  4. Cancel"
    PRINT "Enter choice (1-4):"
    PRINT ">"
    LINE INPUT inputStr
    choice = VAL(inputStr)
    IF choice = 1 THEN
        resultStr = "Save and Exit"
    ELSE
        IF choice = 2 THEN
            resultStr = "Save without Exiting"
        ELSE
            IF choice = 3 THEN
                resultStr = "Exit without Saving"
            ELSE
                resultStr = "Cancelled"
            END IF
        END IF
    END IF
    PRINT "Result: " + resultStr
    PRINT ""
    PRINT "============================================"
    PRINT "     CONFIRMATION CHAIN"
    PRINT "============================================"
    PRINT ""
    LOCAL stage AS LONG
    confirmed = 1
    FOR stage = 1 TO 3
        PRINT "Stage " + STR$(stage) + " confirmation:"
        IF stage = 1 THEN
            lineStr = "Step 1: Initialize"
        END IF
        IF stage = 2 THEN
            lineStr = "Step 2: Process"
        END IF
        IF stage = 3 THEN
            lineStr = "Step 3: Finalize"
        END IF
        PRINT lineStr
        PRINT "Continue to next step? (Y/N):"
        PRINT ">"
        LINE INPUT inputStr
        IF UCASE$(inputStr) = "Y" OR UCASE$(inputStr) = "YES" OR LEN(inputStr) = 0 THEN
            confirmed = 1
            resultStr = "Proceeding"
        ELSE
            confirmed = 0
            resultStr = "Stopped at stage " + STR$(stage)
            EXIT FOR
        END IF
        PRINT "  " + resultStr
    NEXT stage
    PRINT ""
    IF confirmed = 1 THEN
        PRINT "All stages confirmed"
    ELSE
        PRINT "Process stopped"
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "     RETRY CONFIRMATION"
    PRINT "============================================"
    PRINT ""
    LOCAL attempts AS LONG
    LOCAL maxAttempts AS LONG
    LOCAL success AS LONG
    maxAttempts = 3
    attempts = 0
    success = 0
    DO WHILE attempts < maxAttempts
        attempts = attempts + 1
        PRINT "Attempt " + STR$(attempts) + " of " + STR$(maxAttempts)
        PRINT "Retry operation? (Y/N):"
        PRINT ">"
        LINE INPUT inputStr
        IF UCASE$(inputStr) = "Y" OR UCASE$(inputStr) = "YES" THEN
            success = 1
            EXIT DO
        END IF
        IF UCASE$(inputStr) = "N" OR UCASE$(inputStr) = "NO" THEN
            EXIT DO
        END IF
    LOOP
    IF success = 1 THEN
        resultStr = "Operation succeeded on attempt " + STR$(attempts)
    ELSE
        resultStr = "Operation failed after " + STR$(attempts) + " attempts"
    END IF
    PRINT resultStr
    PRINT ""
    PRINT "Confirmation dialog demonstration complete"
    FUNCTION = 0
END FUNCTION
