package eu.darken.sdmse.main.ui.areas

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.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.twotone.ArrowBack
import androidx.compose.material.icons.twotone.Folder
import androidx.compose.material.icons.twotone.Info
import androidx.compose.material.icons.twotone.Refresh
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.HorizontalDivider
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 eu.darken.sdmse.common.compose.preview.Preview2
import eu.darken.sdmse.common.compose.preview.PreviewWrapper
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.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.R
import eu.darken.sdmse.common.areas.DataArea
import eu.darken.sdmse.common.areas.label
import eu.darken.sdmse.common.compose.dialog.SdmConfirmDialog
import eu.darken.sdmse.common.compose.dialog.SdmDialogAction
import eu.darken.sdmse.common.compose.layout.SdmTooltipIconButton
import eu.darken.sdmse.common.error.ErrorEventHandler
import eu.darken.sdmse.common.navigation.NavigationEventHandler
import eu.darken.sdmse.common.R as CommonR

@Composable
fun DataAreasScreenHost(
    vm: DataAreasViewModel = hiltViewModel(),
) {
    ErrorEventHandler(vm)
    NavigationEventHandler(vm)
    val state by vm.state.collectAsStateWithLifecycle()

    DataAreasScreen(
        state = state,
        onReload = vm::reloadDataAreas,
        onNavigateUp = vm::navUp,
        onOpenDocumentation = vm::openDocumentation,
    )
}

@Composable
internal fun DataAreasScreen(
    state: DataAreasViewModel.State = DataAreasViewModel.State(),
    onReload: () -> Unit = {},
    onNavigateUp: () -> Unit = {},
    onOpenDocumentation: () -> Unit = {},
) {
    var showInfo by remember { mutableStateOf(false) }

    if (showInfo) {
        SdmConfirmDialog(
            message = stringResource(R.string.data_areas_description),
            onDismissRequest = { showInfo = false },
            positive = SdmDialogAction(
                label = stringResource(android.R.string.ok),
                onClick = { showInfo = false },
            ),
            neutral = SdmDialogAction(
                label = stringResource(CommonR.string.general_more_info_action),
                onClick = {
                    onOpenDocumentation()
                    showInfo = false
                },
            ),
        )
    }

    SdmScaffold(
        topBar = {
            TopAppBar(
                title = {
                    Column {
                        Text(stringResource(R.string.data_areas_label))
                        Text(
                            text = stringResource(CommonR.string.general_details_label),
                            style = MaterialTheme.typography.labelMedium,
                            color = MaterialTheme.colorScheme.onSurfaceVariant,
                        )
                    }
                },
                navigationIcon = {
                    SdmTooltipIconButton(
                        icon = Icons.AutoMirrored.TwoTone.ArrowBack,
                        label = stringResource(CommonR.string.general_navigate_up_action),
                        onClick = onNavigateUp,
                    )
                },
                actions = {
                    if (state.isReloading) {
                        Box(
                            modifier = Modifier.size(48.dp),
                            contentAlignment = Alignment.Center,
                        ) {
                            CircularProgressIndicator(
                                modifier = Modifier.size(24.dp),
                                strokeWidth = 3.dp,
                            )
                        }
                    } else if (state.allowReload) {
                        SdmTooltipIconButton(
                            icon = Icons.TwoTone.Refresh,
                            label = stringResource(CommonR.string.general_refresh_action),
                            onClick = onReload,
                        )
                    }
                    SdmTooltipIconButton(
                        icon = Icons.TwoTone.Info,
                        label = stringResource(CommonR.string.general_info_label),
                        onClick = { showInfo = true },
                    )
                },
            )
        },
    ) { paddingValues ->
        val areas = state.areas
        if (areas == null) {
            CircularProgressIndicator(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(paddingValues)
                    .padding(16.dp),
            )
        } else {
            LazyColumn(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(paddingValues),
            ) {
                items(areas.toList(), key = { it.path.path }) { area ->
                    DataAreaRow(area = area)
                    HorizontalDivider()
                }
            }
        }
    }
}

@Composable
private fun DataAreaRow(area: DataArea) {
    val context = LocalContext.current
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp, vertical = 12.dp),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Icon(
            imageVector = Icons.TwoTone.Folder,
            contentDescription = null,
            modifier = Modifier.size(24.dp),
            tint = MaterialTheme.colorScheme.onSurfaceVariant,
        )
        Spacer(modifier = Modifier.width(16.dp))
        Column(modifier = Modifier.weight(1f)) {
            Text(
                text = area.type.label.get(context),
                style = MaterialTheme.typography.bodyLarge,
            )
            Text(
                text = area.path.userReadablePath.get(context),
                style = MaterialTheme.typography.bodySmall,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
            )
        }
    }
}

@Preview2
@Composable
private fun DataAreasScreenPreview() {
    PreviewWrapper {
        DataAreasScreen()
    }
}
