#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL watch_file AS STRING
    LOCAL prev_size AS LONG
    LOCAL current_size AS LONG
    LOCAL prev_time AS DOUBLE
    LOCAL current_time AS DOUBLE
    LOCAL file_open AS LONG
    LOCAL i AS LONG
    LOCAL change_count AS LONG
    LOCAL temp_str AS STRING

    ' File change monitor: poll file size and timestamp
    ' Uses simulated file data since we can't open real files

    PRINT "File Change Monitor (Simulated)"
    PRINT "================================"
    PRINT ""

    watch_file = "test_data.txt"
    prev_size = 1000
    prev_time = 50000.0
    change_count = 0

    PRINT "Watching: " + watch_file
    PRINT "Initial size: " + STR$(prev_size) + " bytes"
    PRINT ""

    ' Simulate 5 poll cycles with changes
    i = 0
    WHILE i < 5
        current_size = prev_size + i * 250
        current_time = prev_time + i * 1000.0

        temp_str = STR$(current_size)
        PRINT "Poll " + STR$(i + 1) + ": size=" + temp_str + " bytes"

        IF current_size <> prev_size THEN
            change_count = change_count + 1
            temp_str = STR$(current_size - prev_size)
            IF current_size > prev_size THEN
                PRINT "  CHANGE: File grew by " + temp_str + " bytes"
            ELSE
                temp_str = STR$(prev_size - current_size)
                PRINT "  CHANGE: File shrank by " + temp_str + " bytes"
            END IF

            ' Check growth rate
            IF current_size - prev_size > 500 THEN
                PRINT "  WARNING: Rapid growth detected"
            END IF
            prev_size = current_size
        END IF

        IF current_time <> prev_time THEN
            temp_str = STR$(current_time - prev_time)
            PRINT "  Timestamp changed (+" + temp_str + " ms)"
            prev_time = current_time
        END IF

        PRINT ""
        i = i + 1
    WEND

    ' Simulate file deletion
    PRINT "Simulating file deletion..."
    current_size = -1
    IF current_size = -1 THEN
        change_count = change_count + 1
        PRINT "  ALERT: File no longer accessible"
    END IF

    ' Simulate file recreation
    PRINT ""
    PRINT "File recreated:"
    current_size = 500
    prev_size = -1
    IF prev_size = -1 AND current_size > 0 THEN
        change_count = change_count + 1
        PRINT "  File was recreated with " + STR$(current_size) + " bytes"
    END IF

    ' File size categories
    PRINT ""
    PRINT "File size analysis:"
    current_size = 1500
    IF current_size < 1024 THEN
        PRINT "  Size: " + STR$(current_size) + " bytes (< 1 KB)"
    ELSEIF current_size < 1048576 THEN
        temp_str = STR$(current_size / 1024)
        PRINT "  Size: " + temp_str + " KB"
    ELSE
        temp_str = STR$(current_size / 1048576)
        PRINT "  Size: " + temp_str + " MB"
    END IF

    ' Rate of change monitoring
    PRINT ""
    PRINT "Change rate monitoring:"
    prev_size = 100
    current_size = 350
    temp_str = STR$(current_size - prev_size)
    PRINT "  Change: +" + temp_str + " bytes in 1 second"
    temp_str = STR$((current_size - prev_size) * 60)
    PRINT "  Projected: " + temp_str + " bytes/minute"

    PRINT ""
    temp_str = STR$(change_count)
    PRINT "Total changes detected: " + temp_str
    PRINT "File watcher demo complete."

    FUNCTION = 0
END FUNCTION
