package eu.friedcutlet.yokan.ui.screens.detail import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.CheckCircle import androidx.compose.material.icons.filled.SaveAlt import androidx.compose.material.icons.filled.Info import androidx.compose.material3.AlertDialog import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import eu.friedcutlet.yokan.database.DbLocalFolder import eu.friedcutlet.yokan.domain.Chapter import eu.friedcutlet.yokan.domain.ItemType import eu.friedcutlet.yokan.ui.components.ScrollbarBox import eu.friedcutlet.yokan.ui.components.compactDialogWidth import org.jetbrains.compose.resources.stringResource import yokan.composeapp.generated.resources.Res import yokan.composeapp.generated.resources.action_cancel import yokan.composeapp.generated.resources.delete @Composable internal fun DownloadDestinationDialog( title: String, folders: List, defaultRoot: String, selectedName: String?, onSelect: (String?) -> Unit, onDismiss: () -> Unit, ) { val listState = rememberLazyListState() val resolvedSelectedName = selectedName?.takeIf { selected -> folders.any { it.name == selected } } AlertDialog( onDismissRequest = onDismiss, modifier = Modifier.compactDialogWidth(), shape = MaterialTheme.shapes.extraLarge, title = { Text(title, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold) }, text = { eu.friedcutlet.yokan.ui.components.ScrollbarBox( state = listState, modifier = Modifier.fillMaxWidth(), ) { LazyColumn( state = listState, modifier = Modifier.fillMaxWidth().heightIn(max = 360.dp), verticalArrangement = Arrangement.spacedBy(8.dp), ) { item(key = "default") { DownloadDestinationRow( title = "Default app folder", subtitle = defaultRoot, selected = resolvedSelectedName == null, onClick = { onSelect(null) }, ) } items(folders, key = { it.name }) { folder -> DownloadDestinationRow( title = folder.name, subtitle = folder.path, selected = resolvedSelectedName == folder.name, onClick = { onSelect(folder.name) }, ) } } } }, confirmButton = {}, dismissButton = { TextButton(onClick = onDismiss) { Text(stringResource(Res.string.action_cancel)) } }, ) } @Composable private fun DownloadDestinationRow( title: String, subtitle: String, selected: Boolean, onClick: () -> Unit, ) { Surface( modifier = Modifier .fillMaxWidth() .clip(MaterialTheme.shapes.large) .clickable(onClick = onClick), shape = MaterialTheme.shapes.large, color = if (selected) { MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.62f) } else { MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.34f) }, ) { Row( modifier = Modifier.padding(horizontal = 12.dp, vertical = 10.dp), horizontalArrangement = Arrangement.spacedBy(12.dp), verticalAlignment = Alignment.CenterVertically, ) { Icon( imageVector = if (selected) Icons.Filled.CheckCircle else Icons.Filled.SaveAlt, contentDescription = null, tint = if (selected) { MaterialTheme.colorScheme.onPrimaryContainer } else { MaterialTheme.colorScheme.onSurfaceVariant }, ) Column(modifier = Modifier.weight(1f)) { Text( title, style = MaterialTheme.typography.bodyLarge, fontWeight = if (selected) FontWeight.SemiBold else FontWeight.Medium, color = if (selected) MaterialTheme.colorScheme.onPrimaryContainer else MaterialTheme.colorScheme.onSurface, ) Text( subtitle, style = MaterialTheme.typography.bodySmall, color = if (selected) { MaterialTheme.colorScheme.onPrimaryContainer.copy(alpha = 0.76f) } else { MaterialTheme.colorScheme.onSurfaceVariant }, maxLines = 2, overflow = TextOverflow.Ellipsis, ) } } } } /** Confirms deleting downloaded files, or local chapters when [isLocalEntry]; [deletesEntry] warns the whole title goes. */ @Composable internal fun DeleteChaptersDialog( isLocalEntry: Boolean, deletesEntry: Boolean, selectedCount: Int, itemType: ItemType, onConfirm: () -> Unit, onDismiss: () -> Unit, ) { AlertDialog( onDismissRequest = onDismiss, title = { Text( if (isLocalEntry) { "Delete chapters" } else { "Delete downloads" }, ) }, text = { Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { if (deletesEntry) { Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp), ) { Icon( Icons.Filled.Info, contentDescription = "Warning", tint = MaterialTheme.colorScheme.error, ) Text( "Are you sure you want to delete all chapters? This will delete the entire entry from the library.", color = MaterialTheme.colorScheme.error, style = MaterialTheme.typography.bodyMedium, ) } } else { Text( if (isLocalEntry) { "Delete $selectedCount selected local " + "${chapterLabel(itemType).lowercase()}${if (selectedCount == 1) "" else "s"}?" } else { "Delete downloaded files for $selectedCount selected " + "${chapterLabel(itemType).lowercase()}${if (selectedCount == 1) "" else "s"}?" }, style = MaterialTheme.typography.bodyMedium, ) } } }, confirmButton = { TextButton(onClick = onConfirm) { Text(stringResource(Res.string.delete), color = MaterialTheme.colorScheme.error) } }, dismissButton = { TextButton(onClick = onDismiss) { Text(stringResource(Res.string.action_cancel)) } }, ) } internal sealed class PendingDownloadRequest { data class Single(val chapter: Chapter) : PendingDownloadRequest() data class Bulk(val chapters: List, val option: DetailDownloadOption) : PendingDownloadRequest() } internal enum class DetailDownloadOption { Next, Next5, Next10, Next25, Unread, All, Selected, } internal fun downloadOptionLabel(option: DetailDownloadOption, itemType: ItemType): String { val unit = if (itemType == ItemType.Anime) "episode" else "chapter" return when (option) { DetailDownloadOption.Next -> "Next $unit" DetailDownloadOption.Next5 -> "Next 5 ${unit}s" DetailDownloadOption.Next10 -> "Next 10 ${unit}s" DetailDownloadOption.Next25 -> "Next 25 ${unit}s" DetailDownloadOption.Unread -> if (itemType == ItemType.Anime) "Unwatched" else "Unread" DetailDownloadOption.All -> "All ${unit}s" DetailDownloadOption.Selected -> "Selected ${unit}s" } } internal fun chaptersForDownloadOption(chapters: List, option: DetailDownloadOption): List = when (option) { DetailDownloadOption.Unread -> chapters.filter { !it.isRead } DetailDownloadOption.All -> chapters DetailDownloadOption.Selected -> chapters DetailDownloadOption.Next, DetailDownloadOption.Next5, DetailDownloadOption.Next10, DetailDownloadOption.Next25 -> { val count = when (option) { DetailDownloadOption.Next -> 1 DetailDownloadOption.Next5 -> 5 DetailDownloadOption.Next10 -> 10 DetailDownloadOption.Next25 -> 25 DetailDownloadOption.Unread, DetailDownloadOption.All, DetailDownloadOption.Selected -> 1 } val lastReadIndex = chapters.indexOfLast { it.isRead } val startIndex = if (lastReadIndex < 0 || chapters.size == 1) 0 else lastReadIndex + 1 chapters.drop(startIndex).take(count) } }