#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL k AS LONG
    LOCAL char_idx AS LONG
    LOCAL current_text AS STRING
    LOCAL current_line AS STRING
    LOCAL current_char AS STRING
    LOCAL c AS LONG
    LOCAL delay_val AS LONG
    LOCAL char_speed AS LONG
    LOCAL line_idx AS LONG
    LOCAL char_per_line AS LONG
    LOCAL pos AS LONG
    LOCAL word_count AS LONG
    LOCAL word_len AS LONG
    LOCAL word_start AS LONG
    LOCAL blink_count AS LONG
    LOCAL blink_state AS LONG
    LOCAL spaces_str AS STRING
    LOCAL temp_str AS STRING

    PRINT "=== Typewriter Text Effect Demo ==="
    PRINT "Each character appears one at a time"
    PRINT "with configurable speed per line."
    PRINT ""

    line_idx = 0
    WHILE line_idx < 7
        IF line_idx = 0 THEN current_text = "The quick brown fox jumps over the lazy dog."
        IF line_idx = 1 THEN current_text = "This is a demo of the typewriter text effect."
        IF line_idx = 2 THEN current_text = "Each line can have its own typing speed."
        IF line_idx = 3 THEN current_text = "SLOW deliberate text effect with pauses."
        IF line_idx = 4 THEN current_text = "Fast typing for quick passages right here!"
        IF line_idx = 5 THEN current_text = "Variable speed within a line is possible."
        IF line_idx = 6 THEN current_text = "=== END OF TYPEWRITER DEMO ==="

        IF line_idx = 0 THEN char_speed = 50
        IF line_idx = 1 THEN char_speed = 30
        IF line_idx = 2 THEN char_speed = 20
        IF line_idx = 3 THEN char_speed = 150
        IF line_idx = 4 THEN char_speed = 10
        IF line_idx = 5 THEN char_speed = 40
        IF line_idx = 6 THEN char_speed = 60

        char_per_line = LEN(current_text)
        current_line = ""
        char_idx = 1

        WHILE char_idx <= char_per_line
            current_char = MID$(current_text, char_idx, 1)
            current_line = current_line + current_char

            c = ASC(current_char)
            IF c = 46 THEN
                delay_val = char_speed * 3
            ELSEIF c = 44 THEN
                delay_val = char_speed * 2
            ELSEIF c = 32 THEN
                delay_val = char_speed / 2
            ELSE
                delay_val = char_speed
            END IF

            i = 0
            WHILE i < delay_val * 100
                i = i + 1
            WEND

            char_idx = char_idx + 1
        WEND

        PRINT current_line

        IF line_idx < 6 THEN
            i = 0
            WHILE i < 60000
                i = i + 1
            WEND
        END IF

        line_idx = line_idx + 1
    WEND

    PRINT ""
    PRINT "=== Word-by-Word Mode ==="
    PRINT ""

    current_text = "This text appears one word at a time for easier reading."
    word_count = 1
    word_start = 1
    pos = 1

    WHILE pos <= LEN(current_text)
        c = ASC(MID$(current_text, pos, 1))
        IF c = 32 THEN
            word_len = pos - word_start
            IF word_len > 0 THEN
                current_line = MID$(current_text, word_start, word_len)
                PRINT current_line + " "
            END IF
            word_start = pos + 1
            word_count = word_count + 1

            i = 0
            WHILE i < 30000
                i = i + 1
            WEND
        END IF
        pos = pos + 1
    WEND

    word_len = pos - word_start
    IF word_len > 0 THEN
        current_line = MID$(current_text, word_start, word_len)
        PRINT current_line + " "
    END IF

    PRINT ""
    PRINT ""
    PRINT "=== Blinking Cursor Effect ==="
    PRINT ""

    current_text = "Press any key to continue..."
    blink_count = 0
    blink_state = 0

    WHILE blink_count < 6
        IF blink_state = 0 THEN
            PRINT CHR$(13) + current_text + " _"
            blink_state = 1
        ELSE
            PRINT CHR$(13) + current_text + "  "
            blink_state = 0
        END IF

        i = 0
        WHILE i < 150000
            i = i + 1
        WEND

        blink_count = blink_count + 1
    WEND

    PRINT ""
    PRINT ""
    PRINT "=== Scrolling Marquee Text ==="
    PRINT ""

    current_text = ">>> SCROLLING MARQUEE TEXT <<<"
    j = 0
    WHILE j < 20
        spaces_str = ""
        k = 0
        WHILE k < j
            spaces_str = spaces_str + " "
            k = k + 1
        WEND
        PRINT spaces_str + current_text

        i = 0
        WHILE i < 16000
            i = i + 1
        WEND

        j = j + 1
    WEND

    PRINT ""
    PRINT ""
    temp_str = STR$(7)
    PRINT "Total lines typed: " + temp_str
    PRINT "Typewriter effect complete."

    FUNCTION = 0
END FUNCTION
