package com.aiosman.ravenow.ui.group

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.CircularProgressIndicator
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.graphics.ColorFilter
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.ravenow.LocalAppTheme
import com.aiosman.ravenow.R
import com.aiosman.ravenow.ui.modifiers.noRippleClickable
import androidx.compose.foundation.Image
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.platform.LocalContext
import android.widget.Toast
import androidx.compose.ui.graphics.Brush
import com.aiosman.ravenow.ui.composables.CustomAsyncImage
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource

@Composable
fun GroupMemoryManageContent(
    groupId: String,
    viewModel: GroupChatInfoViewModel,
    onAddMemoryClick: () -> Unit = {},
    onDismiss: () -> Unit = {}
) {
    val AppColors = LocalAppTheme.current
    val context = LocalContext.current
    
    // 编辑记忆的状态 - 存储正在编辑的记忆ID
    var editingMemoryId by remember { mutableStateOf<Int?>(null) }
    
    // 加载配额和列表数据
    LaunchedEffect(Unit) {
        viewModel.loadMemoryQuota()
        viewModel.loadMemoryList()
    }
    
    val quota = viewModel.memoryQuota
    val memoryList = viewModel.memoryList
    val isLoading = viewModel.isLoadingMemory

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color(0xFFFAF9FB))
    ) {
        // 顶部栏：返回按钮 + 标题 + 加号按钮
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(44.dp)
                .padding(horizontal = 16.dp)
        ) {
            // 中间标题 - 绝对居中，不受其他组件影响
            Text(
                text = stringResource(R.string.group_chat_info_memory_manage2),
                style = TextStyle(
                    color = Color.Black,
                    fontSize = 17.sp,
                    fontWeight = FontWeight.SemiBold
                ),
                modifier = Modifier.align(Alignment.Center),
                textAlign = TextAlign.Center
            )

            // 左侧返回按钮
            Row(
                modifier = Modifier
                    .align(Alignment.CenterStart)
                    .clip(RoundedCornerShape(296.dp))
                    .background(Color.White) // 浅灰色背景
                    .noRippleClickable { onDismiss() }
                    .padding(horizontal = 12.dp, vertical = 8.dp),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(4.dp)
            ) {
                Image(
                    painter = painterResource(R.drawable.rider_pro_back_icon),
                    contentDescription = "返回",
                    modifier = Modifier.size(16.dp),
                    colorFilter = ColorFilter.tint(Color.Black)
                )
                Text(
                    text = stringResource(R.string.back_upper),
                    style = TextStyle(
                        color = Color.Black,
                        fontSize = 15.sp,
                        fontWeight = FontWeight.Normal
                    )
                )
            }

            // 右侧圆形加号按钮
            Box(
                modifier = Modifier
                    .align(Alignment.CenterEnd)
                    .size(32.dp)
                    .clip(CircleShape)
                    .background(Color.White)
                    .noRippleClickable { onAddMemoryClick() },
                contentAlignment = Alignment.Center
            ) {
                Text(
                    text = "+",
                    style = TextStyle(
                        color = Color.Black,
                        fontSize = 20.sp,
                        fontWeight = FontWeight.Medium
                    )
                )
            }
        }

        // 浅黄色提示栏 - 显示真实的配额数据
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .background(Color(0xFFFBF8EF))
                .padding(horizontal = 16.dp, vertical = 12.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Row(horizontalArrangement = Arrangement.spacedBy(16.dp), verticalAlignment = Alignment.CenterVertically) {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Text(stringResource(R.string.memory_paid), style = TextStyle(color = Color(0x993C3C43), fontSize = 13.sp))
                    Spacer(Modifier.width(3.dp))
                    Text(
                        "${quota?.purchasedCount ?: 0}", 
                        style = TextStyle(color = Color(0xFFFF8D28), fontSize = 13.sp)
                    )
                }
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Text(stringResource(R.string.memory_used), style = TextStyle(color = Color(0x993C3C43), fontSize = 13.sp))
                    Spacer(Modifier.width(3.dp))
                    Text(
                        "${quota?.currentCount ?: 0}", 
                        style = TextStyle(color = Color(0xFFFF8D28), fontSize = 13.sp)
                    )
                }
            }
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(stringResource(R.string.upper_limit), style = TextStyle(color = Color(0x993C3C43), fontSize = 13.sp))
                Spacer(Modifier.width(3.dp))
                Text(
                    "50",
                    style = TextStyle(color = Color(0xFFFF8D28), fontSize = 13.sp)
                )
            }
        }

        // 记忆列表或空状态
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
        ) {
            if (isLoading) {
                // 加载中状态
                Box(
                    modifier = Modifier.fillMaxSize(),
                    contentAlignment = Alignment.Center
                ) {
                    CircularProgressIndicator(
                        modifier = Modifier.size(40.dp),
                        color = Color(0xFFFF8D28)
                    )
                }
            } else if (memoryList.isNotEmpty()) {
                // 显示记忆列表
                LazyColumn(
                    modifier = Modifier.fillMaxSize(),
                    contentPadding = PaddingValues(horizontal = 16.dp, vertical = 12.dp),
                    verticalArrangement = Arrangement.spacedBy(12.dp)
                ) {
                    items(memoryList) { memory ->
                        MemoryItem(
                            memory = memory,
                            isEditing = editingMemoryId == memory.id,
                            onEdit = {
                                editingMemoryId = memory.id
                            },
                            onCancel = {
                                editingMemoryId = null
                            },
                            onSave = { newText ->
                                viewModel.updateMemory(memory.id, newText)
                                editingMemoryId = null
                            },
                            onDelete = {
                                viewModel.deleteMemory(memory.id)
                            }
                        )
                    }
                }
            } else {
                Column(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(vertical = 60.dp),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center
                ) {
                    Image(
                        painter = painterResource(id = R.mipmap.group),
                        contentDescription = "暂无记忆",
                        modifier = Modifier
                            .height(150.dp).width(180.dp)
                    )

                    Spacer(Modifier.height(10.dp))
                    Text(
                        text = stringResource(R.string.no_memory),
                        style = TextStyle(color = Color.Black, fontSize = 16.sp, fontWeight = FontWeight.SemiBold)
                    )
                    Spacer(Modifier.height(6.dp))
                    Text(
                        text = stringResource(R.string.add_memory),
                        style = TextStyle(color = Color.Black, fontSize = 14.sp, fontWeight = FontWeight.Normal)
                    )
                }
            }
        }
    }
}

/**
 * 编辑记忆对话框
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun EditGroupMemoryDialog(
    memory: com.aiosman.ravenow.entity.RoomRuleEntity,
    viewModel: GroupChatInfoViewModel,
    onDismiss: () -> Unit,
    onUpdateMemory: (String) -> Unit
) {
    val AppColors = LocalAppTheme.current
    val context = LocalContext.current
    var memoryText by remember { mutableStateOf(memory.rule) }
    val maxLength = 500
    val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)

    val gradientColors = listOf(
        Color(0xFF7C45ED),
        Color(0xFF7C57EE),
        Color(0xFF7BD8F8)
    )
    val gradientBrush = Brush.horizontalGradient(colors = gradientColors)
    
    ModalBottomSheet(
        onDismissRequest = onDismiss,
        sheetState = sheetState,
        containerColor = Color(0xFFFAF9FB),
        dragHandle = {
            Box(
                modifier = Modifier
                    .width(36.dp)
                    .height(5.dp)
                    .padding(top = 5.dp)
                    .background(
                        Color(0xFFCCCCCC),
                        RoundedCornerShape(100.dp)
                    )
            )
        },
        shape = RoundedCornerShape(topStart = 20.dp, topEnd = 20.dp)
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 12.dp, vertical = 16.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.spacedBy(12.dp)
        ) {
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(88.dp)
            ) {
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .clip(RoundedCornerShape(16.dp))
                        .background(brush = gradientBrush)
                )

                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(1.dp)
                        .clip(RoundedCornerShape(15.dp))
                        .background(Color.White)
                        .padding(12.dp),
                    contentAlignment = Alignment.TopStart
                ) {
                    BasicTextField(
                        value = memoryText,
                        onValueChange = { newText ->
                            if (newText.length <= maxLength) {
                                memoryText = newText
                            }
                        },
                        cursorBrush = SolidColor(Color.Black),
                        modifier = Modifier.fillMaxWidth(),
                        textStyle = TextStyle(
                            fontSize = 13.sp,
                            color = Color.Black,
                            lineHeight = 18.sp
                        ),
                        decorationBox = { innerTextField ->
                            Box(
                                modifier = Modifier.fillMaxWidth(),
                                contentAlignment = Alignment.TopStart
                            ) {
                                innerTextField()
                            }
                        }
                    )
                }
            }

            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.spacedBy(12.dp)
            ) {
                // 取消按钮
                Box(
                    modifier = Modifier
                        .weight(1f)
                        .height(34.dp)
                        .clip(RoundedCornerShape(653.8.dp))
                        .background(Color(0x147C7480))
                        .noRippleClickable { onDismiss() },
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = "取消",
                        style = TextStyle(
                            fontSize = 15.sp,
                            color = Color.Black
                        )
                    )
                }
                
                // 保存按钮
                Box(
                    modifier = Modifier
                        .weight(1f)
                        .height(34.dp)
                        .clip(RoundedCornerShape(653.8.dp))
                        .background(Color(0xFF110C13))
                        .noRippleClickable {
                            if (memoryText.isNotBlank() && memoryText != memory.rule) {
                                onUpdateMemory(memoryText)
                                Toast.makeText(context, "记忆更新成功", Toast.LENGTH_SHORT).show()
                            }
                        },
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = "保存",
                        style = TextStyle(
                            fontSize = 15.sp,
                            color = Color.White
                        )
                    )
                }
            }
        }
    }
}

/**
 * 记忆项组件
 */
@Composable
fun MemoryItem(
    memory: com.aiosman.ravenow.entity.RoomRuleEntity,
    isEditing: Boolean = false,
    onEdit: () -> Unit = {},
    onCancel: () -> Unit = {},
    onSave: (String) -> Unit = {},
    onDelete: () -> Unit
) {
    val AppColors = LocalAppTheme.current
    val context = LocalContext.current
    var memoryText by remember { mutableStateOf(memory.rule) }
    val maxLength = 500
    
    // 渐变边框颜色
    val gradientColors = listOf(
        Color(0xFF7C45ED),
        Color(0xFF7C57EE),
        Color(0xFF7BD8F8)
    )
    val gradientBrush = Brush.horizontalGradient(colors = gradientColors)
    
    // 当进入编辑模式时，重置文本
    LaunchedEffect(isEditing) {
        if (isEditing) {
            memoryText = memory.rule
        }
    }
    
    if (isEditing) {
        // 编辑模式：显示编辑界面
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .clip(RoundedCornerShape(16.dp))
                .background(Color.White)
                .padding(horizontal = 12.dp, vertical = 16.dp),
            verticalArrangement = Arrangement.spacedBy(12.dp)
        ) {
            // 文本输入框 - 带渐变边框
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(88.dp)
            ) {
                // 渐变边框层
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .clip(RoundedCornerShape(16.dp))
                        .background(brush = gradientBrush)
                )
                
                // 内容层 - 白色背景，通过padding形成边框效果
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(1.dp)
                        .clip(RoundedCornerShape(15.dp))
                        .background(Color.White)
                        .padding(12.dp),
                    contentAlignment = Alignment.TopStart
                ) {
                    BasicTextField(
                        value = memoryText,
                        onValueChange = { newText ->
                            if (newText.length <= maxLength) {
                                memoryText = newText
                            }
                        },
                        cursorBrush = SolidColor(Color.Black),
                        modifier = Modifier.fillMaxWidth(),
                        textStyle = TextStyle(
                            fontSize = 13.sp,
                            color = Color.Black,
                            lineHeight = 18.sp
                        ),
                        decorationBox = { innerTextField ->
                            Box(
                                modifier = Modifier.fillMaxWidth(),
                                contentAlignment = Alignment.TopStart
                            ) {
                                innerTextField()
                            }
                        }
                    )
                }
            }
            
            // 按钮行：取消和保存
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.spacedBy(12.dp)
            ) {
                // 取消按钮
                Box(
                    modifier = Modifier
                        .weight(1f)
                        .height(34.dp)
                        .clip(RoundedCornerShape(653.8.dp))
                        .background(Color(0x147C7480))
                        .noRippleClickable { onCancel() },
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = "取消",
                        style = TextStyle(
                            fontSize = 15.sp,
                            color = Color.Black
                        )
                    )
                }
                
                // 保存按钮
                Box(
                    modifier = Modifier
                        .weight(1f)
                        .height(34.dp)
                        .clip(RoundedCornerShape(653.8.dp))
                        .background(Color(0xFF110C13))
                        .noRippleClickable {
                            if (memoryText.isNotBlank() && memoryText != memory.rule) {
                                onSave(memoryText)
                                Toast.makeText(context, "记忆更新成功", Toast.LENGTH_SHORT).show()
                            }
                        },
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = "保存",
                        style = TextStyle(
                            fontSize = 15.sp,
                            color = Color.White
                        )
                    )
                }
            }
        }
    } else {
        // 显示模式：显示记忆内容
        // 格式化日期：从 "2025-10-20T10:30:00Z" 格式转换为 "2025年10月20日"
        val formattedDate = try {
            if (memory.createdAt.length >= 10) {
                val dateStr = memory.createdAt.substring(0, 10)
                val parts = dateStr.split("-")
                if (parts.size == 3) {
                    "${parts[0]}年${parts[1].toInt()}月${parts[2].toInt()}日"
                } else {
                    dateStr
                }
            } else {
                memory.createdAt
            }
        } catch (e: Exception) {
            if (memory.createdAt.length >= 10) memory.createdAt.substring(0, 10) else memory.createdAt
        }
        
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .clip(RoundedCornerShape(16.dp))
                .background(Color.White)
                .padding(horizontal = 12.dp, vertical = 16.dp)
        ) {
            // 主文本 - 顶部
            Text(
                text = memory.rule,
                style = TextStyle(
                    color = Color.Black,
                    fontSize = 13.sp,
                    lineHeight = 18.sp
                ),
                maxLines = 3,
                overflow = TextOverflow.Ellipsis
            )
            
            Spacer(modifier = Modifier.height(16.dp))
            
            // 底部行：创建者信息 + 编辑删除按钮
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically
            ) {
                // 创建者信息 - 左侧：头像 + 用户名 · 时间
                Row(
                    horizontalArrangement = Arrangement.spacedBy(8.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    // 圆形头像
                    val avatarUrl = memory.creator?.avatarDirectUrl?.takeIf { it.isNotBlank() }
                    CustomAsyncImage(
                        imageUrl = avatarUrl,
                        contentDescription = "创建者头像",
                        modifier = Modifier
                            .size(20.dp)
                            .clip(CircleShape),
                        defaultRes = R.drawable.default_avatar,
                        placeholderRes = R.drawable.default_avatar,
                        errorRes = R.drawable.default_avatar,
                        contentScale = ContentScale.Crop
                    )
                    
                    // 用户名 · 时间
                    Text(
                        text = buildString {
                            append(memory.creator?.nickname ?: "未知用户")
                            append(" · ")
                            append(formattedDate)
                        },
                        style = TextStyle(
                            color = Color(0x993C3C43),
                            fontSize = 11.sp
                        )
                    )
                }
                
                // 编辑和删除图标 - 右侧
                Row(
                    horizontalArrangement = Arrangement.spacedBy(16.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    // 编辑图标
                    Image(
                        painter = painterResource(R.mipmap.icons_infor_edit),
                        contentDescription = "编辑",
                        modifier = Modifier
                            .size(20.dp)
                            .noRippleClickable { onEdit() },
                        colorFilter = ColorFilter.tint(Color.Black)
                    )

                    Image(
                        painter = painterResource(R.mipmap.iconsdelete),
                        contentDescription = "删除",
                        modifier = Modifier
                            .size(20.dp)
                            .noRippleClickable { onDelete() },
                        colorFilter = ColorFilter.tint(Color(0xFFEE2A33))
                    )
                }
            }
        }
    }
}

