package com.mhss.app.mybrain.presentation.localsync.components import androidx.compose.animation.animateContentSize 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.PaddingValues 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.layout.width import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Icon import androidx.compose.material3.IconButton import androidx.compose.material3.MaterialTheme 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.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import com.mhss.app.mybrain.sync.model.PairedDevice import com.mhss.app.ui.Res import com.mhss.app.ui.add_custom_ip import com.mhss.app.ui.candidate_ips_label import com.mhss.app.ui.custom_ip_label import com.mhss.app.ui.delete import com.mhss.app.ui.edit_custom_ip import com.mhss.app.ui.ic_add import com.mhss.app.ui.ic_delete import com.mhss.app.ui.ic_edit import com.mhss.app.ui.ic_refresh import com.mhss.app.ui.ic_small_arrow_down import com.mhss.app.ui.ic_small_arrow_up import com.mhss.app.ui.sync import com.mhss.app.ui.unknown_device import org.jetbrains.compose.resources.painterResource import org.jetbrains.compose.resources.stringResource @Composable fun DeviceItemCard( device: PairedDevice, onPing: () -> Unit, onDelete: () -> Unit, onRemoveCustomIp: () -> Unit, onEditCustomIp: () -> Unit ) { var expanded by remember { mutableStateOf(false) } Card( modifier = Modifier.fillMaxWidth(), shape = RoundedCornerShape(12.dp), colors = CardDefaults.cardColors( containerColor = MaterialTheme.colorScheme.surface ), border = CardDefaults.outlinedCardBorder() ) { Column( modifier = Modifier .fillMaxWidth() .animateContentSize() ) { Row( modifier = Modifier .fillMaxWidth() .padding(16.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween ) { Column(modifier = Modifier.weight(1f)) { Row(verticalAlignment = Alignment.CenterVertically) { Box( modifier = Modifier .size(8.dp) .background( color = if (device.isConnected) Color(0xFF4CAF50) else Color( 0xFFE91E63 ), shape = CircleShape ) ) Spacer(modifier = Modifier.width(8.dp)) Text( text = device.deviceName.ifBlank { "${stringResource(Res.string.unknown_device)}: ${device.deviceId.take(8)}..." }, fontWeight = FontWeight.Bold, style = MaterialTheme.typography.bodyLarge ) } Spacer(modifier = Modifier.height(4.dp)) Text( text = "${device.ipAddress}:${device.port}", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.7f) ) } Row(verticalAlignment = Alignment.CenterVertically) { IconButton(onClick = onPing) { Icon( painter = painterResource(Res.drawable.ic_refresh), contentDescription = stringResource(Res.string.sync), tint = MaterialTheme.colorScheme.primary ) } IconButton(onClick = onDelete) { Icon( painter = painterResource(Res.drawable.ic_delete), contentDescription = stringResource(Res.string.delete), tint = MaterialTheme.colorScheme.error ) } IconButton(onClick = { expanded = !expanded }) { Icon( painter = painterResource( if (expanded) Res.drawable.ic_small_arrow_up else Res.drawable.ic_small_arrow_down ), contentDescription = null, tint = MaterialTheme.colorScheme.onSurfaceVariant ) } } } if (expanded) { HorizontalDivider(color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.12f)) Column( modifier = Modifier .fillMaxWidth() .padding(horizontal = 16.dp, vertical = 12.dp), verticalArrangement = Arrangement.spacedBy(12.dp) ) { Column { Text( text = stringResource(Res.string.custom_ip_label), style = MaterialTheme.typography.bodyMedium, fontWeight = FontWeight.Bold, color = MaterialTheme.colorScheme.onSurface ) val customIp = device.customIpAddress if (customIp != null) { Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically ) { Text( text = customIp, style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant ) Row(horizontalArrangement = Arrangement.spacedBy(4.dp)) { IconButton( onClick = onEditCustomIp, modifier = Modifier.size(32.dp) ) { Icon( painter = painterResource(Res.drawable.ic_edit), contentDescription = stringResource(Res.string.edit_custom_ip), modifier = Modifier.size(16.dp), tint = MaterialTheme.colorScheme.primary ) } IconButton( onClick = onRemoveCustomIp, modifier = Modifier.size(32.dp) ) { Icon( painter = painterResource(Res.drawable.ic_delete), contentDescription = stringResource(Res.string.delete), modifier = Modifier.size(16.dp), tint = MaterialTheme.colorScheme.error ) } } } } else { TextButton( onClick = onEditCustomIp, contentPadding = PaddingValues.Zero ) { Icon( painter = painterResource(Res.drawable.ic_add), contentDescription = null, modifier = Modifier.size(16.dp), tint = MaterialTheme.colorScheme.primary ) Spacer(modifier = Modifier.width(6.dp)) Text( text = stringResource(Res.string.add_custom_ip), style = MaterialTheme.typography.labelLarge, fontWeight = FontWeight.Medium ) } } } if (device.candidateIpAddresses.isNotEmpty()) { HorizontalDivider( color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.08f) ) Column(verticalArrangement = Arrangement.spacedBy(4.dp)) { Text( text = stringResource(Res.string.candidate_ips_label), style = MaterialTheme.typography.bodyMedium, fontWeight = FontWeight.Bold, color = MaterialTheme.colorScheme.onSurface ) device.candidateIpAddresses.forEach { ip -> Text( text = ip, style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.8f), modifier = Modifier.padding(vertical = 2.dp) ) } } } } } } } }