package app.closer.ui.components

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Chat
import androidx.compose.material.icons.filled.AttachMoney
import androidx.compose.material.icons.filled.CalendarToday
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.People
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material.icons.filled.Psychology
import androidx.compose.material.icons.filled.QuestionAnswer
import androidx.compose.material.icons.filled.Shield
import androidx.compose.material.icons.filled.Star
import androidx.compose.material.icons.filled.Sync
import androidx.compose.material.icons.filled.Timeline
import androidx.compose.material.icons.filled.Visibility
import androidx.compose.material.icons.filled.Warning
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import app.closer.ui.theme.CloserPalette

data class GlyphStyle(
    val icon: ImageVector,
    val tint: Color,
    val container: Color
)

@Composable
fun CategoryGlyph(
    categoryId: String,
    modifier: Modifier = Modifier,
    iconName: String? = null,
    locked: Boolean = false,
    size: Dp = 48.dp,
    iconSize: Dp = 23.dp
) {
    val style = categoryGlyphStyle(categoryId = categoryId, iconName = iconName, locked = locked)

    Surface(
        modifier = modifier.size(size),
        shape = RoundedCornerShape((size.value * 0.36f).dp),
        color = style.container
    ) {
        Box(contentAlignment = Alignment.Center) {
            Icon(
                imageVector = style.icon,
                contentDescription = null,
                tint = style.tint,
                modifier = Modifier.size(iconSize)
            )
        }
    }
}

@Composable
fun StatusGlyph(
    icon: ImageVector,
    modifier: Modifier = Modifier,
    tint: Color = CloserPalette.PurpleDeep,
    container: Color = CloserPalette.PurpleMist,
    size: Dp = 72.dp,
    iconSize: Dp = 34.dp,
    shadowElevation: Dp = 0.dp
) {
    Surface(
        modifier = modifier.size(size),
        shape = RoundedCornerShape((size.value * 0.34f).dp),
        color = container,
        shadowElevation = shadowElevation
    ) {
        Box(contentAlignment = Alignment.Center) {
            Icon(
                imageVector = icon,
                contentDescription = null,
                tint = tint,
                modifier = Modifier.size(iconSize)
            )
        }
    }
}

@Composable
fun ResultGlyph(
    isPositive: Boolean,
    modifier: Modifier = Modifier,
    isClose: Boolean = false,
    size: Dp = 32.dp
) {
    val icon = when {
        isPositive -> Icons.Filled.Check
        isClose -> Icons.Filled.Timeline
        else -> Icons.Filled.Close
    }
    val tint = when {
        isPositive -> CloserPalette.Evergreen
        isClose -> CloserPalette.Gold
        else -> CloserPalette.Danger
    }
    val container = when {
        isPositive -> CloserPalette.Evergreen.copy(alpha = 0.12f)
        isClose -> CloserPalette.Gold.copy(alpha = 0.12f)
        else -> CloserPalette.Danger.copy(alpha = 0.12f)
    }

    StatusGlyph(
        icon = icon,
        modifier = modifier,
        tint = tint,
        container = container,
        size = size,
        iconSize = (size.value * 0.58f).dp
    )
}

@Composable
fun categoryGlyphStyle(
    categoryId: String,
    iconName: String? = null,
    locked: Boolean = false
): GlyphStyle {
    if (locked) {
        return GlyphStyle(
            icon = Icons.Filled.Lock,
            tint = MaterialTheme.colorScheme.outline,
            container = CloserPalette.PurpleMist
        )
    }

    val key = (iconName ?: categoryId).lowercase().trim()
    return when (key) {
        "communication", "chat", "question", "questions" -> GlyphStyle(
            icon = Icons.AutoMirrored.Filled.Chat,
            tint = CloserPalette.PurpleDeep,
            container = CloserPalette.PurpleMist
        )
        "emotional_intimacy", "intimacy", "heart" -> GlyphStyle(
            icon = Icons.Filled.Favorite,
            tint = CloserPalette.PinkAccentDeep,
            container = CloserPalette.PinkMist
        )
        "physical_intimacy", "sex_and_desire", "sexual_preferences", "desire", "sex" -> GlyphStyle(
            icon = Icons.Filled.FavoriteBorder,
            tint = CloserPalette.Romantic,
            container = CloserPalette.Romantic.copy(alpha = 0.12f)
        )
        "trust", "rebuilding_trust", "privacy" -> GlyphStyle(
            icon = Icons.Filled.Shield,
            tint = CloserPalette.PurpleDeep,
            container = CloserPalette.PurpleGlow
        )
        "conflict", "conflict_repair", "repair" -> GlyphStyle(
            icon = Icons.Filled.Sync,
            tint = CloserPalette.PurpleRich,
            container = CloserPalette.PurpleMist
        )
        "future", "timeline" -> GlyphStyle(
            icon = Icons.Filled.Timeline,
            tint = CloserPalette.PurpleDeep,
            container = CloserPalette.PurpleSoft
        )
        "money", "payments" -> GlyphStyle(
            icon = Icons.Filled.AttachMoney,
            tint = CloserPalette.PurpleDeep,
            container = CloserPalette.PurpleMist
        )
        "home_life", "home" -> GlyphStyle(
            icon = Icons.Filled.Home,
            tint = CloserPalette.PurpleRich,
            container = CloserPalette.PurpleGlow
        )
        "gratitude", "star" -> GlyphStyle(
            icon = Icons.Filled.Star,
            tint = CloserPalette.PinkAccentDeep,
            container = CloserPalette.PinkMist
        )
        "parenting", "family", "people" -> GlyphStyle(
            icon = Icons.Filled.People,
            tint = CloserPalette.PurpleDeep,
            container = CloserPalette.PurpleSoft
        )
        "date_night", "date", "calendar" -> GlyphStyle(
            icon = Icons.Filled.CalendarToday,
            tint = CloserPalette.PinkAccentDeep,
            container = CloserPalette.PinkMist
        )
        "fun", "play" -> GlyphStyle(
            icon = Icons.Filled.PlayArrow,
            tint = CloserPalette.PurpleDeep,
            container = CloserPalette.PurpleGlow
        )
        "stress", "difficult_conversations", "boundaries" -> GlyphStyle(
            icon = Icons.Filled.Warning,
            tint = CloserPalette.Danger,
            container = CloserPalette.Danger.copy(alpha = 0.10f)
        )
        "values", "marriage" -> GlyphStyle(
            icon = Icons.Filled.QuestionAnswer,
            tint = CloserPalette.PurpleDeep,
            container = CloserPalette.PurpleMist
        )
        "profile", "person" -> GlyphStyle(
            icon = Icons.Filled.Person,
            tint = CloserPalette.PurpleDeep,
            container = CloserPalette.PurpleMist
        )
        "reveal", "visibility" -> GlyphStyle(
            icon = Icons.Filled.Visibility,
            tint = CloserPalette.PurpleDeep,
            container = CloserPalette.PurpleGlow
        )
        "predict", "psychology" -> GlyphStyle(
            icon = Icons.Filled.Psychology,
            tint = CloserPalette.PurpleDeep,
            container = CloserPalette.PurpleGlow
        )
        else -> GlyphStyle(
            icon = Icons.Filled.Star,
            tint = CloserPalette.PurpleDeep,
            container = CloserPalette.PurpleMist
        )
    }
}
