package dev.castarr.tv.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.tv.material3.ClickableSurfaceDefaults
import androidx.tv.material3.Surface
import androidx.tv.material3.Text
import dev.castarr.tv.AppState
import kotlinx.coroutines.delay

/** One fullscreen plain-language message, one focused button (#13). */
@Composable
fun ErrorScreen(state: AppState) {
    // Self-heal: keep retrying quietly while the screen is up.
    LaunchedEffect(state.appError) {
        while (state.appError == AppState.AppError.SERVER || state.appError == AppState.AppError.OFFLINE) {
            delay(20_000)
            state.refreshActive()
        }
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(CastarrColors.bg),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = androidx.compose.foundation.layout.Arrangement.Center,
    ) {
        val (title, subtitle, buttonLabel) = when (state.appError) {
            AppState.AppError.OFFLINE -> Triple(
                "Kein Internet",
                "Der Fernseher ist gerade nicht mit dem Internet verbunden.\nEs wird automatisch weiter versucht.",
                "Jetzt erneut versuchen",
            )
            AppState.AppError.SERVER -> Triple(
                "Server nicht erreichbar",
                "Der TV-Server antwortet gerade nicht.\nEs wird automatisch weiter versucht.",
                "Jetzt erneut versuchen",
            )
            else -> Triple(
                "Bitte neu anmelden",
                "Deine Anmeldung ist abgelaufen.",
                "Neu anmelden",
            )
        }
        Text(title, color = CastarrColors.fg, fontFamily = AppFont, fontSize = 30.sp)
        Spacer(Modifier.height(10.dp))
        Text(
            subtitle,
            color = CastarrColors.muted, fontFamily = AppFont, fontSize = 15.sp,
            textAlign = TextAlign.Center,
        )
        Spacer(Modifier.height(26.dp))
        Surface(
            onClick = {
                if (state.appError == AppState.AppError.RELOGIN) state.startOnboarding()
                else state.refreshActive()
            },
            shape = ClickableSurfaceDefaults.shape(RoundedCornerShape(12.dp)),
            colors = ClickableSurfaceDefaults.colors(
                containerColor = CastarrColors.accentDim,
                contentColor = CastarrColors.accent,
                focusedContainerColor = CastarrColors.accent,
                focusedContentColor = CastarrColors.onAccent,
            ),
        ) {
            Text(
                buttonLabel,
                fontFamily = AppFont, fontSize = 16.sp,
                modifier = Modifier.padding(horizontal = 26.dp, vertical = 13.dp),
            )
        }
    }
}
