' Event scheduler: add events, check conflicts, filter by date
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    DIM evTitle(1 TO 6) AS STRING
    DIM evYear(1 TO 6) AS INTEGER
    DIM evMonth(1 TO 6) AS INTEGER
    DIM evDay(1 TO 6) AS INTEGER
    DIM evHour(1 TO 6) AS INTEGER
    DIM evMin(1 TO 6) AS INTEGER
    DIM evDur(1 TO 6) AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL n AS LONG
    LOCAL s1 AS LONG
    LOCAL e1e AS LONG
    LOCAL s2 AS LONG
    LOCAL e2e AS LONG
    LOCAL s AS STRING
    LOCAL t AS STRING
    LOCAL y AS STRING
    LOCAL mo AS STRING
    LOCAL d AS STRING
    LOCAL h AS STRING
    LOCAL mi AS STRING
    LOCAL du AS STRING

    PRINT "=== Event Scheduler ==="
    PRINT

    n = 6
    evTitle(1) = "Team Standup" + SPACE$(13)
    evYear(1) = 2024
    evMonth(1) = 7
    evDay(1) = 4
    evHour(1) = 9
    evMin(1) = 0
    evDur(1) = 30
    evTitle(2) = "Lunch Break" + SPACE$(14)
    evYear(2) = 2024
    evMonth(2) = 7
    evDay(2) = 4
    evHour(2) = 12
    evMin(2) = 0
    evDur(2) = 60
    evTitle(3) = "Code Review" + SPACE$(14)
    evYear(3) = 2024
    evMonth(3) = 7
    evDay(3) = 4
    evHour(3) = 14
    evMin(3) = 0
    evDur(3) = 60
    evTitle(4) = "Sprint Planning" + SPACE$(11)
    evYear(4) = 2024
    evMonth(4) = 7
    evDay(4) = 4
    evHour(4) = 9
    evMin(4) = 15
    evDur(4) = 120
    evTitle(5) = "Project Meeting" + SPACE$(11)
    evYear(5) = 2024
    evMonth(5) = 7
    evDay(5) = 5
    evHour(5) = 10
    evMin(5) = 0
    evDur(5) = 90
    evTitle(6) = "Daily Sync" + SPACE$(15)
    evYear(6) = 2024
    evMonth(6) = 7
    evDay(6) = 4
    evHour(6) = 11
    evMin(6) = 0
    evDur(6) = 15

    PRINT "Schedule (" & FORMAT$(n) & " events):"
    FOR i = 1 TO n
        t = TRIM$(evTitle(i))
        y = FORMAT$(evYear(i))
        mo = RIGHT$("00" & FORMAT$(evMonth(i)), 2)
        d = RIGHT$("00" & FORMAT$(evDay(i)), 2)
        h = RIGHT$("00" & FORMAT$(evHour(i)), 2)
        mi = RIGHT$("00" & FORMAT$(evMin(i)), 2)
        du = FORMAT$(evDur(i))
        s = "[" & FORMAT$(i) & "] " & t
        s = s & " on " & y & "-" & mo & "-" & d
        s = s & " at " & h & ":" & mi
        s = s & " (" & du & "min)"
        PRINT "  " & s
    NEXT i
    PRINT

    PRINT "Conflict detection:"
    FOR i = 1 TO n - 1
        FOR j = i + 1 TO n
            IF evYear(i) = evYear(j) THEN
                IF evMonth(i) = evMonth(j) THEN
                    IF evDay(i) = evDay(j) THEN
                        s1 = evHour(i) * 60 + evMin(i)
                        e1e = s1 + evDur(i)
                        s2 = evHour(j) * 60 + evMin(j)
                        e2e = s2 + evDur(j)
                        IF s1 < e2e AND s2 < e1e THEN
                            s = "  CONFLICT: " & CHR$(34) & TRIM$(evTitle(i)) & CHR$(34) & " vs " & CHR$(34) & TRIM$(evTitle(j)) & CHR$(34)
                            PRINT s
                        END IF
                    END IF
                END IF
            END IF
        NEXT j
    NEXT i
    PRINT

    PRINT "Events on 2024-07-04:"
    FOR j = 1 TO n
        IF evYear(j) = 2024 THEN
            IF evMonth(j) = 7 THEN
                IF evDay(j) = 4 THEN
                    t = TRIM$(evTitle(j))
                    y = FORMAT$(evYear(j))
                    mo = RIGHT$("00" & FORMAT$(evMonth(j)), 2)
                    d = RIGHT$("00" & FORMAT$(evDay(j)), 2)
                    h = RIGHT$("00" & FORMAT$(evHour(j)), 2)
                    mi = RIGHT$("00" & FORMAT$(evMin(j)), 2)
                    du = FORMAT$(evDur(j))
                    s = "[" & FORMAT$(j) & "] " & t
                    s = s & " on " & y & "-" & mo & "-" & d
                    s = s & " at " & h & ":" & mi
                    s = s & " (" & du & "min)"
                    PRINT "  " & s
                END IF
            END IF
        END IF
    NEXT j
    PRINT

    PRINT "Scheduler tests complete."
    FUNCTION = 0
END FUNCTION
