package com.pixelized.desktop.lwa.ui.screen.settings

import androidx.compose.foundation.ScrollState
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.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.type
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.pixelized.desktop.lwa.ui.composable.key.KeyHandler
import com.pixelized.desktop.lwa.ui.navigation.screen.LocalScreenController
import com.pixelized.desktop.lwa.ui.navigation.screen.destination.SettingsDestination
import com.pixelized.desktop.lwa.ui.screen.settings.composable.SettingItemUio
import com.pixelized.desktop.lwa.ui.screen.settings.composable.SettingNumberItem
import com.pixelized.desktop.lwa.ui.screen.settings.composable.SettingNumberItemUio
import com.pixelized.desktop.lwa.ui.screen.settings.composable.SettingSection
import com.pixelized.desktop.lwa.ui.screen.settings.composable.SettingSectionUio
import com.pixelized.desktop.lwa.ui.screen.settings.composable.SettingToggleItem
import com.pixelized.desktop.lwa.ui.screen.settings.composable.SettingToggleItemUio
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.settings__reset_action
import lwacharactersheet.composeapp.generated.resources.settings__title
import org.jetbrains.compose.resources.stringResource
import org.koin.compose.viewmodel.koinViewModel

@Stable
object SettingsScreenDefault {
    @Stable
    val margin: PaddingValues = PaddingValues(horizontal = 16.dp)
    @Stable
    val padding: PaddingValues = PaddingValues(start = 16.dp, top = 8.dp, end = 8.dp, bottom = 8.dp)
}

@Composable
fun SettingsScreen(
    viewModel: SettingsViewModel = koinViewModel(),
) {
    val screen = LocalScreenController.current

    Surface {
        SettingsContent(
            modifier = Modifier.fillMaxSize(),
            items = viewModel.items,
            onBack = {
                screen.navigateBack()
            },
            onReset = viewModel::onReset
        )
    }

    SettingsKeyHandler(
        onDismissRequest = {
            screen.navigateBack()
        }
    )
}

@Composable
private fun SettingsContent(
    modifier: Modifier = Modifier,
    state: ScrollState = rememberScrollState(),
    spacing: Dp = 8.dp,
    items: List<SettingItemUio>,
    onBack: () -> Unit,
    onReset: () -> Unit,
) {
    Scaffold(
        modifier = modifier,
        topBar = {
            TopAppBar(
                title = {
                    Text(text = stringResource(Res.string.settings__title))
                },
                navigationIcon = {
                    IconButton(
                        onClick = onBack,
                    ) {
                        Icon(
                            imageVector = Icons.AutoMirrored.Default.ArrowBack,
                            contentDescription = null,
                        )
                    }
                },
                actions = {
                    TextButton(
                        onClick = onReset,
                    ) {
                        Text(text = stringResource(Res.string.settings__reset_action))
                    }
                },
            )
        },
        content = { paddingValues ->
            Column(
                modifier = Modifier
                    .verticalScroll(state = state)
                    .padding(paddingValues = paddingValues)
                    .padding(bottom = spacing),
                verticalArrangement = Arrangement.spacedBy(space = spacing),
            ) {
                items.forEach {
                    when (it) {
                        is SettingSectionUio -> SettingSection(item = it)
                        is SettingToggleItemUio -> SettingToggleItem(item = it)
                        is SettingNumberItemUio -> SettingNumberItem(item = it)
                    }
                }
            }
        }
    )
}

@Composable
private fun SettingsKeyHandler(
    onDismissRequest: () -> Unit,
) {
    KeyHandler {
        when {
            it.type == KeyEventType.KeyDown && it.key == Key.Escape -> {
                onDismissRequest()
                true
            }

            else -> false
        }
    }
}

private fun NavHostController.navigateBack() = popBackStack(
    route = SettingsDestination.baseRoute(),
    inclusive = true,
)