import asyncio
from datetime import datetime

from rich.text import Text

from hibiscus.constants import (
    BABY_BLUE, VALENTINE_PINK, VALENTINE_ROSE,
    DIM_BLUE, DIM_TEXT, START_DATE,
    SONNET_116, ASCII_LOVEBIRDS, ASCII_HEART,
)
from hibiscus.widgets import colorize_sonnet_line


async def run_experience(app) -> None:
    await asyncio.sleep(0.8)
    await app._type_slow("Khaleeesii....")
    await asyncio.sleep(1.2)
    await app._type_fast("I have been thinking about what to say to you.")
    await app._pause(1.8)

    await app._type_fast("And I think I will just be honest.")
    await app._pause(1.6)

    await app._type_fast("I have known you for")
    await asyncio.sleep(0.4)

    counter = app._mount_counter()
    await app._scroll_down()

    now = datetime.now()
    real_seconds = int((now - START_DATE).total_seconds())

    steps = 60
    for i in range(steps + 1):
        progress = i / steps
        eased = 1 - (1 - progress) ** 3
        value = int(eased * real_seconds)
        counter.value = value
        delay = 0.015 if i < steps * 0.7 else 0.03 + (i - steps * 0.7) * 0.003
        await asyncio.sleep(delay)

    counter.value = real_seconds
    counter.live = True

    await app._type_slow("seconds.")
    await app._pause(2.5)

    await app._add_blank()

    memory_lines = [
        "Somewhere in all that time,",
        "there are moments that stayed with me.",
        "",
        "One of them was small.",
        "",
        "After the hackathon,",
        "when you stepped off the bus before me",
        "and said:",
        "",
    ]

    for line in memory_lines:
        if line == "":
            await app._add_blank()
            await asyncio.sleep(0.3)
        else:
            await app._type_fast(line)
            await asyncio.sleep(0.35)

    await app._type_slow("Bye Snave :)", BABY_BLUE)
    await asyncio.sleep(0.8)

    await app._add_blank()
    await app._type_fast("You probably did not think much of it.")
    await asyncio.sleep(0.5)
    await app._add_blank()
    await app._type_fast("But I remember how warm that moment felt.")

    await app._pause(3.0)

    await app._add_blank()

    transition_lines = [
        "I could try to describe what you mean to me.",
        "",
        "I could search for the right words,",
        "rewrite them,",
        "and try again.",
        "",
        "But love has existed longer than both of us,",
        "and there are people who have already said",
        "some things better than I ever could.",
        "",
        "So I will borrow something timeless.",
    ]

    for line in transition_lines:
        if line == "":
            await app._add_blank()
            await asyncio.sleep(0.25)
        else:
            await app._type_fast(line)
            await asyncio.sleep(0.3)

    await app._pause(1.5)
    await app._add_blank()
    await app._type_slow("Read slowly.", DIM_BLUE, style="italic")
    await app._pause(2.5)

    await app._add_blank()

    for line in ASCII_LOVEBIRDS:
        app._add_line(Text(line, style=BABY_BLUE), "ascii-line")
        await asyncio.sleep(0.07)
        await app._scroll_down()

    await app._pause(2.5)

    await app._add_blank()
    await app._type_slow("  Sonnet 116", DIM_BLUE)
    await asyncio.sleep(0.2)
    await app._type_fast("  William Shakespeare", DIM_TEXT)
    await app._add_blank()
    await asyncio.sleep(0.4)

    for i, line in enumerate(SONNET_116):
        await app._type_sonnet_line("  " + line, i)
        await asyncio.sleep(0.4)

    await app._pause(4.0)

    await app._add_blank()
    await app._type_fast("Every word of that was written centuries ago,")
    await asyncio.sleep(0.3)
    await app._type_fast("but it still feels like it was written about us.")
    await app._pause(2.0)
    await app._add_blank()
    await app._type_slow("You are my favorite person.", BABY_BLUE)
    await asyncio.sleep(0.6)
    await app._type_fast("And I mean that with everything I have.")
    await app._pause(2.0)
    await app._add_blank()
    await app._type_fast("If you were a flower,")
    await asyncio.sleep(0.4)
    await app._type_fast("you would be a hibiscus.")
    await asyncio.sleep(0.6)
    await app._type_slow("Red when you laugh,", VALENTINE_ROSE)
    await asyncio.sleep(0.4)
    await app._type_slow("pink when you blush,", VALENTINE_PINK)
    await asyncio.sleep(0.4)
    await app._type_slow("and blue like the calm you bring me.", BABY_BLUE)
    await app._pause(2.5)

    await app._add_blank()
    await app._add_blank()

    for line in ASCII_HEART:
        app._add_line(Text(line, style=f"bold {VALENTINE_ROSE}"), "valentine-line")
        await asyncio.sleep(0.06)
        await app._scroll_down()

    await app._add_blank()
    await asyncio.sleep(0.4)
    await app._type_slow("Will you be my Valentine?", VALENTINE_PINK)
    await asyncio.sleep(0.3)
    await app._type_fast("(type yes or no)", DIM_TEXT)

    await asyncio.sleep(0.6)

    max_attempts = 3
    for attempt in range(max_attempts):
        response = await app._ask_input()
        lower = response.strip().lower()

        echo = Text(f"> {response or '...'}", style="#808080")
        app._add_line(echo)
        await app._scroll_down()

        if lower == "yes":
            await asyncio.sleep(1.0)
            await app._add_blank()
            await app._type_slow("thats my baby :)", BABY_BLUE)
            await asyncio.sleep(0.6)
            await app._type_fast("You just made my whole year.")
            await asyncio.sleep(0.5)
            await app._add_blank()
            await app._type_slow("I am the luckiest person alive.")
            await asyncio.sleep(0.8)
            await app._add_blank()
            await app._type_slow("Happy Valentine's Day, Khaleesi.", VALENTINE_PINK)
            return

        if attempt == 0:
            await asyncio.sleep(0.8)
            await app._type_slow("ooh noo :(", VALENTINE_ROSE)
            await asyncio.sleep(1.2)
            await app._type_fast("let me ask again...")
            await asyncio.sleep(0.6)
            await app._add_blank()
            await app._type_slow("Will you be my Valentine?", VALENTINE_PINK)
            await asyncio.sleep(0.3)
            await app._type_fast("(type yes or no)", DIM_TEXT)
        elif attempt == 1:
            await asyncio.sleep(0.8)
            await app._type_slow("really? :(", VALENTINE_ROSE)
            await asyncio.sleep(1.2)
            await app._type_fast("one last time...")
            await asyncio.sleep(0.6)
            await app._add_blank()
            await app._type_slow("Will you be my Valentine?", VALENTINE_PINK)
            await asyncio.sleep(0.3)
            await app._type_fast("(type yes or no)", DIM_TEXT)

    await asyncio.sleep(1.2)
    await app._add_blank()
    await app._type_slow("ok...", DIM_TEXT)
    await asyncio.sleep(1.0)
    await app._type_fast("I will still be here though.")
    await asyncio.sleep(0.8)
    await app._add_blank()
    await app._type_slow("Happy Valentine's Day, Khaleesi.", VALENTINE_PINK)
