package dev.castarr.tv.ui

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
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.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.tv.material3.Text
import dev.castarr.tv.AppState
import dev.castarr.tv.pairing.Pairing
import dev.castarr.tv.pairing.Qr

/**
 * Phone-first onboarding (ADR-0007): one QR, everything else on the phone.
 * No text entry and no scrolling on the TV (Ten-Foot-Regel).
 */
@Composable
fun WelcomeScreen(state: AppState) {
    val context = LocalContext.current
    val address = remember { Pairing.lanAddress() }

    Box(Modifier.fillMaxSize().background(CastarrColors.bg)) {
        Row(
            modifier = Modifier
                .fillMaxSize()
                .padding(horizontal = 60.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Column(Modifier.weight(1f)) {
                Text(
                    "Willkommen bei Castarr",
                    color = CastarrColors.fg, fontFamily = AppFont,
                    fontSize = 32.sp, fontWeight = FontWeight.Medium,
                )
                Spacer(Modifier.height(28.dp))
                Step(1, "Code mit der Handy-Kamera scannen", state.welcomePhase == AppState.WelcomePhase.WAIT_PHONE, done = state.welcomePhase > AppState.WelcomePhase.WAIT_PHONE)
                Step(2, "Auf dem Handy: Server angeben und anmelden", state.welcomePhase == AppState.WelcomePhase.WAIT_URL || state.welcomePhase == AppState.WelcomePhase.WAIT_LOGIN, done = state.welcomePhase == AppState.WelcomePhase.DONE)
                Step(3, "Fertig — der Fernseher macht den Rest", state.welcomePhase == AppState.WelcomePhase.DONE, done = false)
                Spacer(Modifier.height(24.dp))
                when (state.welcomePhase) {
                    AppState.WelcomePhase.WAIT_PHONE -> Unit
                    AppState.WelcomePhase.WAIT_URL -> Status("Handy verbunden — warte auf den Server…")
                    AppState.WelcomePhase.WAIT_LOGIN -> Status(
                        if (state.welcomeUserCode.isEmpty()) "Warte auf die Anmeldung am Handy…"
                        else "Anmeldung am Handy bestätigen · Code ${state.welcomeUserCode}"
                    )
                    AppState.WelcomePhase.DONE -> Status("Angemeldet! Lade Sender…")
                }
            }

            Spacer(Modifier.width(48.dp))

            if (address != null) {
                Column(horizontalAlignment = Alignment.CenterHorizontally) {
                    Column(
                        Modifier
                            .clip(RoundedCornerShape(20.dp))
                            .background(Color(0xFFFBFCFD))
                            .padding(18.dp),
                    ) {
                        val qr = remember(address) {
                            Qr.encode(
                                Pairing.pairingUrl(context, address),
                                520,
                                android.graphics.Color.parseColor("#101216"),
                            )
                        }
                        Image(qr.asImageBitmap(), contentDescription = "Einrichtungs-QR", modifier = Modifier.size(230.dp))
                    }
                    Spacer(Modifier.height(12.dp))
                    Text(
                        Pairing.remoteUrl(address),
                        color = CastarrColors.faint, fontFamily = AppFont, fontSize = 13.sp,
                    )
                }
            } else {
                Text("Keine Netzwerkverbindung", color = CastarrColors.faint, fontFamily = AppFont, fontSize = 15.sp)
            }
        }
    }
}

@Composable
private fun Step(number: Int, text: String, active: Boolean, done: Boolean) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier.padding(vertical = 8.dp),
    ) {
        Box(
            Modifier
                .size(30.dp)
                .clip(CircleShape)
                .background(if (active || done) CastarrColors.accent else CastarrColors.surface),
            contentAlignment = Alignment.Center,
        ) {
            Text(
                if (done) "✓" else "$number",
                color = if (active || done) CastarrColors.onAccent else CastarrColors.faint,
                fontFamily = AppFont, fontSize = 14.sp, fontWeight = FontWeight.Medium,
            )
        }
        Spacer(Modifier.width(14.dp))
        Text(
            text,
            color = if (active) CastarrColors.fg else CastarrColors.muted,
            fontFamily = AppFont, fontSize = 17.sp,
        )
    }
}

@Composable
private fun Status(text: String) {
    Text(text, color = CastarrColors.accent, fontFamily = AppFont, fontSize = 14.sp)
}
