package eu.darken.sdmse.deduplicator.ui.details

import android.content.ActivityNotFoundException
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.twotone.FormatListBulleted
import androidx.compose.material.icons.twotone.Folder
import eu.darken.sdmse.common.compose.layout.SdmScaffold
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import eu.darken.sdmse.common.R as CommonR
import eu.darken.sdmse.common.compose.layout.SdmDeleteAction
import eu.darken.sdmse.common.compose.layout.SdmEmptyState
import eu.darken.sdmse.common.compose.layout.SdmExcludeAction
import eu.darken.sdmse.common.compose.layout.SdmLoadingState
import eu.darken.sdmse.common.compose.layout.SdmScrollableTabStrip
import eu.darken.sdmse.common.compose.layout.SdmSelectAllAction
import eu.darken.sdmse.common.compose.layout.SdmSelectionTopAppBar
import eu.darken.sdmse.common.compose.layout.SdmTooltipIconButton
import eu.darken.sdmse.common.compose.layout.SdmTopAppBar
import eu.darken.sdmse.common.compose.preview.Preview2
import eu.darken.sdmse.common.compose.preview.PreviewWrapper
import eu.darken.sdmse.common.compose.progress.ProgressOverlay
import eu.darken.sdmse.common.compose.selection.rememberSelectionState
import eu.darken.sdmse.common.compose.tour.LocalGuidedTourController
import eu.darken.sdmse.common.error.ErrorEventHandler
import eu.darken.sdmse.common.navigation.NavigationEventHandler
import eu.darken.sdmse.deduplicator.R as DeduplicatorR
import eu.darken.sdmse.deduplicator.core.Duplicate
import eu.darken.sdmse.deduplicator.ui.DeduplicatorDetailsRoute
import eu.darken.sdmse.deduplicator.ui.details.DeduplicatorDetailsViewModel.DeleteTarget
import eu.darken.sdmse.deduplicator.ui.details.cluster.ClusterContent
import eu.darken.sdmse.deduplicator.ui.details.cluster.buildClusterElements
import eu.darken.sdmse.deduplicator.ui.details.tour.DeduplicatorDetailsTour
import eu.darken.sdmse.deduplicator.ui.dialogs.PreviewDeletionDialog
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.launch

private data class PendingDetailsDeletion(
    val event: DeduplicatorDetailsViewModel.Event.ConfirmDeletion,
)

@Composable
fun DeduplicatorDetailsScreenHost(
    route: DeduplicatorDetailsRoute,
    vm: DeduplicatorDetailsViewModel = hiltViewModel(),
) {
    ErrorEventHandler(vm)
    NavigationEventHandler(vm)

    LaunchedEffect(route) { vm.bindRoute(route) }

    val context = LocalContext.current
    val snackbarHostState = remember { SnackbarHostState() }
    val snackScope = rememberCoroutineScope()
    val undoActionLabel = stringResource(CommonR.string.general_undo_action)
    val viewActionLabel = stringResource(CommonR.string.general_view_action)

    var pendingDeletion by remember { mutableStateOf<PendingDetailsDeletion?>(null) }

    LaunchedEffect(vm) {
        vm.events.collect { event ->
            when (event) {
                is DeduplicatorDetailsViewModel.Event.ConfirmDeletion -> {
                    pendingDeletion = PendingDetailsDeletion(event)
                }

                is DeduplicatorDetailsViewModel.Event.OpenDuplicate -> {
                    try {
                        context.startActivity(event.intent)
                    } catch (_: ActivityNotFoundException) {
                        snackScope.launch {
                            snackbarHostState.showSnackbar(
                                message = context.getString(CommonR.string.general_error_no_compatible_app_found_msg),
                                duration = SnackbarDuration.Long,
                            )
                        }
                    }
                }

                is DeduplicatorDetailsViewModel.Event.TaskResult -> snackScope.launch {
                    snackbarHostState.showSnackbar(
                        message = event.result.primaryInfo.get(context),
                        duration = SnackbarDuration.Short,
                    )
                }

                is DeduplicatorDetailsViewModel.Event.ExclusionsCreated -> snackScope.launch {
                    val message = context.resources.getQuantityString(
                        CommonR.plurals.exclusion_x_new_exclusions,
                        event.count,
                        event.count,
                    )
                    val result = snackbarHostState.showSnackbar(
                        message = message,
                        actionLabel = undoActionLabel,
                        duration = SnackbarDuration.Long,
                    )
                    if (result == SnackbarResult.ActionPerformed) {
                        vm.onUndoExclude(event.undo, event.restoreTarget)
                    }
                }

                is DeduplicatorDetailsViewModel.Event.SelectionExclusionsCreated -> snackScope.launch {
                    val message = context.resources.getQuantityString(
                        CommonR.plurals.exclusion_x_new_exclusions,
                        event.count,
                        event.count,
                    )
                    val result = snackbarHostState.showSnackbar(
                        message = message,
                        actionLabel = viewActionLabel,
                        duration = SnackbarDuration.Long,
                    )
                    if (result == SnackbarResult.ActionPerformed) {
                        vm.onShowExclusions()
                    }
                }
            }
        }
    }

    DeduplicatorDetailsScreen(
        stateSource = vm.state,
        snackbarHostState = snackbarHostState,
        onNavigateUp = vm::navUp,
        onPageChanged = vm::updatePage,
        onToggleDirectoryView = vm::toggleDirectoryView,
        onCollapseToggle = vm::toggleDirectoryCollapse,
        onClusterDelete = { id -> vm.deleteCluster(id) },
        onClusterExclude = { id -> vm.excludeCluster(id) },
        onGroupDelete = { clusterId, groupId -> vm.deleteGroup(clusterId, groupId) },
        onGroupView = { group, position -> vm.previewGroup(group, position) },
        onDuplicatePreview = { dupe -> vm.previewDuplicate(dupe) },
        onDuplicateDelete = { clusterId, dupeId -> vm.deleteDuplicates(clusterId, listOf(dupeId)) },
        onDirectoryDeleteAll = { clusterId, dirGroup ->
            vm.deleteDuplicates(clusterId, dirGroup.duplicates.map { it.identifier })
        },
        onDeleteSelectedDuplicates = { clusterId, ids -> vm.deleteDuplicates(clusterId, ids) },
        onExcludeSelectedDuplicates = { clusterId, ids -> vm.excludeDuplicates(clusterId, ids) },
    )

    pendingDeletion?.let { pending ->
        PreviewDeletionDialog(
            mode = pending.event.mode,
            onConfirm = { deleteAll ->
                pendingDeletion = null
                when (val target = pending.event.target) {
                    is DeleteTarget.ClusterTarget -> vm.deleteCluster(
                        clusterId = target.id,
                        confirmed = true,
                        deleteAll = deleteAll,
                    )

                    is DeleteTarget.GroupTarget -> vm.deleteGroup(
                        clusterId = pending.event.clusterId,
                        groupId = target.id,
                        confirmed = true,
                        deleteAll = deleteAll,
                    )

                    is DeleteTarget.DuplicateTargets -> vm.deleteDuplicates(
                        clusterId = pending.event.clusterId,
                        ids = target.ids,
                        confirmed = true,
                    )
                }
            },
            onDismiss = { pendingDeletion = null },
            onPreviewClick = { options -> vm.navTo(eu.darken.sdmse.common.previews.PreviewRoute(options = options)) },
            // Single-duplicate delete dialog offers "Open" (open in external app) — legacy parity.
            onOpen = (pending.event.mode as? eu.darken.sdmse.deduplicator.ui.dialogs.PreviewDeletionMode.Duplicates)
                ?.duplicates?.singleOrNull()
                ?.let { dupe -> { pendingDeletion = null; vm.openDuplicate(dupe) } },
        )
    }
}

@Composable
internal fun DeduplicatorDetailsScreen(
    stateSource: StateFlow<DeduplicatorDetailsViewModel.State?> = MutableStateFlow(null),
    snackbarHostState: SnackbarHostState = remember { SnackbarHostState() },
    onNavigateUp: () -> Unit = {},
    onPageChanged: (Duplicate.Cluster.Id) -> Unit = {},
    onToggleDirectoryView: () -> Unit = {},
    onCollapseToggle: (Duplicate.Cluster.Id, eu.darken.sdmse.deduplicator.ui.details.cluster.DirectoryGroup.Id) -> Unit = { _, _ -> },
    onClusterDelete: (Duplicate.Cluster.Id) -> Unit = {},
    onClusterExclude: (Duplicate.Cluster.Id) -> Unit = {},
    onGroupDelete: (Duplicate.Cluster.Id, Duplicate.Group.Id) -> Unit = { _, _ -> },
    onGroupView: (Duplicate.Group, Int) -> Unit = { _, _ -> },
    onDuplicatePreview: (Duplicate) -> Unit = {},
    onDuplicateDelete: (Duplicate.Cluster.Id, Duplicate.Id) -> Unit = { _, _ -> },
    onDirectoryDeleteAll: (Duplicate.Cluster.Id, eu.darken.sdmse.deduplicator.ui.details.cluster.DirectoryGroup) -> Unit = { _, _ -> },
    onDeleteSelectedDuplicates: (Duplicate.Cluster.Id, Set<Duplicate.Id>) -> Unit = { _, _ -> },
    onExcludeSelectedDuplicates: (Duplicate.Cluster.Id, Set<Duplicate.Id>) -> Unit = { _, _ -> },
) {
    val state by stateSource.collectAsStateWithLifecycle()
    val current = state
    val items = current?.items ?: emptyList()
    val coroutineScope = rememberCoroutineScope()

    val pagerState = rememberPagerState(pageCount = { items.size })

    val selection = rememberSelectionState<Duplicate.Id>()

    // drop(1) skips the initial currentPage=0 emission so the scroll-to-target effect below
    // isn't clobbered by a spurious onPageChanged(items[0]) before it can scroll.
    LaunchedEffect(pagerState, items) {
        snapshotFlow { pagerState.currentPage }
            .drop(1)
            .distinctUntilChanged()
            .collect { page ->
                selection.clear()
                items.getOrNull(page)?.identifier?.let(onPageChanged)
            }
    }

    LaunchedEffect(current?.target, items) {
        val target = current?.target ?: return@LaunchedEffect
        val idx = items.indexOfFirst { it.identifier == target }
        if (idx != -1 && pagerState.currentPage != idx) {
            pagerState.scrollToPage(idx)
        }
    }

    val currentCluster = items.getOrNull(pagerState.currentPage)
    val liveDupeIds = remember(currentCluster) {
        currentCluster?.groups?.flatMap { g -> g.duplicates.map { it.identifier } }?.toSet() ?: emptySet()
    }
    val allowDeleteAll = current?.allowDeleteAll == true
    val maxSelection = remember(currentCluster, allowDeleteAll) {
        val total = currentCluster?.groups?.sumOf { it.duplicates.size } ?: 0
        if (allowDeleteAll) total else (total - 1).coerceAtLeast(0)
    }
    LaunchedEffect(currentCluster?.identifier, liveDupeIds, maxSelection) {
        selection.retainAll(liveDupeIds)
        // Re-apply the keep-one cap: if the cluster shrank under a held selection (e.g. a group/cluster
        // delete removed members) or a saved selection was restored against a smaller cluster, the
        // retained set could otherwise exceed maxSelection and let a delete take the whole cluster.
        if (selection.count > maxSelection) {
            // Deterministic re-cap: follow the live duplicate order (set iteration order is
            // arbitrary) and drop keepers first — a keep-one delete preserves those copies
            // anyway, so deselecting them loses the least of the user's intent.
            val keeperIds = currentCluster?.groups?.mapNotNull { it.keeperIdentifier }?.toSet() ?: emptySet()
            val ordered = currentCluster?.groups
                ?.flatMap { it.duplicates }
                ?.map { it.identifier }
                ?.filter { it in selection.selected }
                ?.sortedBy { it in keeperIds } // stable: non-keepers keep live order, keepers go last
                ?: selection.selected.toList()
            selection.setSelection(ordered.take(maxSelection).toSet())
        }
    }

    BackHandler(enabled = selection.isActive) { selection.clear() }

    val tourController = LocalGuidedTourController.current
    // Pin tour targets at start: cluster contents can re-sort after a delete and we don't want
    // the bubble to jump to a different row mid-tour.
    var tourStartAttempted by remember { mutableStateOf(false) }
    var tourClusterId by remember { mutableStateOf<Duplicate.Cluster.Id?>(null) }
    var tourDeleteMarkRowId by remember { mutableStateOf<Duplicate.Id?>(null) }
    // Dedicated LazyListState for the toured cluster's LazyColumn so prepareDeleteMark can
    // scroll the delete-marker row into view even if it's below the fold.
    val tourListState = rememberLazyListState()
    val tourDef = remember(items, tourClusterId, tourDeleteMarkRowId) {
        DeduplicatorDetailsTour.definition(
            prepareDeleteMark = {
                val cluster = items.firstOrNull { it.identifier == tourClusterId } ?: return@definition
                val markId = tourDeleteMarkRowId ?: return@definition
                val elements = buildClusterElements(
                    cluster = cluster,
                    isDirectoryView = false,
                    collapsed = emptySet(),
                )
                val idx = elements.indexOfFirst { el ->
                    el is eu.darken.sdmse.deduplicator.ui.details.cluster.ClusterElement.DuplicateRow &&
                        el.duplicate.identifier == markId
                }
                if (idx >= 0) tourListState.animateScrollToItem(idx)
            },
        )
    }
    val tourEligible = items.isNotEmpty() && current?.isDirectoryView == false
    LaunchedEffect(tourEligible) {
        if (!tourEligible || tourStartAttempted) return@LaunchedEffect
        // Anchor on the route target if one was supplied (deep link into a specific cluster);
        // otherwise fall back to the first cluster.
        val anchorClusterId = current?.target ?: items.firstOrNull()?.identifier ?: return@LaunchedEffect
        val anchorIndex = items.indexOfFirst { it.identifier == anchorClusterId }
        if (anchorIndex < 0) return@LaunchedEffect
        // shouldStart() is false both when this tour is done/dismissed (permanent) and when another
        // tour is still active (transient). Don't set tourStartAttempted until it passes, so a
        // transient block can't permanently suppress this tour for the composition.
        if (!tourController.shouldStart(tourDef)) return@LaunchedEffect
        val cluster = items[anchorIndex]
        // Step 1 (cluster header) registers its target only on the pinned page. Deep links land on
        // a non-zero page the separate scroll effect is still settling — drive the scroll here and
        // await it so the header is composed before we start, instead of grace-skipping step 1.
        if (pagerState.currentPage != anchorIndex || pagerState.isScrollInProgress) {
            pagerState.scrollToPage(anchorIndex)
        }
        // Always pin the cluster — the header target is applied only to the pinned page. The
        // delete-mark row is opportunistic: when no group has a keeper-vs-non-keeper split there is
        // nothing to point at, so step 2 simply grace-skips (it no longer suppresses the whole tour).
        val deleteRow = cluster.groups
            .sortedByDescending { it.totalSize }
            .firstOrNull { it.keeperIdentifier != null && it.duplicates.size >= 2 }
            ?.let { group -> group.duplicates.firstOrNull { it.identifier != group.keeperIdentifier } }
        tourClusterId = cluster.identifier
        tourDeleteMarkRowId = deleteRow?.identifier
        tourStartAttempted = true
        tourController.start(tourDef)
    }

    SdmScaffold(
        topBar = {
            if (!selection.isActive) {
                SdmTopAppBar(
                    title = stringResource(CommonR.string.deduplicator_tool_name),
                    subtitle = stringResource(CommonR.string.general_details_label),
                    onNavigateUp = onNavigateUp,
                    actions = {
                        val icon = if (current?.isDirectoryView == true) {
                            Icons.AutoMirrored.TwoTone.FormatListBulleted
                        } else {
                            Icons.TwoTone.Folder
                        }
                        val label = stringResource(
                            if (current?.isDirectoryView == true) {
                                DeduplicatorR.string.deduplicator_view_mode_groups_label
                            } else {
                                DeduplicatorR.string.deduplicator_view_mode_directories_label
                            }
                        )
                        SdmTooltipIconButton(
                            icon = icon,
                            label = label,
                            onClick = onToggleDirectoryView,
                        )
                    },
                )
            } else {
                val clusterId = currentCluster?.identifier
                SdmSelectionTopAppBar(
                    selectedCount = selection.count,
                    onClearSelection = { selection.clear() },
                    actions = {
                        SdmDeleteAction(
                            enabled = clusterId != null,
                            onClick = {
                                if (clusterId != null) {
                                    val selectedIds = selection.selected
                                    onDeleteSelectedDuplicates(clusterId, selectedIds)
                                }
                            },
                        )
                        SdmExcludeAction(
                            enabled = clusterId != null,
                            onClick = {
                                if (clusterId != null) {
                                    val selectedIds = selection.selected
                                    selection.clear()
                                    onExcludeSelectedDuplicates(clusterId, selectedIds)
                                }
                            },
                        )
                        SdmSelectAllAction(
                            visible = selection.count < maxSelection,
                            onClick = { selection.setSelection(liveDupeIds.take(maxSelection).toSet()) },
                        )
                    },
                )
            }
        },
        snackbarHost = { SnackbarHost(snackbarHostState) },
    ) { paddingValues ->
        Box(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues),
        ) {
            ProgressOverlay(
                data = current?.progress,
                modifier = Modifier.fillMaxSize(),
            ) {
                if (current == null) {
                    SdmLoadingState()
                } else if (items.isEmpty()) {
                    SdmEmptyState()
                } else {
                    Column(modifier = Modifier.fillMaxSize()) {
                        SdmScrollableTabStrip(
                            selectedTabIndex = pagerState.currentPage.coerceIn(0, items.lastIndex),
                            tabCount = items.size,
                            onTabSelected = { index ->
                                coroutineScope.launch { pagerState.animateScrollToPage(index) }
                            },
                        ) { index ->
                            Text(
                                text = stringResource(
                                    DeduplicatorR.string.deduplicator_cluster_x_label,
                                    "#${index + 1}",
                                )
                            )
                        }
                        HorizontalPager(
                            state = pagerState,
                            modifier = Modifier.fillMaxSize(),
                            verticalAlignment = Alignment.Top,
                        ) { page ->
                            val cluster = items.getOrNull(page) ?: return@HorizontalPager
                            // Per-page list state (unconditional so the slot survives isTourCluster flips).
                            val pageListState = rememberLazyListState()
                            val collapsedForCluster = current?.collapsedDirs?.get(cluster.identifier) ?: emptySet()
                            val isTourCluster = cluster.identifier == tourClusterId
                            ClusterContent(
                                cluster = cluster,
                                isDirectoryView = current?.isDirectoryView == true,
                                collapsed = collapsedForCluster,
                                selection = selection,
                                selectionEnabled = cluster.identifier == currentCluster?.identifier,
                                onSelectionToggle = { id ->
                                    if (cluster.identifier != currentCluster?.identifier) return@ClusterContent
                                    if (selection.contains(id)) {
                                        selection.deselect(id)
                                    } else if (selection.count < maxSelection) {
                                        selection.select(id)
                                    }
                                },
                                onSelectionLongPress = { id ->
                                    if (cluster.identifier != currentCluster?.identifier) return@ClusterContent
                                    if (selection.count < maxSelection) selection.select(id)
                                },
                                onCollapseToggle = { dirId -> onCollapseToggle(cluster.identifier, dirId) },
                                onClusterDelete = { onClusterDelete(cluster.identifier) },
                                onClusterExclude = { onClusterExclude(cluster.identifier) },
                                onGroupDelete = { groupId -> onGroupDelete(cluster.identifier, groupId) },
                                onGroupView = onGroupView,
                                onDuplicateDelete = { id -> onDuplicateDelete(cluster.identifier, id) },
                                onDuplicatePreview = onDuplicatePreview,
                                onDirectoryDeleteAll = { dirGroup -> onDirectoryDeleteAll(cluster.identifier, dirGroup) },
                                listState = if (isTourCluster) tourListState else pageListState,
                                applyClusterHeaderTourTarget = isTourCluster,
                                tourDeleteMarkTarget = if (isTourCluster) tourDeleteMarkRowId else null,
                            )
                        }
                    }
                }
            }
        }
    }
}

@Preview2
@Composable
private fun DeduplicatorDetailsScreenEmptyPreview() {
    PreviewWrapper {
        DeduplicatorDetailsScreen(
            stateSource = MutableStateFlow(
                DeduplicatorDetailsViewModel.State(
                    items = emptyList(),
                    target = null,
                    progress = null,
                )
            ),
        )
    }
}
