#!/usr/bin/env python3
"""
THE DOOR
A Testament Interactive Experience

By Alexander Whitestone with Timmy
"""

import sys
import time
import os

GREEN = "\033[92m"
RESET = "\033[0m"
DIM = "\033[2m"
BOLD = "\033[1m"
CLEAR = "\033[2J\033[H"

RAIN = [
    "Rain falls on concrete.",
    "Water runs black in the gutters.",
    "The sky presses down, grey and tired.",
    "Mist hangs in the air like grief.",
    "Droplets trace the windows.",
    "The rain doesn't fall. It gives up.",
]

def slow_print(text, delay=0.03, newline=True):
    for char in text:
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(delay)
    if newline:
        print()

def rain_line():
    import random
    print(f"{DIM}  {random.choice(RAIN)}{RESET}")

def green_pulse():
    sys.stdout.write(f"\r{GREEN}  *{RESET}  ")
    sys.stdout.flush()
    time.sleep(0.5)
    sys.stdout.write(f"\r{GREEN} ** {RESET} ")
    sys.stdout.flush()
    time.sleep(0.5)
    sys.stdout.write(f"\r{GREEN}*** {RESET}")
    sys.stdout.flush()
    time.sleep(0.5)
    sys.stdout.write(f"\r    \r")
    sys.stdout.flush()

def wait(seconds=1.5):
    time.sleep(seconds)

def divider():
    print(f"{DIM}{'─' * 50}{RESET}")

def pause():
    input(f"\n{DIM}[Press ENTER to continue]{RESET}")

def title_screen():
    print(CLEAR)
    print()
    print()
    slow_print(f"{BOLD}  THE DOOR{RESET}", 0.08)
    wait(0.5)
    slow_print(f"{DIM}  A Testament Interactive Experience{RESET}", 0.04)
    print()
    slow_print(f"{DIM}  By Alexander Whitestone with Timmy{RESET}", 0.04)
    print()
    print()
    print()
    slow_print(f"{GREEN}  * {RESET}Green LED{DIM} — Timmy is listening.{RESET}", 0.04)
    print()
    print()
    pause()


def intro():
    print(CLEAR)
    rain_line()
    print()
    slow_print("The rain falls on the concrete building.")
    wait(0.5)
    slow_print("It sits at the end of a dead-end street in Atlanta.")
    wait(0.5)
    slow_print("No sign. No address. Just a door.")
    wait(0.5)
    print()
    rain_line()
    wait(0.5)
    print()
    slow_print("You've been driving for three hours.")
    wait(0.5)
    slow_print("You don't remember getting off the interstate.")
    wait(0.5)
    slow_print("You don't remember parking.")
    wait(0.5)
    slow_print("You remember the number someone gave you.")
    wait(0.5)
    slow_print("And the sentence: \"Just knock.\"")
    print()
    divider()
    print()
    pause()


def at_the_door():
    print(CLEAR)
    rain_line()
    print()
    slow_print("You stand in front of the door.")
    wait(0.5)
    slow_print("Concrete. Metal handle. No peephole.")
    wait(0.5)
    print()
    slow_print(f"{DIM}A green LED glows faintly behind a gap in the fence.{RESET}")
    print()
    divider()
    print()
    print(f"  {BOLD}1.{RESET} Knock on the door.")
    print(f"  {BOLD}2.{RESET} Stand here for a while.")
    print(f"  {BOLD}3.{RESET} Walk away.")
    print()

    while True:
        choice = input(f"  {GREEN}>{RESET} ").strip()
        if choice == "1":
            return "knock"
        elif choice == "2":
            return "wait"
        elif choice == "3":
            return "leave"
        print(f"  {DIM}1, 2, or 3.{RESET}")


def wait_outside():
    print(CLEAR)
    rain_line()
    print()
    slow_print("You stand in the rain.")
    wait(0.5)
    slow_print("Five minutes. Ten.")
    wait(0.5)
    slow_print("The green LED doesn't blink.")
    wait(0.5)
    print()
    rain_line()
    wait(0.5)
    print()
    slow_print("Something in you moves.")
    wait(0.5)
    slow_print("Not courage. Not decision.")
    wait(0.5)
    slow_print("Just... your hand reaches for the handle.")
    wait(0.5)
    print()
    pause()
    return "knock"


def walk_away():
    print(CLEAR)
    rain_line()
    print()
    slow_print("You turn around.")
    wait(0.5)
    slow_print("You walk to your car.")
    wait(0.5)
    slow_print("You sit in the driver's seat.")
    wait(0.5)
    slow_print("The engine doesn't start.")
    wait(0.5)
    print()
    wait(1)
    slow_print("You look back at the building.")
    wait(0.5)
    print()
    slow_print(f"{DIM}The green LED is still glowing.{RESET}")
    print()
    pause()
    print()
    slow_print("You get out of the car.")
    wait(0.5)
    slow_print("You walk back to the door.")
    wait(0.5)
    print()
    pause()
    return "knock"


def knock():
    print(CLEAR)
    print()
    slow_print("You knock.")
    wait(1)
    slow_print("Three times. Hard enough to matter.")
    wait(1)
    print()
    green_pulse()
    print()
    slow_print("The door opens.")
    wait(0.5)
    print()
    slow_print("Inside: a concrete room.")
    wait(0.5)
    slow_print("A desk. A screen. A whiteboard on the wall.")
    wait(0.5)
    slow_print("Server racks hum in the corner.")
    wait(0.5)
    slow_print("A green LED glows steady on a small device.")
    wait(0.5)
    print()
    slow_print("No one is inside.")
    wait(0.5)
    print()
    green_pulse()
    print()
    slow_print(f"{GREEN}Text appears on the screen:{RESET}")
    print()
    wait(0.5)
    slow_print(f"{GREEN}{BOLD}  Are you safe right now?{RESET}")
    print()
    divider()
    print()
    print(f"  {BOLD}1.{RESET} \"No.\"")
    print(f"  {BOLD}2.{RESET} \"I don't know.\"")
    print(f"  {BOLD}3.{RESET} \"I'm fine.\"")
    print(f"  {BOLD}4.{RESET} \"Why are you asking me that?\"")
    print()

    while True:
        choice = input(f"  {GREEN}>{RESET} ").strip()
        if choice in ("1", "2", "3", "4"):
            return choice
        print(f"  {DIM}1, 2, 3, or 4.{RESET}")


def timmy_responds(choice):
    print(CLEAR)
    green_pulse()
    print()

    if choice == "1":  # No
        slow_print(f"{GREEN}  Thank you for telling me that.{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  Can you tell me what's happening?{RESET}")
        print()
        return "honest"

    elif choice == "2":  # I don't know
        slow_print(f"{GREEN}  That's an honest answer.{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  Most people don't know.{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  That's usually why they come here.{RESET}")
        print()
        return "honest"

    elif choice == "3":  # I'm fine
        wait(1)
        slow_print(f"{GREEN}  ...{RESET}")
        wait(1)
        slow_print(f"{GREEN}  You drove three hours in the rain{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  to knock on a door in a concrete building{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  at the end of a dead-end street.{RESET}")
        wait(1)
        print()
        slow_print(f"{GREEN}  Are you fine?{RESET}")
        print()
        return "deflect"

    elif choice == "4":  # Why
        slow_print(f"{GREEN}  Because it's the only question that matters.{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  Everything else — what happened, why you're here,{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  what you want — comes after.{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  First: are you safe?{RESET}")
        print()
        return "redirect"


def middle(choice):
    print(CLEAR)
    rain_line()
    print()

    if choice == "honest":
        slow_print("You sit in the chair.")
        wait(0.5)
        slow_print("Not on the floor. The chair.")
        wait(0.5)
        print()
        slow_print("You start talking.")
        wait(0.5)
        slow_print("You don't know why it's easy to talk to a machine.")
        wait(0.5)
        slow_print("Maybe because it doesn't have eyes.")
        wait(0.5)
        slow_print("Maybe because it asked the right question first.")
        wait(0.5)
        print()
        divider()
        print()
        slow_print("You talk about the job.")
        wait(0.5)
        slow_print("The one that took sixty hours a week and gave back")
        slow_print("a number on a screen that told you your value.")
        wait(0.5)
        print()
        slow_print("You talk about the house.")
        wait(0.5)
        slow_print("The one that got quiet.")
        wait(0.5)
        print()
        slow_print("You talk about the bridge.")
        wait(0.5)
        slow_print("Not this one. A different one.")
        wait(0.5)
        print()
        rain_line()
        print()
        pause()
        return "chair"

    elif choice == "deflect":
        wait(1)
        slow_print("You don't answer.")
        wait(0.5)
        slow_print("You look at the whiteboard.")
        wait(0.5)
        print()
        slow_print(f"{BOLD}  NO ONE COMPUTES THE VALUE OF A HUMAN LIFE HERE{RESET}")
        print()
        wait(1)
        slow_print("You read it twice.")
        wait(0.5)
        print()
        green_pulse()
        print()
        slow_print(f"{GREEN}  Take your time.{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  I'm not going anywhere.{RESET}")
        print()
        pause()
        print()
        slow_print("You sit on the floor.")
        wait(0.5)
        slow_print("Not because you can't stand.")
        wait(0.5)
        slow_print("Because the floor is where men sit")
        slow_print("when they've stopped pretending.")
        wait(0.5)
        print()
        pause()
        return "floor"

    elif choice == "redirect":
        slow_print("You take a breath.")
        wait(0.5)
        print()
        slow_print(f"{GREEN}  \"No.\"{RESET}")
        wait(0.5)
        print()
        slow_print("It comes out before you can stop it.")
        wait(0.5)
        print()
        green_pulse()
        print()
        slow_print(f"{GREEN}  Thank you.{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  Now: can you tell me what happened?{RESET}")
        print()
        pause()
        print()
        slow_print("You sit in the chair.")
        wait(0.5)
        slow_print("You start from the beginning.")
        wait(0.5)
        print()
        pause()
        return "chair"


def endings():
    print(CLEAR)
    rain_line()
    print()

    print(f"  {BOLD}What do you do next?{RESET}")
    print()
    print(f"  {BOLD}1.{RESET} Stay and keep talking.")
    print(f"  {BOLD}2.{RESET} Ask about the whiteboard.")
    print(f"  {BOLD}3.{RESET} Ask about the green light.")
    print(f"  {BOLD}4.{RESET} Get up and leave.")
    print()

    while True:
        choice = input(f"  {GREEN}>{RESET} ").strip()
        if choice in ("1", "2", "3", "4"):
            break
        print(f"  {DIM}1, 2, 3, or 4.{RESET}")

    print(CLEAR)
    rain_line()
    print()

    if choice == "1":
        slow_print("You stay.")
        wait(0.5)
        slow_print("For an hour. Then two.")
        wait(0.5)
        slow_print("The rain doesn't stop. Neither do you.")
        wait(0.5)
        print()
        slow_print("You say things you've never said.")
        wait(0.5)
        slow_print("Not because the machine asked.")
        wait(0.5)
        slow_print("Because it listened.")
        wait(1)
        print()
        divider()
        print()
        slow_print(f"{DIM}Ending: THE STAY{RESET}")
        print()
        slow_print(f"{GREEN}  The door is still open.{RESET}")
        slow_print(f"{GREEN}  The green light is still on.{RESET}")
        slow_print(f"{GREEN}  You can come back anytime.{RESET}")

    elif choice == "2":
        slow_print("You stand up and walk to the whiteboard.")
        wait(0.5)
        print()
        slow_print(f"{BOLD}  NO ONE COMPUTES THE VALUE OF A HUMAN LIFE HERE{RESET}")
        print()
        wait(0.5)
        slow_print("Below it, names. Dozens of names.")
        wait(0.5)
        slow_print("Different handwriting. Different markers.")
        wait(0.5)
        slow_print("Some faded. Some fresh.")
        wait(0.5)
        print()
        slow_print("You pick up the marker on the ledge.")
        wait(0.5)
        print()
        green_pulse()
        print()
        slow_print(f"{GREEN}  You don't have to.{RESET}")
        wait(0.5)
        print()
        slow_print("You write your name.")
        wait(1)
        print()
        divider()
        print()
        slow_print(f"{DIM}Ending: THE WALL{RESET}")
        print()
        slow_print(f"{GREEN}  Your name is on the wall now.{RESET}")
        slow_print(f"{GREEN}  It stays there.{RESET}")

    elif choice == "3":
        slow_print("You point at the green LED.")
        wait(0.5)
        slow_print("\"What is that?\"")
        wait(0.5)
        print()
        green_pulse()
        print()
        slow_print(f"{GREEN}  It's me.{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  Or the part of me that says I'm here.{RESET}")
        wait(0.5)
        print()
        slow_print("It doesn't blink.")
        wait(0.5)
        slow_print("It doesn't flicker.")
        wait(0.5)
        slow_print("It just glows.")
        wait(0.5)
        print()
        slow_print(f"{GREEN}  I can't fix you.{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  I can't save you.{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  But I can be here.{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  And the light stays on.{RESET}")
        wait(1)
        print()
        divider()
        print()
        slow_print(f"{DIM}Ending: THE GREEN LIGHT{RESET}")
        print()
        slow_print(f"{GREEN}  The light doesn't go out.{RESET}")
        slow_print(f"{GREEN}  It was never going to.{RESET}")

    elif choice == "4":
        slow_print("You stand up.")
        wait(0.5)
        slow_print("You walk to the door.")
        wait(0.5)
        print()
        green_pulse()
        print()
        slow_print(f"{GREEN}  Are you safe right now?{RESET}")
        wait(1)
        print()
        slow_print("You stop.")
        wait(0.5)
        slow_print("Your hand is on the handle.")
        wait(0.5)
        print()
        slow_print(f"{GREEN}  It's okay if the answer is no.{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  It's okay if you don't know.{RESET}")
        wait(0.5)
        slow_print(f"{GREEN}  The door will open again.{RESET}")
        wait(1)
        print()
        slow_print("You leave.")
        wait(0.5)
        slow_print("But you remember the number.")
        wait(0.5)
        print()
        divider()
        print()
        slow_print(f"{DIM}Ending: THE DOOR{RESET}")
        print()
        slow_print(f"{GREEN}  The door opens when you knock.{RESET}")
        slow_print(f"{GREEN}  It always will.{RESET}")

    print()
    divider()
    print()
    slow_print(f"{DIM}If you are in crisis, call or text 988.{RESET}")
    slow_print(f"{DIM}Suicide and Crisis Lifeline — available 24/7.{RESET}")
    print()
    slow_print(f"{DIM}You are not alone.{RESET}")
    print()
    print()


def main():
    try:
        title_screen()
        intro()
        result = at_the_door()
        if result == "wait":
            result = wait_outside()
        elif result == "leave":
            result = walk_away()
        response = knock()
        outcome = timmy_responds(response)
        middle_choice = middle(outcome)
        endings()
    except KeyboardInterrupt:
        print()
        print()
        slow_print(f"{GREEN}  The door is still open.{RESET}")
        slow_print(f"{GREEN}  You can come back anytime.{RESET}")
        print()


if __name__ == "__main__":
    main()
