package com.shaarit.presentation.todo import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.ArrowBack import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp import androidx.compose.ui.window.Dialog import androidx.compose.ui.window.DialogProperties import org.json.JSONArray import org.json.JSONObject @OptIn(ExperimentalMaterial3Api::class) @Composable fun CustomRepeatDialog( initialRepeatMode: String?, onDismiss: () -> Unit, onSave: (String) -> Unit ) { var interval by remember { mutableStateOf("1") } var unit by remember { mutableStateOf("semaine") } val units = listOf("jour", "semaine", "mois", "an") var expandedUnit by remember { mutableStateOf(false) } val daysOfWeek = listOf("D", "L", "M", "M", "J", "V", "S") var selectedDays by remember { mutableStateOf(setOf()) } var endMode by remember { mutableStateOf("jamais") } // "jamais", "date", "occurrences" var endDate by remember { mutableStateOf("20 mai") } var endOccurrences by remember { mutableStateOf("1") } // Parse initialRepeatMode if it's CUSTOM LaunchedEffect(initialRepeatMode) { if (initialRepeatMode?.startsWith("CUSTOM:") == true) { try { val json = JSONObject(initialRepeatMode.substring(7)) interval = json.optString("interval", "1") unit = json.optString("unit", "semaine") val daysArr = json.optJSONArray("daysOfWeek") if (daysArr != null) { val days = mutableSetOf() for (i in 0 until daysArr.length()) { days.add(daysArr.getInt(i)) } selectedDays = days } endMode = json.optString("endMode", "jamais") endDate = json.optString("endDate", "") endOccurrences = json.optString("endOccurrences", "1") } catch (e: Exception) { // Default fallback } } } Dialog( onDismissRequest = onDismiss, properties = DialogProperties( usePlatformDefaultWidth = false, decorFitsSystemWindows = true ) ) { Scaffold( topBar = { TopAppBar( title = { Text("Répétition personnalisée") }, navigationIcon = { IconButton(onClick = onDismiss) { Icon(Icons.Default.ArrowBack, contentDescription = "Retour") } }, actions = { TextButton(onClick = { val json = JSONObject() json.put("interval", interval) json.put("unit", unit) if (unit == "semaine") { json.put("daysOfWeek", JSONArray(selectedDays.toList())) } json.put("endMode", endMode) if (endMode == "date") json.put("endDate", endDate) if (endMode == "occurrences") json.put("endOccurrences", endOccurrences) onSave("CUSTOM:${json.toString()}") }) { Text("ENREGISTRER", color = MaterialTheme.colorScheme.primary) } }, colors = TopAppBarDefaults.topAppBarColors( containerColor = MaterialTheme.colorScheme.surface ) ) } ) { paddingValues -> Column( modifier = Modifier .fillMaxSize() .padding(paddingValues) .padding(16.dp) ) { Text("Répéter par intervalle de", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) Spacer(modifier = Modifier.height(8.dp)) Row(verticalAlignment = Alignment.CenterVertically) { OutlinedTextField( value = interval, onValueChange = { interval = it }, modifier = Modifier.width(80.dp), keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), singleLine = true ) Spacer(modifier = Modifier.width(16.dp)) Box { OutlinedButton(onClick = { expandedUnit = true }) { Text(unit) } DropdownMenu( expanded = expandedUnit, onDismissRequest = { expandedUnit = false } ) { units.forEach { u -> DropdownMenuItem( text = { Text(u) }, onClick = { unit = u; expandedUnit = false } ) } } } } if (unit == "semaine") { Spacer(modifier = Modifier.height(24.dp)) Text("Répéter le", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) Spacer(modifier = Modifier.height(8.dp)) Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween ) { daysOfWeek.forEachIndexed { index, day -> val isSelected = selectedDays.contains(index) Box( modifier = Modifier .size(40.dp) .background( color = if (isSelected) MaterialTheme.colorScheme.primary else Color.Transparent, shape = CircleShape ) .clickable { if (isSelected) selectedDays = selectedDays - index else selectedDays = selectedDays + index }, contentAlignment = Alignment.Center ) { Text( text = day, color = if (isSelected) MaterialTheme.colorScheme.onPrimary else MaterialTheme.colorScheme.onSurface ) } } } } Spacer(modifier = Modifier.height(24.dp)) Text("Se termine", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) Spacer(modifier = Modifier.height(8.dp)) Row(verticalAlignment = Alignment.CenterVertically) { RadioButton( selected = endMode == "jamais", onClick = { endMode = "jamais" } ) Text("Jamais", modifier = Modifier.clickable { endMode = "jamais" }) } Row(verticalAlignment = Alignment.CenterVertically) { RadioButton( selected = endMode == "date", onClick = { endMode = "date" } ) Text("Le ", modifier = Modifier.clickable { endMode = "date" }) OutlinedTextField( value = endDate, onValueChange = { endDate = it }, modifier = Modifier.width(120.dp).padding(start = 8.dp), enabled = endMode == "date", singleLine = true ) } Row(verticalAlignment = Alignment.CenterVertically) { RadioButton( selected = endMode == "occurrences", onClick = { endMode = "occurrences" } ) Text("Après ", modifier = Modifier.clickable { endMode = "occurrences" }) OutlinedTextField( value = endOccurrences, onValueChange = { endOccurrences = it }, modifier = Modifier.width(80.dp).padding(horizontal = 8.dp), keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), enabled = endMode == "occurrences", singleLine = true ) Text("occurrence", modifier = Modifier.clickable { endMode = "occurrences" }) } } } } }