package com.mhss.app.mybrain.presentation.tgbot.dialogs 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.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.verticalScroll import androidx.compose.material3.AlertDialog import androidx.compose.material3.Button import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text import androidx.compose.material3.TextButton 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.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.mhss.app.ui.theme.Blue import com.mhss.app.ui.theme.Green import com.mhss.app.ui.theme.PrimaryColor import kotlinx.coroutines.delay import kotlinx.coroutines.launch @Composable fun BroadcastComposerDialog( botName: String, targetAudienceCount: Int, onDismiss: () -> Unit, onSendBroadcast: (text: String) -> Unit ) { var messageText by remember { mutableStateOf("") } var isSending by remember { mutableStateOf(false) } var sendSuccess by remember { mutableStateOf(false) } val coroutineScope = rememberCoroutineScope() AlertDialog( onDismissRequest = onDismiss, title = { Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp) ) { Text( text = "New Broadcast Campaign", style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.Bold) ) } }, text = { Column( modifier = Modifier .fillMaxWidth() .verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(10.dp) ) { // Audience Card Box( modifier = Modifier .fillMaxWidth() .clip(RoundedCornerShape(12.dp)) .background(Blue.copy(alpha = 0.12f)) .padding(10.dp) ) { Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically ) { Column { Text( text = "Target Audience", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f) ) Text( text = "$targetAudienceCount Active Bot Users", style = MaterialTheme.typography.labelMedium.copy(fontWeight = FontWeight.Bold), color = Blue ) } Text( text = "Free: 500 Max", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.6f) ) } } Text( text = "Broadcast Message Content:", style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant ) OutlinedTextField( value = messageText, onValueChange = { messageText = it }, placeholder = { Text("Write announcement, updates or markdown text...") }, modifier = Modifier.fillMaxWidth(), shape = RoundedCornerShape(12.dp), minLines = 4, maxLines = 8, enabled = !isSending && !sendSuccess ) if (sendSuccess) { Box( modifier = Modifier .fillMaxWidth() .clip(RoundedCornerShape(10.dp)) .background(Green.copy(alpha = 0.15f)) .padding(10.dp) ) { Text( text = "Broadcast successfully sent to $targetAudienceCount users!", style = MaterialTheme.typography.bodySmall.copy(fontWeight = FontWeight.SemiBold), color = Green ) } } } }, confirmButton = { if (!sendSuccess) { Button( onClick = { if (messageText.isBlank()) return@Button isSending = true coroutineScope.launch { delay(600) // Simulated send onSendBroadcast(messageText) isSending = false sendSuccess = true delay(900) onDismiss() } }, shape = RoundedCornerShape(10.dp), colors = ButtonDefaults.buttonColors(containerColor = Blue), enabled = messageText.isNotBlank() && !isSending ) { if (isSending) { CircularProgressIndicator( color = Color.White, modifier = Modifier.size(16.dp), strokeWidth = 2.dp ) } else { Text("Send to $targetAudienceCount Users") } } } else { Button( onClick = onDismiss, shape = RoundedCornerShape(10.dp) ) { Text("Done") } } }, dismissButton = { if (!sendSuccess) { TextButton(onClick = onDismiss) { Text("Cancel") } } } ) }