package com.aiosman.ravenow.ui.chat

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
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.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.SheetState
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.ravenow.LocalAppTheme
import com.aiosman.ravenow.LocalNavController
import com.aiosman.ravenow.R
import com.aiosman.ravenow.ui.composables.StatusBarSpacer
import com.aiosman.ravenow.ui.comment.NoticeScreenHeader
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.IconButton
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.text.style.TextAlign
import com.aiosman.ravenow.ui.composables.CustomAsyncImage

@Composable
fun ChatSettingScreen() {
    val appColors = LocalAppTheme.current
    val navController = LocalNavController.current
    var showThemeSheet by remember { mutableStateOf(false) }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(appColors.secondaryBackground)
    ) {
        StatusBarSpacer()
        Box(modifier = Modifier.padding(horizontal = 24.dp, vertical = 16.dp)) {
            NoticeScreenHeader(title = stringResource(R.string.chat_settings), moreIcon = false)
        }

        Column(modifier = Modifier.padding(horizontal = 16.dp)) {
            SettingCard(
                title = stringResource(R.string.chat_theme_settings),
                onClick = { showThemeSheet = true }
            )
            Spacer(modifier = Modifier.height(12.dp))
            SettingCard(
                title = stringResource(R.string.report),
                onClick = { /* TODO: 跳转举报 */ }
            )
        }
    }

    if (showThemeSheet) {
        ThemePickerSheet(onClose = { showThemeSheet = false })
    }
}

@Composable
private fun SettingCard(title: String, onClick: () -> Unit) {
    val appColors = LocalAppTheme.current
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .clip(RoundedCornerShape(12.dp))
            .background(appColors.background)
            .clickable { onClick() }
            .padding(horizontal = 12.dp, vertical = 14.dp)
    ) {
        Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {
            Text(
                text = title,
                color = appColors.text,
                fontSize = 16.sp,
                fontWeight = FontWeight.W500,
                modifier = Modifier.weight(1f)
            )
            Icon(
                painter = painterResource(id = R.drawable.rave_now_nav_right),
                contentDescription = null,
                tint = appColors.text,
                modifier = Modifier.size(20.dp)
            )
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun ThemePickerSheet(onClose: () -> Unit) {
    val appColors = LocalAppTheme.current
    val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
    val configuration = LocalConfiguration.current
    val screenHeight = configuration.screenHeightDp.dp
    val sheetHeight = screenHeight * 0.9f
    var previewUrl by remember { mutableStateOf<String?>(null) }
    ModalBottomSheet(
        onDismissRequest = onClose,
        sheetState = sheetState,
        containerColor = appColors.secondaryBackground,
        dragHandle = null,
        modifier = Modifier
            .fillMaxWidth()
            .height(sheetHeight),
    ) {
        Column(modifier = Modifier.padding(horizontal = 16.dp)) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 4.dp, bottom = 8.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Spacer(modifier = Modifier.size(24.dp))
                Text(
                    text = stringResource(R.string.custom_background),
                    color = appColors.text,
                    fontSize = 16.sp,
                    fontWeight = FontWeight.W600,
                    modifier = Modifier.weight(1f).padding(start = 90.dp),
                )
                IconButton(onClick = onClose) {
                    Icon(
                        painter = painterResource(id = R.drawable.rider_pro_close),
                        contentDescription = "close",
                        tint = appColors.text
                    )
                }
            }

            // 从相册选择
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .clip(RoundedCornerShape(12.dp))
                    .background(appColors.background)
                    .clickable { /* TODO: 打开相册选择 */ }
                    .padding(horizontal = 16.dp, vertical = 14.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(text = stringResource(R.string.select_from_gallery), color = appColors.text, fontSize = 15.sp, modifier = Modifier.weight(1f))
                Icon(
                    painter = painterResource(id = R.drawable.group_info_edit),
                    contentDescription = null,
                    tint = appColors.text,
                    modifier = Modifier.size(20.dp)
                )
            }

            Spacer(modifier = Modifier.height(12.dp))
            Text(text = stringResource(R.string.featured_backgrounds), color = appColors.text, fontSize = 12.sp)
            Spacer(modifier = Modifier.height(8.dp))

            val presets = remember {
                listOf(
                    "https://picsum.photos/seed/ai1/400/600",
                    "https://picsum.photos/seed/ai2/400/600",
                    "https://picsum.photos/seed/ai3/400/600",
                    "https://picsum.photos/seed/ai4/400/600",
                    "https://picsum.photos/seed/ai5/400/600",
                    "https://picsum.photos/seed/ai6/400/600",
                    "https://picsum.photos/seed/ai7/400/600",
                    "https://picsum.photos/seed/ai8/400/600",
                    "https://picsum.photos/seed/ai9/400/600",
                    "https://picsum.photos/seed/ai10/400/600",
                    "https://picsum.photos/seed/ai11/400/600",
                    "https://picsum.photos/seed/ai12/400/600",
                    "https://picsum.photos/seed/ai13/400/600",
                    "https://picsum.photos/seed/ai14/400/600",
                    "https://picsum.photos/seed/ai15/400/600",
                )
            }

            LazyVerticalGrid(
                columns = GridCells.Fixed(3),
                horizontalArrangement = Arrangement.spacedBy(12.dp),
                verticalArrangement = Arrangement.spacedBy(12.dp),
                modifier = Modifier
                    .fillMaxWidth()
            ) {
                items(presets) { url ->
                    Column(
                        modifier = Modifier
                            .fillMaxWidth()
                            .clickable { previewUrl = url }
                    ) {
                        Box(
                            modifier = Modifier
                                .clip(RoundedCornerShape(12.dp))
                                .background(appColors.decentBackground)
                        ) {
                            CustomAsyncImage(
                                imageUrl = url,
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .aspectRatio(3f / 4f),
                                contentDescription = "preset",
                                contentScale = ContentScale.Crop
                            )
                        }
                        Spacer(modifier = Modifier.height(6.dp))
                        Text(
                            text = "Heart Drive",
                            color = appColors.text,
                            fontSize = 12.sp
                        )
                    }
                }
            }

            // 预览自定义背景弹窗
            if (previewUrl != null) {
                ModalBottomSheet(
                    onDismissRequest = { previewUrl = null },
                    sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true),
                    containerColor = appColors.secondaryBackground,
                    dragHandle = null,
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(sheetHeight)
                ) {
                    Box(modifier = Modifier.fillMaxSize()) {
                        CustomAsyncImage(
                            imageUrl = previewUrl!!,
                            modifier = Modifier.fillMaxSize(),
                            contentDescription = "preview_bg",
                            contentScale = ContentScale.Crop
                        )

                        Column(modifier = Modifier
                            .fillMaxSize()
                            .padding(horizontal = 16.dp, vertical = 8.dp)) {
                            Box(
                                modifier = Modifier
                                    .align(Alignment.CenterHorizontally)
                                    .padding(horizontal = 12.dp, vertical = 16.dp)
                            ) {
                                Text(text = stringResource(R.string.previewing_custom_background), color = Color.White, fontSize = 15.sp)
                            }


                            Column(modifier = Modifier.padding(8.dp)) {
                                Row {
                                    Box(
                                        modifier = Modifier
                                            .clip(RoundedCornerShape(24.dp))
                                            .background(Color.White)
                                            .padding(horizontal = 14.dp, vertical = 8.dp)
                                    ) {
                                        Text(text = stringResource(R.string.each_theme_unique_experience), color = Color.Black, fontSize = 12.sp)
                                    }
                                }
                                Spacer(modifier = Modifier.height(12.dp))
                                Row {
                                    Box(
                                        modifier = Modifier
                                            .clip(RoundedCornerShape(24.dp))
                                            .background(Color.White)
                                            .padding(horizontal = 14.dp, vertical = 8.dp)
                                    ) {
                                        Text(text = stringResource(R.string.select_apply_to_use_theme), color = Color.Black, fontSize = 12.sp)
                                    }
                                }
                                Spacer(modifier = Modifier.height(12.dp))
                                Row {
                                    Spacer(modifier = Modifier.weight(1f))
                                    Box(
                                        modifier = Modifier
                                            .clip(RoundedCornerShape(20.dp))
                                            .background(Color(0xFF7C4DFF))
                                            .padding(horizontal = 16.dp, vertical = 8.dp)
                                    ) {
                                        Text(text = stringResource(R.string.tap_cancel_to_preview_other_themes), color = Color.White, fontSize = 12.sp)
                                    }
                                }
                            }

                            Spacer(modifier = Modifier.weight(1f))

                            // 底部按钮
                            Row(
                                modifier = Modifier.fillMaxWidth()
                                    .padding(bottom = 60.dp),
                                verticalAlignment = Alignment.CenterVertically
                            ) {
                                Box(
                                    modifier = Modifier
                                        .weight(1f)
                                        .clip(RoundedCornerShape(12.dp))
                                        .background(Color.White)
                                        .clickable { previewUrl = null }
                                        .padding(vertical = 12.dp),
                                    contentAlignment = Alignment.Center
                                ) {
                                    Text(text = stringResource(R.string.cancel), color = Color.Black, fontSize = 14.sp)
                                }
                                Spacer(modifier = Modifier.size(12.dp))
                                Box(
                                    modifier = Modifier
                                        .weight(1f)
                                        .clip(RoundedCornerShape(12.dp))
                                        .background(
                                            brush = Brush.horizontalGradient(
                                                colors = listOf(
                                                    Color(0xFFEE2A33),
                                                    Color(0xFFD80264),
                                                    Color(0xFF664C92)
                                                )
                                            )
                                        )
                                        .clickable {
                                            previewUrl?.let { url ->
                                                com.aiosman.ravenow.AppStore.saveChatBackgroundUrl(url)
                                                previewUrl = null
                                                onClose()
                                            }
                                        }
                                        .padding(vertical = 12.dp),
                                    contentAlignment = Alignment.Center
                                ) {
                                    Text(text = stringResource(R.string.moment_ai_apply), color = Color.White, fontSize = 14.sp)
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}


