package eu.gaudian.translator.view.hints

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 eu.gaudian.translator.R
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.settings.SettingsRoutes

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

/**
 * Hints overview screen that lists all available hint screens in organized categories
 */
@Composable
fun HintsOverviewScreen(
    navController: NavController,
    modifier: Modifier = Modifier
) {
    val showExperimental = LocalShowExperimentalFeatures.current

    // Get hints using the new function-based approach
    val importHint = HintDefinition.VOCABULARY_GENERATE_AI.hint()
    val addModelScanHint = HintDefinition.ADD_MODEL_SCAN.hint()
    val dictionaryOptionsHint = HintDefinition.DICTIONARY_OPTIONS.hint()
    val translationScreenHint = HintDefinition.TRANSLATION.hint()
    val categoryHint = HintDefinition.CATEGORY.hint()
    val learningStagesHint = HintDefinition.LEARNING_STAGES.hint()
    val sortingScreenHint = HintDefinition.SORTING.hint()
    val vocabularyProgressHint = HintDefinition.VOCABULARY_PROGRESS.hint()
    val apiKeyHint = HintDefinition.API_KEY.hint()


    val hintGroups = remember(showExperimental, importHint, addModelScanHint, dictionaryOptionsHint, translationScreenHint) {
        val allGroups = listOf(
            R.string.hint_hints_header_basics to listOf(
                HintItem(categoryHint.titleRes, AppIcons.Category, SettingsRoutes.HINTS_CATEGORIES),
                HintItem(learningStagesHint.titleRes, AppIcons.Stages, SettingsRoutes.HINTS_STAGES),
                HintItem(translationScreenHint.titleRes, AppIcons.Translate, SettingsRoutes.HINTS_TRANSLATION)
            ),
            R.string.hint_hints_header_vocabulary to listOf(
                HintItem(importHint.titleRes, AppIcons.Vocabulary, SettingsRoutes.HINTS_IMPORT),
                HintItem(sortingScreenHint.titleRes, AppIcons.Sort, SettingsRoutes.HINTS_SORTING),
                HintItem(dictionaryOptionsHint.titleRes, AppIcons.Dictionary, SettingsRoutes.HINTS_DICTIONARY),
                HintItem(vocabularyProgressHint.titleRes, AppIcons.Stages, SettingsRoutes.HINTS_VOCABULARY_PROGRESS)
            ),
            R.string.hint_hints_header_advanced to listOf(
                HintItem(addModelScanHint.titleRes, AppIcons.AI, SettingsRoutes.HINTS_SCAN),
                HintItem(apiKeyHint.titleRes, AppIcons.ApiKey, SettingsRoutes.HINTS_API)
            )
        )

        allGroups
    }

    AppOutlinedCard {
        AppScaffold(
            topBar = {
                AppTopAppBar(
                    title = stringResource(R.string.hint_title_hints_overview)
                )
            }
        ) { paddingValues ->
            LazyColumn(modifier = modifier.padding(paddingValues).padding(bottom = 0.dp)) {
                // Introduction section
                item {
                    Column(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(16.dp)
                    ) {
                        Text(
                            text = stringResource(R.string.hint_hints_overview_intro),
                            style = MaterialTheme.typography.titleMedium,
                            color = MaterialTheme.colorScheme.primary
                        )
                        Text(
                            text = stringResource(R.string.hint_hints_overview_description),
                            style = MaterialTheme.typography.bodyMedium,
                            color = MaterialTheme.colorScheme.onSurfaceVariant,
                            modifier = Modifier.padding(top = 8.dp)
                        )
                    }
                }

                // Hint categories
                hintGroups.forEach { (headerRes, hints) ->
                    item {
                        HintHeader(title = stringResource(id = headerRes))
                    }
                    item {
                        AppCard(
                            modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
                        ) {
                            Column {
                                hints.forEachIndexed { index, hint ->
                                    HintListItem(
                                        title = stringResource(id = hint.titleRes),
                                        icon = hint.icon,
                                        onClick = { navController.navigate(hint.route) }
                                    )
                                    if (index < hints.lastIndex) {
                                        HorizontalDivider(modifier = Modifier.padding(start = 56.dp))
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}



@Composable
private fun HintHeader(
    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)
    )
}

@Composable
private fun HintListItem(
    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)
    )
}


