package tv.tivilux.nativeplayer.ui.screens import androidx.activity.compose.BackHandler import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.clickable import androidx.compose.foundation.focusable import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.BasicTextField import androidx.compose.material3.Text import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.focus.onFocusChanged import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.compose.ui.window.Dialog import kotlinx.coroutines.delay import tv.tivilux.nativeplayer.model.HouseholdProfile import tv.tivilux.nativeplayer.ui.components.LuxButton 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.LuxPanel import tv.tivilux.nativeplayer.ui.theme.LuxText @Composable fun ProfilesScreen( profiles: List, activeProfile: HouseholdProfile, onSelect: (HouseholdProfile) -> Unit, onCreate: (String) -> Unit, onRename: (HouseholdProfile, String) -> Unit, onDelete: (HouseholdProfile) -> Unit, onBack: () -> Unit, ) { var adding by remember { mutableStateOf(false) } var managing by remember { mutableStateOf(null) } var name by remember { mutableStateOf("") } var deleteArmed by remember { mutableStateOf(false) } val editorFocus = remember { FocusRequester() } fun closeEditor() { adding = false managing = null name = "" deleteArmed = false } BackHandler { if (adding || managing != null) closeEditor() else onBack() } Box( Modifier.fillMaxSize().background( Brush.radialGradient(listOf(Color(0xFF202C36), Color(0xFF080B10)), radius = 1200f) ) ) { Column( Modifier.fillMaxSize().padding(horizontal = 46.dp, vertical = 34.dp), verticalArrangement = Arrangement.spacedBy(22.dp), ) { Row( Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween, ) { Column { Text("TIVILUX HOUSEHOLD", color = LuxGold, fontSize = 10.sp, fontWeight = FontWeight.Bold, letterSpacing = 1.7.sp) Text("Who's watching?", color = LuxText, fontSize = 38.sp, fontWeight = FontWeight.SemiBold) Text("Up to 3 members. History, Favorites and Continue Watching stay separate.", color = LuxMuted, fontSize = 12.sp) } LuxButton("BACK", onBack) } Row( Modifier.fillMaxWidth().weight(1f), horizontalArrangement = Arrangement.spacedBy(18.dp), verticalAlignment = Alignment.CenterVertically, ) { profiles.take(3).forEach { profile -> ProfileCard( profile = profile, active = profile.id == activeProfile.id, modifier = Modifier.weight(1f), onSelect = { onSelect(profile) }, onManage = { managing = profile adding = false name = profile.name deleteArmed = false }, ) } if (profiles.size < 3) { AddProfileCard(Modifier.weight(1f)) { adding = true managing = null name = "" deleteArmed = false } } } } if (adding) { MemberEditorDialog( title = "ADD MEMBER", subtitle = "Create a separate TiviLux household profile.", name = name, onNameChange = { name = it.take(22) }, focusRequester = editorFocus, onSave = { if (name.isNotBlank()) { onCreate(name) closeEditor() } }, onDismiss = ::closeEditor, ) } managing?.let { profile -> ManageMemberDialog( profile = profile, name = name, canDelete = profiles.size > 1, deleteArmed = deleteArmed, focusRequester = editorFocus, onNameChange = { name = it.take(22) deleteArmed = false }, onSave = { if (name.isNotBlank()) { onRename(profile, name) closeEditor() } }, onDelete = { if (!deleteArmed) { deleteArmed = true } else { onDelete(profile) closeEditor() } }, onDismiss = ::closeEditor, ) } } } @Composable private fun ProfileCard( profile: HouseholdProfile, active: Boolean, modifier: Modifier, onSelect: () -> Unit, onManage: () -> Unit, ) { var watchFocused by remember { mutableStateOf(false) } Column( modifier.height(300.dp) .background(if (watchFocused) Color(0xFF243440) else LuxPanel, RoundedCornerShape(20.dp)) .border(2.dp, if (watchFocused || active) LuxGold else LuxLine, RoundedCornerShape(20.dp)) .padding(14.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(10.dp), ) { Column( Modifier.fillMaxWidth().weight(1f) .onFocusChanged { watchFocused = it.isFocused } .focusable() .clickable(onClick = onSelect) .padding(top = 6.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(10.dp), ) { Box( Modifier.size(112.dp).background( Brush.linearGradient(listOf(Color(0xFFE8C38B).copy(alpha = .9f), Color(0xFF6E8798))), RoundedCornerShape(30.dp), ), contentAlignment = Alignment.Center, ) { Text( profile.name.trim().take(1).uppercase().ifBlank { "M" }, color = Color(0xFF101318), fontSize = 42.sp, fontWeight = FontWeight.Black, ) } Text(profile.name, color = LuxText, fontSize = 20.sp, fontWeight = FontWeight.SemiBold) Text( if (active) "CURRENT MEMBER" else "PRESS OK TO WATCH", color = if (active) LuxGold else LuxMuted, fontSize = 9.sp, fontWeight = FontWeight.Bold, ) } LuxButton("MANAGE MEMBER", onManage, modifier = Modifier.fillMaxWidth()) } } @Composable private fun AddProfileCard(modifier: Modifier, onAdd: () -> Unit) { var focused by remember { mutableStateOf(false) } Column( modifier.height(300.dp).background(if (focused) Color(0xFF243440) else Color(0xFF11171E), RoundedCornerShape(20.dp)) .border(2.dp, if (focused) LuxGold else LuxLine, RoundedCornerShape(20.dp)) .onFocusChanged { focused = it.isFocused }.focusable().clickable(onClick = onAdd).padding(18.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, ) { Text("+", color = LuxGold, fontSize = 58.sp, fontWeight = FontWeight.Light) Text("ADD MEMBER", color = LuxText, fontSize = 14.sp, fontWeight = FontWeight.Bold) Text("Maximum 3", color = LuxMuted, fontSize = 10.sp) } } @Composable private fun MemberEditorDialog( title: String, subtitle: String, name: String, onNameChange: (String) -> Unit, focusRequester: FocusRequester, onSave: () -> Unit, onDismiss: () -> Unit, ) { Dialog(onDismissRequest = onDismiss) { MemberDialogShell { Text(title, color = LuxGold, fontSize = 11.sp, fontWeight = FontWeight.Bold, letterSpacing = 1.2.sp) Text(subtitle, color = LuxMuted, fontSize = 11.sp) MemberNameField(name, onNameChange, focusRequester) Row(horizontalArrangement = Arrangement.spacedBy(10.dp)) { LuxButton("SAVE MEMBER", onSave, primary = true, enabled = name.isNotBlank()) LuxButton("CANCEL", onDismiss) } } } } @Composable private fun ManageMemberDialog( profile: HouseholdProfile, name: String, canDelete: Boolean, deleteArmed: Boolean, focusRequester: FocusRequester, onNameChange: (String) -> Unit, onSave: () -> Unit, onDelete: () -> Unit, onDismiss: () -> Unit, ) { Dialog(onDismissRequest = onDismiss) { MemberDialogShell { Text("MANAGE MEMBER", color = LuxGold, fontSize = 11.sp, fontWeight = FontWeight.Bold, letterSpacing = 1.2.sp) Text("Edit or remove ${profile.name}.", color = LuxMuted, fontSize = 11.sp) MemberNameField(name, onNameChange, focusRequester) if (deleteArmed) { Text( "Delete ${profile.name}? Press DELETE MEMBER again to confirm. This also clears this member's Favorites, history and Continue Watching.", color = Color(0xFFFFC2B8), fontSize = 10.sp, ) } else if (!canDelete) { Text("At least one household member must remain.", color = LuxMuted, fontSize = 10.sp) } Row(horizontalArrangement = Arrangement.spacedBy(10.dp)) { LuxButton("SAVE CHANGES", onSave, primary = true, enabled = name.isNotBlank()) if (canDelete) { LuxButton(if (deleteArmed) "DELETE MEMBER — CONFIRM" else "DELETE MEMBER", onDelete) } LuxButton("DONE", onDismiss) } } } } @Composable private fun MemberDialogShell(content: @Composable ColumnScope.() -> Unit) { Column( Modifier.widthIn(min = 560.dp, max = 760.dp) .background(Color(0xFF111820), RoundedCornerShape(18.dp)) .border(1.dp, LuxGold.copy(alpha = .7f), RoundedCornerShape(18.dp)) .padding(20.dp), verticalArrangement = Arrangement.spacedBy(14.dp), content = content, ) } @Composable private fun MemberNameField( name: String, onNameChange: (String) -> Unit, focusRequester: FocusRequester, ) { LaunchedEffect(Unit) { delay(80) runCatching { focusRequester.requestFocus() } } BasicTextField( value = name, onValueChange = onNameChange, singleLine = true, textStyle = TextStyle(color = LuxText, fontSize = 17.sp, fontWeight = FontWeight.SemiBold), modifier = Modifier.fillMaxWidth().height(48.dp).focusRequester(focusRequester) .background(Color(0xFF202A34), RoundedCornerShape(9.dp)) .border(1.dp, LuxLine, RoundedCornerShape(9.dp)).padding(horizontal = 12.dp, vertical = 11.dp), decorationBox = { inner -> Box(contentAlignment = Alignment.CenterStart) { if (name.isBlank()) Text("Enter member name", color = LuxMuted, fontSize = 14.sp) inner() } }, ) }