package eu.friedcutlet.yokan.ui.screens.detail import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.FlowRow import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.padding import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.verticalScroll import androidx.compose.material3.AlertDialog import androidx.compose.material3.FilledTonalButton import androidx.compose.material3.FilterChip import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedButton import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Surface 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.Modifier import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.unit.dp import eu.friedcutlet.yokan.database.DbTrackSearch import eu.friedcutlet.yokan.domain.Manga import org.jetbrains.compose.resources.stringResource import yokan.composeapp.generated.resources.Res import yokan.composeapp.generated.resources.action_search import yokan.composeapp.generated.resources.ui_close import yokan.composeapp.generated.resources.ui_search_tracker import yokan.composeapp.generated.resources.ui_title_field import eu.friedcutlet.yokan.ui.components.ScrollbarBox import eu.friedcutlet.yokan.ui.components.autoFocusOnMount import eu.friedcutlet.yokan.ui.components.compactDialogWidth import eu.friedcutlet.yokan.ui.components.compactSearchField import eu.friedcutlet.yokan.ui.components.searchOnEnter @Composable internal fun TrackerSearchDialog( manga: Manga, trackerNames: List, search: TrackerSearchState, onSearch: (trackerIndex: Int, query: String) -> Unit, onBind: (trackerIndex: Int, result: DbTrackSearch) -> Unit, onDismiss: () -> Unit, ) { var serviceIndex by remember { mutableStateOf(0) } var query by remember { mutableStateOf(manga.name) } val loading = search.loading val error = search.error val results = search.results fun submitTrackerSearch() { if (loading || query.isBlank() || trackerNames.isEmpty()) return onSearch(serviceIndex, query) } androidx.compose.material3.AlertDialog( onDismissRequest = onDismiss, modifier = Modifier.compactDialogWidth(), shape = MaterialTheme.shapes.extraLarge, confirmButton = { TextButton(onClick = onDismiss) { Text(stringResource(Res.string.ui_close)) } }, title = { Text( stringResource(Res.string.ui_search_tracker), style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold, ) }, text = { // Tracker search can return a long result list; without a scroll // wrapper the AlertDialog body clips at the bottom and the // "Add" buttons for lower results aren't reachable. Cap height // so short result lists still hug their content. val dialogScroll = androidx.compose.foundation.rememberScrollState() eu.friedcutlet.yokan.ui.components.ScrollbarBox( state = dialogScroll, modifier = Modifier.fillMaxWidth().heightIn(max = 520.dp), ) { Column( modifier = Modifier .fillMaxWidth() .verticalScroll(dialogScroll), verticalArrangement = Arrangement.spacedBy(8.dp), ) { if (trackerNames.isEmpty()) { Text( "No trackers configured.", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, ) } else { FlowRow( horizontalArrangement = Arrangement.spacedBy(6.dp), verticalArrangement = Arrangement.spacedBy(6.dp), ) { trackerNames.forEachIndexed { index, trackerName -> FilterChip( selected = serviceIndex == index, onClick = { serviceIndex = index }, label = { Text(trackerName) }, ) } } OutlinedTextField( value = query, onValueChange = { query = it }, placeholder = { Text(stringResource(Res.string.ui_title_field)) }, singleLine = true, keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search), keyboardActions = KeyboardActions(onSearch = { submitTrackerSearch() }), modifier = Modifier .fillMaxWidth() .autoFocusOnMount() .compactSearchField() .searchOnEnter { submitTrackerSearch() }, ) FilledTonalButton( onClick = { submitTrackerSearch() }, enabled = !loading && query.isNotBlank(), ) { Text(if (loading) "Searching…" else stringResource(Res.string.action_search)) } error?.let { Text( it, color = MaterialTheme.colorScheme.error, style = MaterialTheme.typography.bodySmall, ) } if (results.isEmpty() && !loading && error == null) { Text( "Type a title and tap Search.", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, ) } results.forEach { result -> Surface( modifier = Modifier.fillMaxWidth(), tonalElevation = 1.dp, shape = RoundedCornerShape(8.dp), ) { Column(modifier = Modifier.padding(10.dp)) { Text(result.title.orEmpty(), fontWeight = FontWeight.SemiBold) Text( "Chapters: ${result.totalChapter ?: "?"} • ${result.status}", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, ) OutlinedButton( onClick = { onBind(serviceIndex, result) onDismiss() }, modifier = Modifier.padding(top = 6.dp), ) { Text("Bind") } } } } } } } // end ScrollbarBox }, ) }