package dev.castarr.tv.ui

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.width
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.tv.material3.Text
import dev.castarr.tv.AppState

/** Text-heavy configuration, deliberately outside the family path (#14). */
@Composable
fun AdvancedScreen(state: AppState) {
    var m3u by remember { mutableStateOf(state.source.m3uUrl) }
    var epg by remember { mutableStateOf(state.source.epgUrl) }
    var message by remember { mutableStateOf("") }
    val status by state.source.status.collectAsState()

    val fieldColors = OutlinedTextFieldDefaults.colors(
        focusedTextColor = CastarrColors.fg,
        unfocusedTextColor = CastarrColors.muted,
        focusedBorderColor = CastarrColors.accent,
        unfocusedBorderColor = CastarrColors.line,
        focusedLabelColor = CastarrColors.accent,
        unfocusedLabelColor = CastarrColors.faint,
        cursorColor = CastarrColors.accent,
        focusedContainerColor = CastarrColors.surface,
        unfocusedContainerColor = CastarrColors.surface,
    )

    Column(
        Modifier
            .fillMaxSize()
            .padding(horizontal = 40.dp, vertical = 20.dp)
    ) {
        Text("Erweitert", color = CastarrColors.fg, fontFamily = AppFont, fontSize = 22.sp)
        Spacer(Modifier.height(4.dp))
        Text(
            "Eigene M3U-Quelle (ErsatzTV, Threadfin, Tunarr …) — optionales XMLTV-EPG. " +
                "Server: ${state.auth.serverUrl.ifEmpty { "—" }}",
            color = CastarrColors.faint, fontFamily = AppFont, fontSize = 13.sp,
        )
        Spacer(Modifier.height(18.dp))
        TvTextField(
            value = m3u,
            onValueChange = { m3u = it },
            label = { Text("M3U-URL", fontFamily = AppFont) },
            colors = fieldColors,
            textStyle = TextStyle(fontSize = 14.sp),
            modifier = Modifier.width(600.dp),
        )
        Spacer(Modifier.height(12.dp))
        TvTextField(
            value = epg,
            onValueChange = { epg = it },
            label = { Text("XMLTV-EPG-URL (optional)", fontFamily = AppFont) },
            colors = fieldColors,
            textStyle = TextStyle(fontSize = 14.sp),
            modifier = Modifier.width(600.dp),
        )
        Spacer(Modifier.height(18.dp))
        Row {
            ActionButton("Quelle laden") {
                message = ""
                state.source.configure(m3u.trim(), epg.trim()) { result ->
                    message = result.fold(
                        onSuccess = { "$it Sender geladen" },
                        onFailure = { "Fehler: Quelle nicht ladbar" },
                    )
                }
            }
            Spacer(Modifier.width(10.dp))
            ActionButton("Zurück") { state.screen = AppState.Screen.SETTINGS }
        }
        Spacer(Modifier.height(10.dp))
        val statusText = when {
            status == "loading_channels" -> "Lade Senderliste…"
            status == "loading_epg" -> "Lade EPG…"
            else -> message
        }
        if (statusText.isNotEmpty()) {
            Text(statusText, color = CastarrColors.muted, fontFamily = AppFont, fontSize = 13.sp)
        }
    }
}
