package tv.tivilux.nativeplayer.ui.components 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.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.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.width import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.verticalScroll import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.OutlinedTextFieldDefaults import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue 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.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import kotlinx.coroutines.delay import kotlinx.coroutines.launch import tv.tivilux.nativeplayer.data.SupportReportClient import tv.tivilux.nativeplayer.data.SupportReportContext import tv.tivilux.nativeplayer.ui.theme.LuxGold import tv.tivilux.nativeplayer.ui.theme.LuxGreen 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 enum class SupportIssue(val label: String) { CHANNEL_DOWN("Channel Down"), BUFFERING("Buffering / Freezing"), AV_SYNC("Audio / Video Sync"), GUIDE("Wrong Guide Info"), SPORTS("Sports Feed Problem"), VOD("Movie / Series Problem"), LOGIN("Login / Account"), OTHER("Other"), } @Composable fun SupportReportDialog( details: SupportReportContext, initialIssue: SupportIssue = SupportIssue.OTHER, loginHelpMode: Boolean = false, onDismiss: () -> Unit, ) { val context = LocalContext.current.applicationContext val scope = rememberCoroutineScope() var selected by remember(initialIssue) { mutableStateOf(initialIssue) } var comment by remember { mutableStateOf("") } var sending by remember { mutableStateOf(false) } var sentId by remember { mutableStateOf(null) } var error by remember { mutableStateOf(null) } val firstIssueFocus = remember { FocusRequester() } androidx.compose.runtime.LaunchedEffect(Unit) { delay(70) runCatching { firstIssueFocus.requestFocus() } } BackHandler(enabled = !sending) { onDismiss() } Box( Modifier.fillMaxSize().background(Color.Black.copy(alpha = .78f)), contentAlignment = Alignment.Center, ) { Column( Modifier.width(690.dp) .background(LuxPanel, RoundedCornerShape(22.dp)) .border(1.dp, LuxGold.copy(alpha = .72f), RoundedCornerShape(22.dp)) .padding(24.dp) .verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(12.dp), ) { Text("TIVILUX SUPPORT", color = LuxGold, fontSize = 10.sp, fontWeight = FontWeight.Bold, letterSpacing = 1.8.sp) Text( if (loginHelpMode) "Need help signing in?" else "Report a Problem", color = LuxText, fontSize = 28.sp, fontWeight = FontWeight.SemiBold, ) Text( if (loginHelpMode) { "Tell the TiviLux team what you need help with. Your password is never included in a report." } else { "Choose the closest issue. TiviLux will attach safe device/playback details so the team can troubleshoot faster." }, color = LuxMuted, fontSize = 12.sp, ) Text("ISSUE", color = LuxMuted, fontSize = 9.sp, fontWeight = FontWeight.Bold, letterSpacing = 1.2.sp) Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp)) { Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(8.dp)) { SupportIssue.values().take(4).forEach { issue -> IssueChoice( issue = issue, selected = selected == issue, modifier = if (issue == initialIssue) Modifier.focusRequester(firstIssueFocus) else Modifier, ) { selected = issue } } } Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(8.dp)) { SupportIssue.values().drop(4).forEach { issue -> IssueChoice( issue = issue, selected = selected == issue, modifier = if (issue == initialIssue) Modifier.focusRequester(firstIssueFocus) else Modifier, ) { selected = issue } } } } OutlinedTextField( value = comment, onValueChange = { if (it.length <= 500) comment = it }, label = { Text(if (loginHelpMode) "How can we help?" else "Optional note") }, placeholder = { Text( if (loginHelpMode) "Example: I forgot my username/password and need help recovering my account." else "Describe what happened…" ) }, minLines = 2, maxLines = 4, keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), colors = OutlinedTextFieldDefaults.colors( focusedTextColor = LuxText, unfocusedTextColor = LuxText, focusedBorderColor = LuxGold, unfocusedBorderColor = LuxLine, focusedLabelColor = LuxGold, unfocusedLabelColor = LuxMuted, cursorColor = LuxGold, ), modifier = Modifier.fillMaxWidth(), ) Text( "Sent automatically: app/build, device + Android version, and available channel/player context. Never sent: password or provider credentials.", color = LuxMuted.copy(alpha = .82f), fontSize = 9.sp, ) if (!error.isNullOrBlank()) { Text(error.orEmpty(), color = Color(0xFFFFAAAA), fontSize = 11.sp) } if (!sentId.isNullOrBlank()) { Text("✓ Report sent to the TiviLux team • ${sentId}", color = LuxGreen, fontSize = 12.sp, fontWeight = FontWeight.SemiBold) } Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(10.dp)) { LuxButton( text = if (sending) "SENDING…" else if (sentId != null) "SENT" else "SEND REPORT", onClick = { if (sending || sentId != null) return@LuxButton sending = true error = null scope.launch { val result = SupportReportClient.submit( context = context, issueType = selected.label, comment = comment, details = details, ) sending = false if (result.ok) { sentId = result.reportId.ifBlank { "Report received" } } else { error = result.error.ifBlank { "Unable to send report. Please try again." } } } }, modifier = Modifier.weight(1f), primary = true, enabled = !sending && sentId == null, ) LuxButton( text = if (sentId != null) "DONE" else "CANCEL", onClick = onDismiss, modifier = Modifier.weight(.58f), enabled = !sending, ) } } } } @Composable private fun IssueChoice( issue: SupportIssue, selected: Boolean, modifier: Modifier = Modifier, onClick: () -> Unit, ) { var focused by remember { mutableStateOf(false) } Box( modifier.fillMaxWidth().height(42.dp) .background( when { focused -> Color(0xFF304451) selected -> Color(0xFF1B3331) else -> Color(0xFF172029) }, RoundedCornerShape(9.dp), ) .border( 1.dp, when { focused -> LuxGold selected -> LuxGreen.copy(alpha = .9f) else -> LuxLine }, RoundedCornerShape(9.dp), ) .onFocusChanged { focused = it.isFocused } .focusable() .clickable(onClick = onClick) .padding(horizontal = 12.dp), contentAlignment = Alignment.CenterStart, ) { Text( (if (selected) "✓ " else "") + issue.label, color = if (selected || focused) LuxText else LuxMuted, fontSize = 11.sp, fontWeight = if (selected || focused) FontWeight.SemiBold else FontWeight.Normal, ) } }