package eu.friedcutlet.yokan.ui.screens.migration import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.background import androidx.compose.foundation.combinedClickable 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.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.ArrowForward import androidx.compose.material.icons.automirrored.filled.MenuBook import androidx.compose.material.icons.filled.ExpandLess import androidx.compose.material.icons.filled.ExpandMore import androidx.compose.material.icons.filled.Extension import androidx.compose.material3.Button import androidx.compose.material3.Checkbox import androidx.compose.material3.Icon import androidx.compose.material3.ListItem 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.draw.clip import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.role import androidx.compose.ui.semantics.selected import androidx.compose.ui.semantics.semantics import androidx.compose.ui.semantics.stateDescription import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.viewmodel.compose.viewModel import eu.friedcutlet.yokan.app.YokanRuntime import eu.friedcutlet.yokan.domain.Source import eu.friedcutlet.yokan.ui.components.RouteScaffold import eu.friedcutlet.yokan.ui.components.hoverHighlight import eu.friedcutlet.yokan.ui.navigation.MissingRoute import eu.friedcutlet.yokan.ui.navigation.Navigator import eu.friedcutlet.yokan.ui.navigation.Route import eu.friedcutlet.yokan.services.extensions.languageName import org.jetbrains.compose.resources.stringResource import yokan.composeapp.generated.resources.Res import yokan.composeapp.generated.resources.accessibility_collapsed import yokan.composeapp.generated.resources.accessibility_expanded import yokan.composeapp.generated.resources.mass_migration_collapse_source_chapters import yokan.composeapp.generated.resources.mass_migration_expand_source_chapters @Composable internal fun MassMigrationPreviewRoute(route: Route.MassMigrationPreview, nav: Navigator) { val vm = viewModel { MassMigrationPreviewViewModel(route.sourceId, YokanRuntime.repository, YokanRuntime.settings, YokanRuntime.libraryIndex) } val state by vm.state.collectAsStateWithLifecycle() val source = state.source ?: return MissingRoute("Mass Migration Preview", nav::back) MassMigrationPreviewScreen( source = source, state = state, onToggle = vm::toggle, onLongPress = vm::selectRangeTo, onToggleAll = vm::toggleAll, onContinue = { nav.navigate(Route.MassMigrationDestination(source.id, state.selectedMangaIds)) }, onBack = nav::back, ) } @OptIn(ExperimentalFoundationApi::class) @Composable internal fun MassMigrationPreviewScreen( source: Source, state: MassMigrationPreviewState, onToggle: (index: Int) -> Unit, onLongPress: (index: Int) -> Unit, onToggleAll: () -> Unit, onContinue: () -> Unit, onBack: () -> Unit, ) { val items = state.items.orEmpty() val totalCount = items.size val selectedCount = state.selectedCount val allSelected = state.allSelected RouteScaffold( title = "Mass Migration Preview", onBack = onBack, actions = { if (totalCount > 0) { TextButton(onClick = onToggleAll) { Text( if (allSelected) "Deselect all" else "Select all", style = MaterialTheme.typography.labelLarge, fontWeight = FontWeight.SemiBold, ) } } }, ) { // Header source tile — same rounded-surface idiom as the rest of the // ported screens; no clickable so it reads as a banner, not nav row. item { androidx.compose.material3.Surface( modifier = Modifier .fillMaxWidth() .padding(horizontal = 8.dp, vertical = 2.dp), shape = MaterialTheme.shapes.large, color = MaterialTheme.colorScheme.surfaceContainer, border = androidx.compose.foundation.BorderStroke(1.dp, MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.4f)), ) { ListItem( headlineContent = { Text( source.name, style = MaterialTheme.typography.titleSmall, fontWeight = FontWeight.SemiBold, ) }, supportingContent = { Text( "${languageName(source.lang)} • $selectedCount/$totalCount selected", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, ) }, leadingContent = { Box( modifier = Modifier .size(40.dp) .clip(androidx.compose.foundation.shape.CircleShape) .background(MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.55f)), contentAlignment = Alignment.Center, ) { Icon( Icons.Filled.Extension, contentDescription = null, tint = MaterialTheme.colorScheme.onPrimaryContainer, modifier = Modifier.size(20.dp), ) } }, colors = androidx.compose.material3.ListItemDefaults.colors( containerColor = androidx.compose.ui.graphics.Color.Transparent, ), ) } } // Per-manga preview cards w/ selectable checkbox & expandable chapter section. itemsIndexed(items, key = { _, it -> it.manga.id }) { index, item -> MassMigrationPreviewCard( item = item, index = index + 1, selected = item.manga.id in state.selectedIds, onToggle = { onToggle(index) }, onLongClick = { onLongPress(index) }, ) } // Bottom CTA — picks the destination next. item { Button( onClick = onContinue, modifier = Modifier.fillMaxWidth().height(50.dp), enabled = selectedCount > 0, shape = RoundedCornerShape(50), ) { Icon( Icons.AutoMirrored.Filled.ArrowForward, contentDescription = null, modifier = Modifier.size(18.dp), ) Spacer(Modifier.width(8.dp)) Text( "Select destination source", style = MaterialTheme.typography.labelLarge, fontWeight = FontWeight.SemiBold, ) } } } } @OptIn(ExperimentalFoundationApi::class) @Composable private fun MassMigrationPreviewCard( item: MassMigrationPreviewItem, index: Int, selected: Boolean, onToggle: () -> Unit, onLongClick: () -> Unit, ) { val title = item.manga var expanded by remember(title.id) { mutableStateOf(false) } val cardColor = if (selected) { MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.35f) } else { MaterialTheme.colorScheme.surfaceContainer } val borderColor = if (selected) { MaterialTheme.colorScheme.primary.copy(alpha = 0.7f) } else { MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.4f) } androidx.compose.material3.Surface( modifier = Modifier .fillMaxWidth() .padding(horizontal = 8.dp, vertical = 2.dp), shape = MaterialTheme.shapes.large, color = cardColor, border = androidx.compose.foundation.BorderStroke(if (selected) 1.5.dp else 1.dp, borderColor), ) { Column(Modifier.fillMaxWidth().padding(12.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) { Row( modifier = Modifier .fillMaxWidth() .padding(2.dp) .clip(MaterialTheme.shapes.medium) .combinedClickable( onClick = onToggle, onLongClick = onLongClick, ) .semantics { role = Role.Checkbox this.selected = selected }, horizontalArrangement = Arrangement.spacedBy(10.dp), verticalAlignment = Alignment.CenterVertically, ) { Checkbox( checked = selected, onCheckedChange = null, modifier = Modifier.size(20.dp), ) // Index in a small tonal disc — keeps numbering legible while // matching the rest of the modernised badges. Box( modifier = Modifier .size(28.dp) .clip(androidx.compose.foundation.shape.CircleShape) .background( if (selected) MaterialTheme.colorScheme.primary.copy(alpha = 0.85f) else MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.55f), ), contentAlignment = Alignment.Center, ) { Text( "$index", style = MaterialTheme.typography.labelLarge, fontWeight = FontWeight.SemiBold, color = if (selected) MaterialTheme.colorScheme.onPrimary else MaterialTheme.colorScheme.onPrimaryContainer, ) } Box( modifier = Modifier .size(width = 60.dp, height = 84.dp) .clip(MaterialTheme.shapes.medium), ) { eu.friedcutlet.yokan.ui.components.CoverImage( model = title.imageUrl, contentDescription = title.name, modifier = Modifier.fillMaxSize(), fallbackInitials = title.name.take(2).uppercase(), ) } Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(2.dp)) { Text( title.name, style = MaterialTheme.typography.titleSmall, fontWeight = FontWeight.SemiBold, maxLines = 2, overflow = TextOverflow.Ellipsis, ) Text( listOfNotNull(title.author?.takeIf { it.isNotBlank() }, "${item.chapterCount} chapters") .joinToString(" • "), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, ) } } // Expansion toggle — rounded clickable row with chevron that // rotates between collapsed/expanded states. val expansionActionLabel = stringResource( if (expanded) Res.string.mass_migration_collapse_source_chapters else Res.string.mass_migration_expand_source_chapters, title.name, ) val expansionStateDescription = stringResource( if (expanded) Res.string.accessibility_expanded else Res.string.accessibility_collapsed, ) Row( modifier = Modifier .fillMaxWidth() .padding(horizontal = 6.dp, vertical = 6.dp) .clip(MaterialTheme.shapes.large) .hoverHighlight(onClick = { expanded = !expanded }) .semantics { role = Role.Button contentDescription = expansionActionLabel stateDescription = expansionStateDescription }, verticalAlignment = Alignment.CenterVertically, ) { Icon( if (expanded) Icons.Filled.ExpandLess else Icons.Filled.ExpandMore, contentDescription = null, tint = MaterialTheme.colorScheme.primary, ) Spacer(Modifier.width(8.dp)) Text( "Source chapters (${item.chapterCount})", style = MaterialTheme.typography.labelLarge, fontWeight = FontWeight.Medium, ) } if (expanded) { val shown = item.previewChapterNames androidx.compose.material3.Surface( modifier = Modifier.fillMaxWidth(), shape = MaterialTheme.shapes.large, color = MaterialTheme.colorScheme.surfaceContainerHigh, ) { Column(modifier = Modifier.padding(horizontal = 12.dp, vertical = 8.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) { shown.forEach { chapterName -> Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) { Icon( Icons.AutoMirrored.Filled.MenuBook, contentDescription = null, tint = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.size(14.dp), ) Text( chapterName, style = MaterialTheme.typography.bodySmall, maxLines = 1, overflow = TextOverflow.Ellipsis, ) } } val remaining = item.chapterCount - shown.size if (remaining > 0) { Text( "and $remaining more chapters", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.primary, fontWeight = FontWeight.Medium, ) } } } } } } }