package eu.gaudian.translator.view.settings

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.ListItem
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import eu.gaudian.translator.R
import eu.gaudian.translator.ui.theme.ThemePreviews
import eu.gaudian.translator.view.LocalShowExperimentalFeatures
import eu.gaudian.translator.view.composable.AppCard
import eu.gaudian.translator.view.composable.AppIcons
import eu.gaudian.translator.view.composable.AppOutlinedCard
import eu.gaudian.translator.view.composable.AppScaffold
import eu.gaudian.translator.view.composable.AppTopAppBar
import eu.gaudian.translator.view.composable.Screen

private data class Setting(val titleRes: Int, val icon: ImageVector, val route: String)

@Composable
fun MainSettingsScreen(
    navController: NavController,
    modifier: Modifier = Modifier
) {
    val showExperimental = LocalShowExperimentalFeatures.current

    val settingsGroups = remember(showExperimental) {
        val allGroups = listOf(
            R.string.label_general to listOf(
                Setting(R.string.label_about, AppIcons.Info, SettingsRoutes.ABOUT),
                Setting(R.string.label_general, AppIcons.Tune, SettingsRoutes.GENERAL),
                Setting(R.string.settings_title_connection, AppIcons.ApiKey, SettingsRoutes.API_KEY),
                Setting(R.string.label_appearance, AppIcons.Appearance, SettingsRoutes.LAYOUT_OPTIONS),
                Setting(R.string.settings_title_voice, AppIcons.TextToSpeech, SettingsRoutes.TTS_OPTIONS),
                Setting(R.string.label_logs, AppIcons.Log, SettingsRoutes.LOGS),
                Setting(R.string.label_languages, AppIcons.Language, SettingsRoutes.LANGUAGE_OPTIONS),
                //Setting(R.string.hint_settings_title_help, AppIcons.Info, SettingsRoutes.HINTS_OVERVIEW)

            ),
            R.string.settings_header_translator to listOf(
                Setting(R.string.label_translation, AppIcons.Translate, SettingsRoutes.CUSTOM_TRANSLATION_PROMPT)
            ),
            R.string.label_dictionary to listOf(
                Setting(R.string.label_dictionary, AppIcons.Dictionary, SettingsRoutes.DICTIONARY_OPTIONS)
            ),
            R.string.label_vocabulary to listOf(
                Setting(R.string.settings_title_vocab_progress, AppIcons.Stages, SettingsRoutes.VOCABULARY_OPTIONS),
                Setting(R.string.settings_title_vocab_prompt, AppIcons.EditNote, SettingsRoutes.VOCABULARY_PROMPT_OPTIONS),
                Setting(R.string.settings_title_vocab_repo, AppIcons.Repository, SettingsRoutes.VOCABULARY_REPOSITORY_OPTIONS)
            ),
            R.string.label_exercises to listOf(
                Setting(R.string.settings_title_exercise_generation, AppIcons.Exercise, SettingsRoutes.EXERCISE_OPTIONS)
            )
        )

        if (showExperimental) {
            allGroups
        } else {
            allGroups.map { (headerRes, settings) ->
                val filteredSettings = settings.filterNot { setting ->
                    setting.route == SettingsRoutes.DEV_OPTIONS
                }
                headerRes to filteredSettings
            }.filterNot { (headerRes, _) ->
                headerRes == R.string.label_exercises
            }
        }
    }

    AppOutlinedCard {
        AppScaffold(
            topBar = {
                AppTopAppBar(
                    title =stringResource(R.string.title_settings),
                    onNavigateBack = {
                        if (!navController.popBackStack()) {
                            navController.navigate(Screen.Home.route) {
                                launchSingleTop = true
                                restoreState = false
                            }
                        }
                    }
                )
            }
        ) { paddingValues ->
            LazyColumn(modifier = modifier.padding(paddingValues).padding(bottom = 0.dp)) {
                settingsGroups.forEach { (headerRes, settings) ->
                    item {
                        // The string is resolved here, in the composable context
                        SettingsHeader(title = stringResource(id = headerRes))
                    }
                    item {
                        AppCard(
                            modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
                        ) {
                            Column {
                                settings.forEachIndexed { index, setting ->
                                    SettingsItem(
                                        // The string is resolved here, in the composable context
                                        title = stringResource(id = setting.titleRes),
                                        icon = setting.icon,
                                        onClick = { navController.navigate(setting.route) }
                                    )
                                    if (index < settings.lastIndex) {
                                        HorizontalDivider(modifier = Modifier.padding(start = 56.dp))
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

@ThemePreviews
@Composable
fun MainSettingsScreenPreview() {
    MainSettingsScreen(navController = rememberNavController())
}

@Composable
private fun SettingsHeader(
    title: String,
    modifier: Modifier = Modifier
) {
    Text(
        text = title,
        style = MaterialTheme.typography.labelLarge,
        color = MaterialTheme.colorScheme.primary,
        modifier = modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp, vertical = 16.dp)
    )
}

@ThemePreviews
@Composable
fun SettingsHeaderPreview() {
    SettingsHeader(title = stringResource(R.string.general_settings))
}

@Composable
private fun SettingsItem(
    title: String,
    icon: ImageVector,
    onClick: () -> Unit,
) {
    ListItem(
        headlineContent = { Text(title) },
        leadingContent = {
            Icon(
                imageVector = icon,
                contentDescription = null,
                tint = MaterialTheme.colorScheme.onSurfaceVariant
            )
        },
        colors = ListItemDefaults.colors(
            containerColor = MaterialTheme.colorScheme.surfaceContainer
        ),
        modifier = Modifier.clickable(onClick = onClick)
    )
}

@ThemePreviews
@Composable
fun SettingsItemPreview() {
    SettingsItem(
        title = stringResource(R.string.label_about),
        icon = AppIcons.Info,
        onClick = {}
    )
}