package tv.tivilux.nativeplayer.ui.screens import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyRow import androidx.compose.foundation.lazy.items import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.remember import androidx.compose.ui.focus.FocusRequester import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import coil.compose.AsyncImage import tv.tivilux.nativeplayer.model.Episode import tv.tivilux.nativeplayer.model.PlaybackProgress import tv.tivilux.nativeplayer.model.SeriesDetail import tv.tivilux.nativeplayer.ui.components.LuxButton import tv.tivilux.nativeplayer.ui.components.LuxCard import tv.tivilux.nativeplayer.ui.theme.LuxGold import tv.tivilux.nativeplayer.ui.theme.LuxLine import tv.tivilux.nativeplayer.ui.theme.LuxMuted import tv.tivilux.nativeplayer.ui.theme.LuxPanel2 import tv.tivilux.nativeplayer.ui.theme.LuxText @Composable fun SeriesDetailScreen( detail: SeriesDetail?, isFavorite: Boolean, onBack: () -> Unit, onFavorite: () -> Unit, onPlayEpisode: (Episode) -> Unit, progressForEpisode: (Episode) -> PlaybackProgress? = { null }, ) { if (detail == null) return val playFocus = remember(detail.series.id) { FocusRequester() } val seasons = detail.episodes.keys.sorted() var selectedSeason by remember(detail.series.id) { mutableStateOf(seasons.firstOrNull() ?: 1) } val episodes = detail.episodes[selectedSeason].orEmpty() val seasonInfo = detail.seasons[selectedSeason] LaunchedEffect(detail.series.id, episodes.isNotEmpty()) { if (episodes.isNotEmpty()) playFocus.requestFocus() } BoxWithConstraints(Modifier.fillMaxSize().background(Color(0xFF090B10))) { // Keep the cinematic identity, but give episodes the majority of the useful TV viewport. // Short/overscanned displays get an even tighter hero automatically. val compactLayout = maxHeight < 650.dp val backdropHeight = if (compactLayout) 188.dp else 212.dp val heroHeight = if (compactLayout) 148.dp else 168.dp val posterWidth = if (compactLayout) 92.dp else 102.dp val episodeRowHeight = if (compactLayout) 112.dp else 120.dp val background = detail.backdrop.ifBlank { detail.series.image } if (background.isNotBlank()) { AsyncImage( model = background, contentDescription = detail.series.name, modifier = Modifier.fillMaxWidth().height(backdropHeight), contentScale = ContentScale.Crop, alpha = .38f, ) } Box( Modifier.fillMaxWidth().height(backdropHeight + 18.dp).background( Brush.verticalGradient(listOf(Color(0x44090B10), Color(0xFF090B10))) ) ) Column(Modifier.fillMaxSize().padding(horizontal = 30.dp, vertical = 16.dp)) { Row(Modifier.fillMaxWidth().height(heroHeight), horizontalArrangement = Arrangement.spacedBy(18.dp)) { Box( Modifier.width(posterWidth).fillMaxHeight() .background(LuxPanel2, RoundedCornerShape(16.dp)) .border(1.dp, LuxLine, RoundedCornerShape(16.dp)) ) { AsyncImage( model = detail.series.image, contentDescription = detail.series.name, modifier = Modifier.fillMaxSize(), contentScale = ContentScale.Crop, ) } Column(Modifier.weight(1f).fillMaxHeight(), verticalArrangement = Arrangement.Center) { Text("SERIES", color = LuxGold, fontSize = 10.sp, fontWeight = FontWeight.Bold, letterSpacing = 1.8.sp) Text( detail.series.name, color = LuxText, fontSize = if (compactLayout) 25.sp else 28.sp, lineHeight = if (compactLayout) 28.sp else 31.sp, fontWeight = FontWeight.SemiBold, maxLines = if (compactLayout) 1 else 2, overflow = TextOverflow.Ellipsis, ) val meta = listOf( detail.releaseDate.take(4).ifBlank { detail.series.year }, if (seasons.isNotEmpty()) "${seasons.size} season${if (seasons.size == 1) "" else "s"}" else "", detail.genre, detail.rating.takeIf { it.isNotBlank() }?.let { "★ $it" }.orEmpty(), ).filter { it.isNotBlank() }.joinToString(" • ") if (meta.isNotBlank()) Text(meta, color = LuxGold, fontSize = 12.sp, fontWeight = FontWeight.SemiBold) if (detail.plot.isNotBlank()) { Text( detail.plot, color = LuxText.copy(alpha = .86f), fontSize = 13.sp, lineHeight = 19.sp, modifier = Modifier.padding(top = 6.dp).fillMaxWidth(.92f), maxLines = if (compactLayout) 1 else 2, overflow = TextOverflow.Ellipsis, ) } if (detail.cast.isNotBlank()) { Text("Cast ${detail.cast}", color = LuxMuted, fontSize = 10.sp, maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.padding(top = 5.dp)) } Row(Modifier.padding(top = 8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) { episodes.firstOrNull()?.let { first -> LuxButton("PLAY S${first.season} E${first.episodeNumber}", { onPlayEpisode(first) }, primary = true, focusRequester = playFocus, illuminateOnFocus = true) } LuxButton(if (isFavorite) "MY LIST" else "ADD TO MY LIST", onFavorite) LuxButton("BACK", onBack) } } } Spacer(Modifier.height(5.dp)) Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween) { Text("Episodes", color = LuxText, fontSize = 20.sp, fontWeight = FontWeight.SemiBold) Text("${episodes.size} episodes", color = LuxMuted, fontSize = 11.sp) } Spacer(Modifier.height(5.dp)) LazyRow(horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.fillMaxWidth()) { items(seasons, key = { it }) { season -> LuxButton("SEASON $season", { selectedSeason = season }, primary = selectedSeason == season) } } if (seasonInfo != null && (seasonInfo.overview.isNotBlank() || seasonInfo.airDate.isNotBlank())) { Column(Modifier.fillMaxWidth().padding(top = 5.dp, bottom = 2.dp)) { Text(seasonInfo.name.ifBlank { "Season $selectedSeason" }, color = LuxText, fontSize = 12.sp, fontWeight = FontWeight.SemiBold) val seasonMeta = listOf(seasonInfo.airDate, seasonInfo.episodeCount.takeIf { it > 0 }?.let { "$it episodes" }.orEmpty()).filter { it.isNotBlank() }.joinToString(" • ") if (seasonMeta.isNotBlank()) Text(seasonMeta, color = LuxGold, fontSize = 9.sp) if (seasonInfo.overview.isNotBlank()) Text(seasonInfo.overview, color = LuxMuted, fontSize = 10.sp, lineHeight = 14.sp, maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.padding(top = 2.dp)) } } Spacer(Modifier.height(4.dp)) LazyColumn(Modifier.fillMaxWidth().weight(1f), verticalArrangement = Arrangement.spacedBy(8.dp)) { items(episodes, key = { it.id }) { episode -> EpisodeRow(episode, progressForEpisode(episode), episodeRowHeight, onPlayEpisode) } } } } } @Composable private fun EpisodeRow( episode: Episode, progress: PlaybackProgress?, rowHeight: androidx.compose.ui.unit.Dp, onPlayEpisode: (Episode) -> Unit, ) { LuxCard(modifier = Modifier.fillMaxWidth().height(rowHeight), onClick = { onPlayEpisode(episode) }) { Row(Modifier.fillMaxSize().padding(10.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(14.dp)) { Box(Modifier.width(188.dp).fillMaxHeight().background(Color.Black, RoundedCornerShape(9.dp))) { if (episode.image.isNotBlank()) { AsyncImage(episode.image, episode.name, Modifier.fillMaxSize(), contentScale = ContentScale.Crop) } Box( Modifier.align(Alignment.BottomStart).background(Color.Black.copy(alpha = .72f), RoundedCornerShape(topEnd = 7.dp)).padding(horizontal = 8.dp, vertical = 4.dp) ) { Text("S${episode.season} E${episode.episodeNumber}", color = LuxGold, fontSize = 9.sp, fontWeight = FontWeight.Bold) } } Column(Modifier.weight(1f)) { Text("E${episode.episodeNumber} ${episode.name}", color = LuxText, fontSize = 16.sp, fontWeight = FontWeight.SemiBold, maxLines = 1, overflow = TextOverflow.Ellipsis) val meta = listOf(episode.duration, episode.releaseDate).filter { it.isNotBlank() }.joinToString(" • ") if (meta.isNotBlank()) Text(meta, color = LuxGold, fontSize = 9.sp) if (episode.plot.isNotBlank()) { Text(episode.plot, color = LuxMuted, fontSize = 10.sp, lineHeight = 14.sp, maxLines = 3, overflow = TextOverflow.Ellipsis, modifier = Modifier.padding(top = 5.dp)) } } val progressPercent = if (progress != null && progress.positionMs > 0L && progress.durationMs > 0L) { ((progress.positionMs * 100L) / progress.durationMs).coerceIn(1L, 100L) } else 0L val progressLabel = when { progress?.completed == true -> "✓ WATCHED" progressPercent > 0L -> { val remainingMinutes = ((progress!!.durationMs - progress.positionMs).coerceAtLeast(0L) / 60_000L) if (remainingMinutes > 0L) "RESUME $progressPercent%\n${remainingMinutes}m LEFT" else "RESUME $progressPercent%" } else -> "PLAY ▶" } Column(Modifier.widthIn(min = 84.dp).padding(end = 8.dp), horizontalAlignment = Alignment.End) { Text( progressLabel, color = if (progress?.completed == true) LuxGold else LuxText, fontSize = 10.sp, lineHeight = 14.sp, fontWeight = FontWeight.Bold, ) if (progressPercent in 1L..99L) { Box(Modifier.padding(top = 6.dp).width(82.dp).height(3.dp).background(LuxLine, RoundedCornerShape(2.dp))) { Box( Modifier.fillMaxHeight().fillMaxWidth(progressPercent / 100f) .background(LuxGold, RoundedCornerShape(2.dp)) ) } } } } } }