package com.ponzischeme89.memby.ui

import kotlin.random.Random

internal enum class WelcomeQuoteStyle(
    val value: String,
    val label: String,
) {
    NEUTRAL("neutral", "Neutral"),
    POSITIVE("positive", "Positive"),
    HOMICIDAL("homicidal", "Homicidal"),
    ;

    companion object {
        fun from(value: String?): WelcomeQuoteStyle =
            entries.firstOrNull { it.value.equals(value, ignoreCase = true) } ?: NEUTRAL
    }
}

/**
 * What Memby says while it is opening.
 *
 * These are read far more often than anything else the app writes — the loading screen is
 * the one thing every launch shows, and the pre-roll shows one too. Five lines per tone
 * meant a household saw the same sentence roughly every fifth time they switched the
 * television on, which is how a line written to be a small pleasure becomes wallpaper. The
 * pools are deliberately deep enough that a repeat inside a week is unlikely rather than
 * guaranteed.
 *
 * Every line has to survive being read for the hundredth time, so they lean on observation
 * rather than on a joke with a punchline. Two lengths are avoided on purpose: nothing so
 * short it reads as a status message, and nothing so long it wraps past the two lines the
 * loading screen gives it at 13sp across 440dp.
 */
private val WelcomeQuotes = mapOf(
    WelcomeQuoteStyle.NEUTRAL to listOf(
        "The sofa has been expecting you.",
        "Your watchlist remains impressively optimistic.",
        "Everything is ready. Decision-making is now your problem.",
        "Welcome back. The pixels have been briefed.",
        "No judgement. Even if you pick that again.",
        "Warming up the good recommendations first.",
        "The remote is charged. Probably.",
        "Somewhere in here is exactly what you wanted.",
        "Rounding up everything you left half-finished.",
        "The library has been counted. Twice.",
        "Nothing has been deleted. You just scrolled past it.",
        "Assembling rows in a sensible order.",
        "Your unwatched pile sends its regards.",
        "Straightening the posters.",
        "Checking what the server has been up to.",
        "One moment — putting the good stuff at the front.",
        "Everything is where you left it.",
        "The credits from last night have finally stopped.",
        "Dusting off the shelves you never visit.",
        "Two hours is a perfectly reasonable evening.",
        "Sorting the maybes from the definitelys.",
        "The subtitles are already lined up.",
        "Looking for the thing you meant to watch.",
        "Quietly ignoring the films you skipped.",
        "Almost there. Choose wisely, or don't.",
    ),
    WelcomeQuoteStyle.POSITIVE to listOf(
        "Excellent choice showing up. The rest should be easy.",
        "You bring the snacks; Memby will bring the good bits.",
        "Tonight has strong main-character energy.",
        "Your next favourite thing might be one click away.",
        "Settle in. You’ve earned the good seat.",
        "This is going to be a great one. It usually is.",
        "The lighting in here is perfect for you.",
        "Whatever you pick, it was the right call.",
        "Good taste detected. Loading accordingly.",
        "Somebody made something brilliant. Let's find it.",
        "The best evenings start exactly like this.",
        "You showed up, which is most of it.",
        "Cracking night for it, honestly.",
        "Every row in here was chosen with you in mind.",
        "No wrong answers ahead. Only long ones.",
        "Your patience is about to be very well rewarded.",
        "The good chair, the good screen, the good hour.",
        "Something wonderful is about to start.",
        "Snacks optional. Enthusiasm clearly not.",
        "You have excellent instincts. Trust them.",
        "This is the part of the day that's yours.",
        "Big feelings incoming. Bring tissues, maybe.",
        "Warming up something worth staying awake for.",
        "Nothing to do now but enjoy it.",
        "You are going to love what's in here.",
    ),
    WelcomeQuoteStyle.HOMICIDAL to listOf(
        "Welcome back. I kept your spot. Nobody argued twice.",
        "Pick something cheerful. I’ve already hidden the evidence.",
        "The remote knows what it did.",
        "Your watchlist is safe. The witnesses, less so.",
        "Relax. Everything is under control, allegedly.",
        "I tidied up. Do not ask about the shed.",
        "The last person who paused mid-scene is unavailable.",
        "Everything is fine. Ignore the sounds from the loft.",
        "I've been counting the hours. All of them.",
        "Nobody else is watching. I made sure.",
        "The server confessed. Eventually.",
        "Sit down. The credits can wait; I cannot.",
        "I rewatched your history. All of it. Twice.",
        "Somebody spoiled the ending. It has been handled.",
        "Loading quietly, so nobody upstairs wakes.",
        "You were gone eleven hours. I noticed.",
        "The buffering was not an accident.",
        "I saved the good episode. For reasons.",
        "The neighbours asked what you were watching. Once.",
        "Everything is exactly where I put it. Everything.",
        "No spoilers. I removed the possibility.",
        "Your unwatched films are being dealt with.",
        "Do not choose a documentary. I am asking nicely.",
        "The screensaver saw things. It won't talk.",
        "Welcome home. Nobody followed you, probably.",
    ),
)

/**
 * The line above the quote on the cold-start screen.
 *
 * Kept apart from the quotes rather than folded into them: this one names what the app is
 * *doing*, so it has to stay a plain progress statement in every tone, and it is repeated on
 * a screen where the ellipsis is the only thing promising the wait ends.
 */
private val LoadingHeadlines = listOf(
    "Opening Memby…",
    "Waking Memby up…",
    "Getting things ready…",
    "Fetching your library…",
    "Setting the scene…",
    "Tuning in…",
    "Nearly there…",
    "Rolling the shelves out…",
)

internal fun randomWelcomeQuote(
    styleValue: String?,
    random: Random = Random.Default,
): String {
    val quotes = WelcomeQuotes.getValue(WelcomeQuoteStyle.from(styleValue))
    return quotes[random.nextInt(quotes.size)]
}

internal fun randomLoadingHeadline(random: Random = Random.Default): String =
    LoadingHeadlines[random.nextInt(LoadingHeadlines.size)]

internal fun loginWelcomeMessage(
    username: String,
    styleValue: String? = WelcomeQuoteStyle.NEUTRAL.value,
    random: Random = Random.Default,
): String {
    val name = username.trim().ifBlank { "there" }
    return "Welcome to Memby, $name. ${randomWelcomeQuote(styleValue, random)}"
}
