package eu.darken.sdmse.analyzer.ui.storage.storage

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.twotone.ArrowBack
import androidx.compose.material.icons.twotone.Refresh
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import eu.darken.sdmse.common.compose.layout.SdmScaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import eu.darken.sdmse.analyzer.R
import eu.darken.sdmse.analyzer.ui.StorageContentRoute
import eu.darken.sdmse.analyzer.ui.storage.storage.categories.AppCategoryCard
import eu.darken.sdmse.analyzer.ui.storage.storage.categories.MediaCategoryCard
import eu.darken.sdmse.analyzer.ui.storage.storage.categories.SystemCategoryCard
import eu.darken.sdmse.common.compose.layout.ScrollAwareFab
import eu.darken.sdmse.common.compose.layout.SdmListDefaults
import eu.darken.sdmse.common.compose.layout.SdmTooltipIconButton
import eu.darken.sdmse.common.compose.layout.plusBottom
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.error.ErrorEventHandler
import eu.darken.sdmse.common.getSpanCount
import eu.darken.sdmse.common.navigation.NavigationEventHandler
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow

@Composable
fun StorageContentScreenHost(
    route: StorageContentRoute,
    vm: StorageContentViewModel = hiltViewModel(),
) {
    ErrorEventHandler(vm)
    NavigationEventHandler(vm)

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

    StorageContentScreen(
        stateSource = vm.state,
        onCategoryClick = vm::onCategoryClick,
        onNavigateBack = vm::onNavigateBack,
        onExitNotFound = vm::exitOnNotFound,
        onRefresh = vm::refresh,
    )
}

@Composable
internal fun StorageContentScreen(
    stateSource: Flow<StorageContentViewModel.State> = MutableStateFlow(StorageContentViewModel.State.Loading),
    onCategoryClick: (StorageContentViewModel.Row) -> Unit = {},
    onNavigateBack: () -> Unit = {},
    onExitNotFound: () -> Unit = {},
    onRefresh: () -> Unit = {},
) {
    val state by stateSource.collectAsStateWithLifecycle(initialValue = StorageContentViewModel.State.Loading)
    val context = LocalContext.current
    val gridState = rememberLazyGridState()

    BackHandler(enabled = true) { onNavigateBack() }

    when (val s = state) {
        StorageContentViewModel.State.NotFound -> {
            LaunchedEffect(Unit) { onExitNotFound() }
            SdmScaffold(
                topBar = {
                    TopAppBar(
                        title = { Text(stringResource(R.string.analyzer_storage_content_title)) },
                        navigationIcon = {
                            SdmTooltipIconButton(
                                icon = Icons.AutoMirrored.TwoTone.ArrowBack,
                                label = stringResource(eu.darken.sdmse.common.R.string.general_navigate_up_action),
                                onClick = onNavigateBack,
                            )
                        },
                    )
                },
            ) { _ -> }
        }

        StorageContentViewModel.State.Loading -> {
            SdmScaffold(
                topBar = {
                    TopAppBar(
                        title = { Text(stringResource(R.string.analyzer_storage_content_title)) },
                        navigationIcon = {
                            SdmTooltipIconButton(
                                icon = Icons.AutoMirrored.TwoTone.ArrowBack,
                                label = stringResource(eu.darken.sdmse.common.R.string.general_navigate_up_action),
                                onClick = onNavigateBack,
                            )
                        },
                    )
                },
            ) { paddingValues ->
                ProgressOverlay(
                    data = null,
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(paddingValues),
                ) { }
            }
        }

        is StorageContentViewModel.State.Ready -> {
            SdmScaffold(
                topBar = {
                    TopAppBar(
                        title = {
                            Column {
                                Text(stringResource(R.string.analyzer_storage_content_title))
                                Text(
                                    text = s.storage.label.get(context),
                                    style = MaterialTheme.typography.bodySmall,
                                )
                            }
                        },
                        navigationIcon = {
                            SdmTooltipIconButton(
                                icon = Icons.AutoMirrored.TwoTone.ArrowBack,
                                label = stringResource(eu.darken.sdmse.common.R.string.general_navigate_up_action),
                                onClick = onNavigateBack,
                            )
                        },
                    )
                },
                floatingActionButton = {
                    ScrollAwareFab(scrollState = gridState, visible = s.progress == null) {
                        FloatingActionButton(onClick = onRefresh) {
                            Icon(
                                Icons.TwoTone.Refresh,
                                contentDescription = stringResource(eu.darken.sdmse.common.R.string.general_refresh_action),
                            )
                        }
                    }
                },
            ) { paddingValues ->
                ProgressOverlay(
                    data = s.progress,
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(paddingValues),
                ) {
                    LazyVerticalGrid(
                        columns = GridCells.Fixed(context.getSpanCount()),
                        state = gridState,
                        modifier = Modifier.fillMaxSize(),
                        contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp)
                            .plusBottom(SdmListDefaults.FabClearance),
                        horizontalArrangement = Arrangement.spacedBy(8.dp),
                        verticalArrangement = Arrangement.spacedBy(8.dp),
                    ) {
                        items(s.rows.orEmpty(), key = { it::class.java.name }) { row ->
                            when (row) {
                                is StorageContentViewModel.Row.Apps -> AppCategoryCard(
                                    row = row,
                                    onClick = { onCategoryClick(row) },
                                )
                                is StorageContentViewModel.Row.Media -> MediaCategoryCard(
                                    row = row,
                                    onClick = { onCategoryClick(row) },
                                )
                                is StorageContentViewModel.Row.System -> SystemCategoryCard(
                                    row = row,
                                    onClick = { onCategoryClick(row) },
                                )
                            }
                        }
                    }
                }
            }
        }
    }
}

@Preview2
@Composable
private fun StorageContentScreenLoadingPreview() {
    PreviewWrapper {
        StorageContentScreen()
    }
}
