package com.seven.ikar.kotlin.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import com.seven.ikar.kotlin.core.settings.DEFAULT_CHAT_WALLPAPER_PRESET_ID
import com.seven.ikar.kotlin.core.settings.ChatSettingsSnapshot

enum class ChatWallpaperAccentStyle {
    SoftCircles,
    CornerGlow,
    Ribbon
}

data class ChatWallpaperPreset(
    val id: String,
    val title: String,
    val gradientColors: List<Color>,
    val accentPrimary: Color,
    val accentSecondary: Color,
    val accentStyle: ChatWallpaperAccentStyle
)

object ChatWallpaperCatalog {
    val presets: List<ChatWallpaperPreset> = listOf(
        ChatWallpaperPreset(
            id = DEFAULT_CHAT_WALLPAPER_PRESET_ID,
            title = "Мятное поле",
            gradientColors = listOf(Color(0xFFE9F4D7), Color(0xFFD5E8BB), Color(0xFFC8E0AD)),
            accentPrimary = Color(0x88FFFFFF),
            accentSecondary = Color(0x445C8F5A),
            accentStyle = ChatWallpaperAccentStyle.SoftCircles
        ),
        ChatWallpaperPreset(
            id = "sky_haze",
            title = "Небесная дымка",
            gradientColors = listOf(Color(0xFFE8F5FF), Color(0xFFD6ECFF), Color(0xFFC1DFFF)),
            accentPrimary = Color(0x88FFFFFF),
            accentSecondary = Color(0x44489AD9),
            accentStyle = ChatWallpaperAccentStyle.CornerGlow
        ),
        ChatWallpaperPreset(
            id = "peach_dawn",
            title = "Персиковый рассвет",
            gradientColors = listOf(Color(0xFFFFF0E2), Color(0xFFFFDDBD), Color(0xFFFFCCA3)),
            accentPrimary = Color(0x88FFFFFF),
            accentSecondary = Color(0x44F08F5A),
            accentStyle = ChatWallpaperAccentStyle.Ribbon
        ),
        ChatWallpaperPreset(
            id = "violet_dusk",
            title = "Сиреневый закат",
            gradientColors = listOf(Color(0xFFF0E8FF), Color(0xFFE0D1FF), Color(0xFFD1BCFF)),
            accentPrimary = Color(0x88FFFFFF),
            accentSecondary = Color(0x447D5AD8),
            accentStyle = ChatWallpaperAccentStyle.SoftCircles
        ),
        ChatWallpaperPreset(
            id = "ocean_breeze",
            title = "Океан",
            gradientColors = listOf(Color(0xFFE3FAFF), Color(0xFFC7F0F7), Color(0xFFA5E1EC)),
            accentPrimary = Color(0x88FFFFFF),
            accentSecondary = Color(0x4446A8B8),
            accentStyle = ChatWallpaperAccentStyle.CornerGlow
        ),
        ChatWallpaperPreset(
            id = "lemon_mist",
            title = "Лимонный туман",
            gradientColors = listOf(Color(0xFFFFF7D7), Color(0xFFFFEFAE), Color(0xFFFBE18D)),
            accentPrimary = Color(0x88FFFFFF),
            accentSecondary = Color(0x44C9A94C),
            accentStyle = ChatWallpaperAccentStyle.Ribbon
        ),
        ChatWallpaperPreset(
            id = "rose_cloud",
            title = "Розовое облако",
            gradientColors = listOf(Color(0xFFFFE7EE), Color(0xFFFFD7E7), Color(0xFFF7C4DB)),
            accentPrimary = Color(0x88FFFFFF),
            accentSecondary = Color(0x44D36C95),
            accentStyle = ChatWallpaperAccentStyle.SoftCircles
        ),
        ChatWallpaperPreset(
            id = "graphite_night",
            title = "Графитовая ночь",
            gradientColors = listOf(Color(0xFF2A3342), Color(0xFF1D2734), Color(0xFF131B27)),
            accentPrimary = Color(0x2267A3F3),
            accentSecondary = Color(0x2241E0C2),
            accentStyle = ChatWallpaperAccentStyle.Ribbon
        ),
        ChatWallpaperPreset(
            id = "forest_glow",
            title = "Лесное свечение",
            gradientColors = listOf(Color(0xFFE4F4E5), Color(0xFFCFE6C6), Color(0xFFBCD7B2)),
            accentPrimary = Color(0x88FFFFFF),
            accentSecondary = Color(0x444B8658),
            accentStyle = ChatWallpaperAccentStyle.CornerGlow
        ),
        ChatWallpaperPreset(
            id = "coral_sand",
            title = "Коралловый песок",
            gradientColors = listOf(Color(0xFFFFEEE6), Color(0xFFFFD8C8), Color(0xFFF6BFA8)),
            accentPrimary = Color(0x88FFFFFF),
            accentSecondary = Color(0x44CF7861),
            accentStyle = ChatWallpaperAccentStyle.Ribbon
        ),
        ChatWallpaperPreset(
            id = "azure_glass",
            title = "Лазурное стекло",
            gradientColors = listOf(Color(0xFFF0FCFF), Color(0xFFD7F4FF), Color(0xFFBAE7FA)),
            accentPrimary = Color(0x88FFFFFF),
            accentSecondary = Color(0x444C9BC4),
            accentStyle = ChatWallpaperAccentStyle.SoftCircles
        ),
        ChatWallpaperPreset(
            id = "lilac_ice",
            title = "Лиловый лёд",
            gradientColors = listOf(Color(0xFFF4EEFF), Color(0xFFE7DDFF), Color(0xFFD5C7FF)),
            accentPrimary = Color(0x88FFFFFF),
            accentSecondary = Color(0x447C73CF),
            accentStyle = ChatWallpaperAccentStyle.CornerGlow
        ),
        ChatWallpaperPreset(
            id = "amber_paper",
            title = "Янтарная бумага",
            gradientColors = listOf(Color(0xFFFFF2DC), Color(0xFFF6DFB7), Color(0xFFE9C88D)),
            accentPrimary = Color(0x88FFFFFF),
            accentSecondary = Color(0x44A06A2B),
            accentStyle = ChatWallpaperAccentStyle.Ribbon
        ),
        ChatWallpaperPreset(
            id = "olive_smoke",
            title = "Оливковый дым",
            gradientColors = listOf(Color(0xFFF0F4E6), Color(0xFFDEE4C6), Color(0xFFCDD59D)),
            accentPrimary = Color(0x88FFFFFF),
            accentSecondary = Color(0x446B8A4B),
            accentStyle = ChatWallpaperAccentStyle.SoftCircles
        ),
        ChatWallpaperPreset(
            id = "ruby_night",
            title = "Рубиновая ночь",
            gradientColors = listOf(Color(0xFF422437), Color(0xFF2F1A28), Color(0xFF1C1119)),
            accentPrimary = Color(0x22FF8AAE),
            accentSecondary = Color(0x22FFD4A3),
            accentStyle = ChatWallpaperAccentStyle.CornerGlow
        )
    )

    fun resolvePreset(id: String?): ChatWallpaperPreset =
        presets.firstOrNull { it.id == id } ?: presets.first { it.id == DEFAULT_CHAT_WALLPAPER_PRESET_ID }
}

@Composable
fun ChatWallpaperBackground(
    snapshot: ChatSettingsSnapshot,
    modifier: Modifier = Modifier
) {
    Box(modifier = modifier) {
        if (snapshot.useCustomWallpaper && !snapshot.customWallpaperUri.isNullOrBlank()) {
            AuthenticatedAsyncImage(
                imageUrl = snapshot.customWallpaperUri,
                contentDescription = "Пользовательские обои чата",
                modifier = Modifier.fillMaxSize(),
                contentScale = ContentScale.Crop
            )
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .background(
                        Brush.verticalGradient(
                            colors = listOf(Color.White.copy(alpha = 0.08f), Color.Black.copy(alpha = 0.05f))
                        )
                    )
            )
        } else {
            PresetWallpaperBackground(
                preset = ChatWallpaperCatalog.resolvePreset(snapshot.selectedWallpaperPresetId),
                modifier = Modifier.fillMaxSize()
            )
        }
    }
}

@Composable
fun PresetWallpaperBackground(
    preset: ChatWallpaperPreset,
    modifier: Modifier = Modifier
) {
    Box(
        modifier = modifier.background(
            Brush.linearGradient(colors = preset.gradientColors)
        )
    ) {
        when (preset.accentStyle) {
            ChatWallpaperAccentStyle.SoftCircles -> {
                AccentCircle(
                    modifier = Modifier
                        .align(Alignment.TopEnd)
                        .offset(x = 36.dp, y = (-18).dp)
                        .size(132.dp),
                    color = preset.accentPrimary
                )
                AccentCircle(
                    modifier = Modifier
                        .align(Alignment.BottomStart)
                        .offset(x = (-28).dp, y = 24.dp)
                        .size(156.dp),
                    color = preset.accentSecondary
                )
            }

            ChatWallpaperAccentStyle.CornerGlow -> {
                AccentCircle(
                    modifier = Modifier
                        .align(Alignment.TopStart)
                        .offset(x = (-40).dp, y = (-24).dp)
                        .size(150.dp),
                    color = preset.accentPrimary
                )
                AccentCircle(
                    modifier = Modifier
                        .align(Alignment.CenterEnd)
                        .offset(x = 28.dp)
                        .size(124.dp),
                    color = preset.accentSecondary
                )
                AccentCircle(
                    modifier = Modifier
                        .align(Alignment.BottomCenter)
                        .offset(y = 34.dp)
                        .size(108.dp),
                    color = preset.accentPrimary.copy(alpha = 0.45f)
                )
            }

            ChatWallpaperAccentStyle.Ribbon -> {
                AccentBand(
                    modifier = Modifier
                        .align(Alignment.TopStart)
                        .offset(x = (-18).dp, y = 22.dp)
                        .fillMaxWidth(0.9f)
                        .height(64.dp),
                    color = preset.accentPrimary
                )
                AccentBand(
                    modifier = Modifier
                        .align(Alignment.BottomEnd)
                        .offset(x = 22.dp, y = (-14).dp)
                        .fillMaxWidth(0.76f)
                        .height(56.dp),
                    color = preset.accentSecondary
                )
            }
        }
    }
}

@Composable
private fun AccentCircle(
    modifier: Modifier,
    color: Color
) {
    Box(
        modifier = modifier.background(color = color, shape = CircleShape)
    )
}

@Composable
private fun AccentBand(
    modifier: Modifier,
    color: Color
) {
    Box(
        modifier = modifier.background(
            color = color,
            shape = RoundedCornerShape(28.dp)
        )
    )
}
