' Countdown timer with display update simulation and alarm trigger
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL totalSeconds  AS LONG
    LOCAL remaining  AS LONG
    LOCAL hours  AS LONG
    LOCAL minutes  AS LONG
    LOCAL seconds  AS LONG
    LOCAL alarmTime  AS LONG
    LOCAL alarmTriggered  AS LONG
    LOCAL elapsed  AS LONG
    LOCAL displaySec  AS LONG
    LOCAL phase AS LONG
    LOCAL timerValues(60) AS LONG
    LOCAL numPhases AS LONG

    PRINT "=== Countdown Timer ==="
    PRINT

    totalSeconds = 3665
    alarmTime = 0
    alarmTriggered = 0
    PRINT "Countdown started from " & STR$(totalSeconds) & " seconds"
    PRINT

    remaining = totalSeconds
    numPhases = 5

    timerValues(0) = 3665
    timerValues(1) = 3000
    timerValues(2) = 2000
    timerValues(3) = 1000
    timerValues(4) = 500
    timerValues(5) = 100
    timerValues(6) = 60
    timerValues(7) = 30
    timerValues(8) = 10
    timerValues(9) = 5
    timerValues(10) = 4
    timerValues(11) = 3
    timerValues(12) = 2
    timerValues(13) = 1
    timerValues(14) = 0

    PRINT "--- Timer Stages ---"
    FOR i = 0 TO 14
        remaining = timerValues(i)
        hours = remaining / 3600
        minutes = (remaining MOD 3600) / 60
        seconds = remaining MOD 60

        IF remaining > 3600 THEN
            PRINT "  " & FORMAT$(hours) & "h " & FORMAT$(minutes) & "m " & FORMAT$(seconds) & "s  [Running]"
        ELSE
            IF remaining > 60 THEN
                PRINT "  " & FORMAT$(minutes) & "m " & FORMAT$(seconds) & "s  [Running]"
            ELSE
                IF remaining > 10 THEN
                    PRINT "  " & FORMAT$(seconds) & "s  [Running]"
                ELSE
                    IF remaining > 0 THEN
                        PRINT "  " & FORMAT$(seconds) & "s  [CRITICAL]"
                    ELSE
                        PRINT "  " & FORMAT$(remaining) & "s  [ALARM: Time's up!]"
                        alarmTriggered = 1
                    END IF
                END IF
            END IF
        END IF
    NEXT i
    PRINT

    IF alarmTriggered = 1 THEN
        PRINT "*** ALARM TRIGGERED ***"
        PRINT "  Timer has reached zero."
    END IF
    PRINT

    PRINT "--- Timer Controls ---"
    PRINT "  Start: " & STR$(totalSeconds) & " seconds"
    PRINT "  Pause: Holds current value"
    PRINT "  Resume: Continues from held value"
    PRINT "  Reset: Returns to " & STR$(totalSeconds) & " seconds"
    PRINT

    PRINT "--- Format Tests ---"
    PRINT "  0 seconds = " & FORMAT$(0) & "s"
    PRINT "  60 seconds = " & FORMAT$(1) & "m 0s"
    PRINT "  3600 seconds = " & FORMAT$(1) & "h 0m 0s"
    PRINT "  86400 seconds = " & FORMAT$(24) & "h 0m 0s"
    PRINT

    PRINT "--- Countdown Use Cases ---"
    PRINT "  Pomodoro: 25 min work, 5 min break"
    PRINT "  Timer " & STR$(25 * 60) & "s work, " & STR$(5 * 60) & "s break"
    PRINT
    PRINT "  Egg timer: 3 min (180s)"
    PRINT "  Cooking: " & STR$(3 * 60) & " seconds standard"
    PRINT
    PRINT "  Presentation: 10 min (600s)"
    PRINT "  Speech: " & STR$(10 * 60) & " seconds typical limit"

    PRINT
    PRINT "Countdown timer complete."

    FUNCTION = 0
END FUNCTION
