@file:Suppress("HardCodedStringLiteral")

package eu.gaudian.translator.view.settings

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
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.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import eu.gaudian.translator.view.composable.AppCard
import eu.gaudian.translator.view.composable.AppCheckbox
import eu.gaudian.translator.view.composable.AppIcons
import eu.gaudian.translator.view.composable.AppScaffold
import eu.gaudian.translator.view.composable.AppSwitch
import eu.gaudian.translator.view.composable.Compass
import eu.gaudian.translator.view.composable.HintIcons
import eu.gaudian.translator.view.composable.Info
import eu.gaudian.translator.view.composable.Key
import eu.gaudian.translator.view.composable.Lightbulb
import eu.gaudian.translator.view.composable.MagicWand
import eu.gaudian.translator.view.composable.MagnifyingGlass
import eu.gaudian.translator.view.composable.PointingHand
import eu.gaudian.translator.view.composable.PrimaryButton
import eu.gaudian.translator.view.composable.Puzzle
import eu.gaudian.translator.view.composable.QuestionCircle
import eu.gaudian.translator.view.composable.Scroll
import eu.gaudian.translator.view.composable.SecondaryButton

@Composable
fun ThemePreviewScreen(navController: NavController) {
    AppScaffold(
        topBar = {
            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically
            ) {
                IconButton(onClick = { navController.popBackStack() }) {
                    Icon(AppIcons.ArrowBack, contentDescription = "Back")
                }
                Text(
                    text = "Theme Preview",
                    style = MaterialTheme.typography.titleLarge,
                    fontWeight = FontWeight.SemiBold
                )
            }
        }
    ) { padding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .verticalScroll(rememberScrollState())
                .padding(padding)
                .padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            // Buttons
            AppCard {
                Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
                    Text("Buttons", style = MaterialTheme.typography.titleMedium)
                    Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
                        PrimaryButton(onClick = {}, text = "Primary")
                        SecondaryButton(onClick = {}, text = "Secondary")
                    }
                }
            }

            // Switch & Checkbox
            AppCard {
                Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
                    Text("Toggles", style = MaterialTheme.typography.titleMedium)
                    var sw by remember { mutableStateOf(true) }
                    var cb by remember { mutableStateOf(false) }
                    Row(horizontalArrangement = Arrangement.spacedBy(16.dp), verticalAlignment = Alignment.CenterVertically) {
                        AppSwitch(checked = sw, onCheckedChange = { sw = it })
                        AppCheckbox(checked = cb, onCheckedChange = { cb = it })
                    }
                }
            }

            // Hint icons
            AppCard {
                Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
                    Text("Hint icons", style = MaterialTheme.typography.titleMedium)
                    Row(horizontalArrangement = Arrangement.spacedBy(12.dp), verticalAlignment = Alignment.CenterVertically) {
                        Icon(HintIcons.Lightbulb, contentDescription = "Lightbulb", modifier = Modifier.size(28.dp), tint = MaterialTheme.colorScheme.primary)
                        Text("Lightbulb")
                    }
                    Row(horizontalArrangement = Arrangement.spacedBy(12.dp), verticalAlignment = Alignment.CenterVertically) {
                        Icon(HintIcons.QuestionCircle, contentDescription = "QuestionCircle", modifier = Modifier.size(28.dp), tint = MaterialTheme.colorScheme.primary)
                        Text("QuestionCircle")
                    }
                    Row(horizontalArrangement = Arrangement.spacedBy(12.dp), verticalAlignment = Alignment.CenterVertically) {
                        Icon(HintIcons.MagnifyingGlass, contentDescription = "MagnifyingGlass", modifier = Modifier.size(28.dp), tint = MaterialTheme.colorScheme.primary)
                        Text("MagnifyingGlass")
                    }
                    Row(horizontalArrangement = Arrangement.spacedBy(12.dp), verticalAlignment = Alignment.CenterVertically) {
                        Icon(HintIcons.Key, contentDescription = "Key", modifier = Modifier.size(28.dp), tint = MaterialTheme.colorScheme.primary)
                        Text("Key")
                    }
                    Row(horizontalArrangement = Arrangement.spacedBy(12.dp), verticalAlignment = Alignment.CenterVertically) {
                        Icon(HintIcons.Info, contentDescription = "Info", modifier = Modifier.size(28.dp), tint = MaterialTheme.colorScheme.primary)
                        Text("Info")
                    }
                    Row(horizontalArrangement = Arrangement.spacedBy(12.dp), verticalAlignment = Alignment.CenterVertically) {
                        Icon(HintIcons.Puzzle, contentDescription = "Puzzle", modifier = Modifier.size(28.dp), tint = MaterialTheme.colorScheme.primary)
                        Text("Puzzle")
                    }
                    Row(horizontalArrangement = Arrangement.spacedBy(12.dp), verticalAlignment = Alignment.CenterVertically) {
                        Icon(HintIcons.MagicWand, contentDescription = "MagicWand", modifier = Modifier.size(28.dp), tint = MaterialTheme.colorScheme.primary)
                        Text("MagicWand")
                    }
                    Row(horizontalArrangement = Arrangement.spacedBy(12.dp), verticalAlignment = Alignment.CenterVertically) {
                        Icon(HintIcons.Compass, contentDescription = "Compass", modifier = Modifier.size(28.dp), tint = MaterialTheme.colorScheme.primary)
                        Text("Compass")
                    }
                    Row(horizontalArrangement = Arrangement.spacedBy(12.dp), verticalAlignment = Alignment.CenterVertically) {
                        Icon(HintIcons.PointingHand, contentDescription = "PointingHand", modifier = Modifier.size(28.dp), tint = MaterialTheme.colorScheme.primary)
                        Text("PointingHand")
                    }
                    Row(horizontalArrangement = Arrangement.spacedBy(12.dp), verticalAlignment = Alignment.CenterVertically) {
                        Icon(HintIcons.Scroll, contentDescription = "Scroll", modifier = Modifier.size(28.dp), tint = MaterialTheme.colorScheme.primary)
                        Text("Scroll")
                    }
                }
            }

            // Color swatches from Material color scheme (backed by ThemeColorSet)
            AppCard {
                Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) {
                    Text("Theme colors", style = MaterialTheme.typography.titleMedium)
                    val cs = MaterialTheme.colorScheme
                    val items: List<Triple<String, Color, Color>> = listOf(
                        Triple("primary", cs.primary, cs.onPrimary),
                        Triple("secondary", cs.secondary, cs.onSecondary),
                        Triple("tertiary", cs.tertiary, cs.onTertiary),
                        Triple("primaryContainer", cs.primaryContainer, cs.onPrimaryContainer),
                        Triple("secondaryContainer", cs.secondaryContainer, cs.onSecondaryContainer),
                        Triple("tertiaryContainer", cs.tertiaryContainer, cs.onTertiaryContainer),
                        Triple("error", cs.error, cs.onError),
                        Triple("errorContainer", cs.errorContainer, cs.onErrorContainer),
                        Triple("background", cs.background, cs.onBackground),
                        Triple("surface", cs.surface, cs.onSurface),
                        Triple("surfaceVariant", cs.surfaceVariant, cs.onSurfaceVariant),
                        Triple("outline", cs.outline, cs.onSurface),
                        Triple("outlineVariant", cs.outlineVariant, cs.onSurface),
                        Triple("scrim", cs.scrim, cs.onSurface),
                        Triple("inverseSurface", cs.inverseSurface, cs.inverseOnSurface),
                        Triple("inversePrimary", cs.inversePrimary, cs.onPrimary),
                        Triple("surfaceDim", cs.surfaceDim, cs.onSurface),
                        Triple("surfaceBright", cs.surfaceBright, cs.onSurface),
                        Triple("surfaceContainerLowest", cs.surfaceContainerLowest, cs.onSurface),
                        Triple("surfaceContainerLow", cs.surfaceContainerLow, cs.onSurface),
                        Triple("surfaceContainer", cs.surfaceContainer, cs.onSurface),
                        Triple("surfaceContainerHigh", cs.surfaceContainerHigh, cs.onSurface),
                        Triple("surfaceContainerHighest", cs.surfaceContainerHighest, cs.onSurface),
                    )
                    items.forEach { (name, bg, fg) ->
                        ColorRow(name = name, bg = bg, fg = fg)
                    }
                }
            }
        }
    }
}

@Composable
private fun ColorRow(name: String, bg: Color, fg: Color) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .height(48.dp)
            .background(bg)
            .padding(horizontal = 12.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        Text(text = name, color = appropriateTextColor(bg, fg), fontWeight = FontWeight.Medium)
        Box(
            modifier = Modifier
                .size(28.dp)
                .background(appropriateTextColor(bg, fg))
        )
    }
}

private fun appropriateTextColor(bg: Color, suggested: Color): Color {
    // prefer suggested, but ensure sufficient contrast by falling back to black/white heuristic
    val luminance = 0.299f * bg.red + 0.587f * bg.green + 0.114f * bg.blue
    val fallback = if (luminance < 0.5f) Color.White else Color.Black
    return if (suggested.alpha > 0f) suggested else fallback
}