package com.aiosman.ravenow.ui.group

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.aiosman.ravenow.LocalAppTheme
import com.aiosman.ravenow.LocalNavController
import com.aiosman.ravenow.R
import com.aiosman.ravenow.data.PointService
import com.aiosman.ravenow.ui.composables.CustomAsyncImage
import com.aiosman.ravenow.ui.composables.StatusBarSpacer
import com.aiosman.ravenow.ui.composables.TabItem
import com.aiosman.ravenow.ui.composables.TabSpacer
import com.aiosman.ravenow.ui.modifiers.noRippleClickable
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import kotlinx.coroutines.launch

// 成员数据类
data class GroupMember(
    val id: String,
    val name: String,
    val avatar: String,
    val isAi:Boolean = false
)

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun CreateGroupChatScreen() {
    val AppColors = LocalAppTheme.current
    val navController = LocalNavController.current
    val systemUiController = rememberSystemUiController()
    val context = LocalContext.current

    // 状态管理
    var groupName by remember { mutableStateOf(TextFieldValue("")) }
    var searchText by remember { mutableStateOf(TextFieldValue("")) }
    var selectedMembers by remember { mutableStateOf(listOf<GroupMember>()) }
    var selectedMemberIds by remember { mutableStateOf<Set<String>>(emptySet()) }
    var pagerState = rememberPagerState(pageCount = { 2 })
    var scope = rememberCoroutineScope()

    // LazyRow状态管理
    val lazyRowState = rememberLazyListState()

    // 清除错误信息
    LaunchedEffect(groupName.text, searchText.text) {
        if (CreateGroupChatViewModel.errorMessage != null) {
            CreateGroupChatViewModel.clearError()
        }
    }

    // 监听页面切换，清除当前tab的选中状态（但保留已选成员列表）
    LaunchedEffect(pagerState.currentPage) {
        // 不清除selectedMemberIds，因为我们需要保持跨tab的选中状态
        // 这样用户可以在AI智能体和朋友之间切换，选中的状态会保持
    }

    // 监听selectedMembers变化，当有新成员添加时自动滚动到最后一个
    LaunchedEffect(selectedMembers.size) {
        if (selectedMembers.isNotEmpty()) {
            // 延迟一点时间确保LazyRow已经更新了布局
            kotlinx.coroutines.delay(100)
            lazyRowState.animateScrollToItem(selectedMembers.size - 1)
        }
    }

    val navigationBarPadding = WindowInsets.navigationBars.asPaddingValues().calculateBottomPadding()

    // 获取费用和余额信息
    val pointsRules by PointService.pointsRules.collectAsState(initial = null)
    val pointsBalance by PointService.pointsBalance.collectAsState(initial = null)
    val roomMaxMembers by PointService.roomMaxMembers.collectAsState(initial = null)
    
    val cost = CreateGroupChatViewModel.getCreateRoomCost()
    val currentBalance = CreateGroupChatViewModel.getCurrentBalance()
    val balanceAfterCost = CreateGroupChatViewModel.calculateBalanceAfterCost(cost)
    val isBalanceSufficient = CreateGroupChatViewModel.isBalanceSufficient(cost)
    
    // 获取群聊初始上限
    val maxMemberLimit = roomMaxMembers?.defaultMaxTotal ?: 5

    LaunchedEffect(Unit) {
        systemUiController.setNavigationBarColor(Color.Transparent)
    }

    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(AppColors.background)
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
        ) {
            StatusBarSpacer()

            // 头部
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 16.dp, vertical = 12.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                // 返回按钮
                Image(
                    painter = painterResource(id = R.drawable.rider_pro_back_icon),
                    contentDescription = "back",
                    modifier = Modifier
                        .size(24.dp)
                        .noRippleClickable {
                            navController.popBackStack()
                        },
                    colorFilter = ColorFilter.tint(AppColors.text)
                )

                // 标题
                Text(
                    text = stringResource(R.string.create_group_chat),
                    fontSize = 17.sp,
                    fontWeight = FontWeight.W700,
                    color = AppColors.text,
                    modifier = Modifier
                        .weight(1f)
                        .padding(start = 16.dp)
                )

                // 一键创建按钮
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier.noRippleClickable {
                        // 一键创建逻辑
                    }
                ) {
                    Image(
                        painter = painterResource(id = R.drawable.rider_pro_new_post_add_pic),
                        contentDescription = "quick create",
                        modifier = Modifier.size(16.dp),
                        colorFilter = ColorFilter.tint(AppColors.main)
                    )
                    Spacer(modifier = Modifier.width(4.dp))
                    Text(
                        text = stringResource(R.string.quick_create),
                        fontSize = 14.sp,
                        color = AppColors.main
                    )
                }
            }

            // 搜索栏
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 16.dp, vertical = 8.dp)
                    .background(
                        color = AppColors.inputBackground,
                        shape = RoundedCornerShape(8.dp)
                    )
                    .padding(horizontal = 16.dp, vertical = 13.dp)
            ) {
                Row(
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Image(
                        painter = painterResource(id = R.drawable.rider_pro_nav_search),
                        contentDescription = stringResource(R.string.search),
                        modifier = Modifier.size(16.dp),
                        colorFilter = ColorFilter.tint(AppColors.secondaryText)
                    )
                    Spacer(modifier = Modifier.width(8.dp))
                    BasicTextField(
                        value = searchText,
                        onValueChange = { searchText = it },
                        textStyle = androidx.compose.ui.text.TextStyle(
                            color = AppColors.text,
                            fontSize = 14.sp
                        ),
                        modifier = Modifier.weight(1f),
                        singleLine = true,
                        cursorBrush = SolidColor(AppColors.text),
                        decorationBox = { innerTextField ->
                            Box {
                                if (searchText.text.isEmpty()) {
                                    Text(
                                        text = stringResource(R.string.search),
                                        color = AppColors.secondaryText,
                                        fontSize = 14.sp
                                    )
                                }
                                innerTextField()
                            }
                        }
                    )
                }
            }

            //        // 搜索栏
            //        Box(
            //            modifier = Modifier
            //                .fillMaxWidth()
            //                .padding(horizontal = 16.dp, vertical = 8.dp)
            //                .background(
            //                    color = AppColors.inputBackground,
            //                    shape = RoundedCornerShape(8.dp)
            //                )
            //                .padding(horizontal = 12.dp, vertical = 8.dp)
            //        ) {

            //                    Row(
            //                        verticalAlignment = Alignment.CenterVertically
            //                    ) {
            //                        Image(
            //                            painter = painterResource(id = R.drawable.rider_pro_nav_search),
            //                            contentDescription = stringResource(R.string.search),
            //                            modifier = Modifier.size(16.dp),
            //                            colorFilter = ColorFilter.tint(AppColors.secondaryText)
            //                        )
            //                        Spacer(modifier = Modifier.width(8.dp))
            //                        if (searchText.text.isEmpty()) {
            //                            Text(
            //                                text = stringResource(R.string.search),
            //                                color = AppColors.secondaryText,
            //                                fontSize = 14.sp
            //                            )
            //                        }
            //                        innerTextField()
            //                    }
            //                }
            //            )
            //        }

            // 群聊名称输入：同一圆角灰色矩形容器
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 16.dp, vertical = 8.dp)
                    .background(
                        color = AppColors.inputBackground,
                        shape = RoundedCornerShape(8.dp)
                    )
                    .padding(horizontal = 12.dp, vertical = 8.dp)
            ) {
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = stringResource(R.string.group_name),
                        fontSize = 14.sp,
                        color = AppColors.text,
                        modifier = Modifier.width(80.dp)
                    )
                    BasicTextField(
                        value = groupName,
                        onValueChange = { groupName = it },
                        textStyle = androidx.compose.ui.text.TextStyle(
                            color = AppColors.text,
                            fontSize = 14.sp
                        ),
                        modifier = Modifier
                            .weight(1f),
                        singleLine = true,
                        cursorBrush = SolidColor(AppColors.text),
                        decorationBox = { innerTextField ->
                            Box(Modifier.fillMaxWidth()) {
                                if (groupName.text.isEmpty()) {
                                    Text(
                                        text = stringResource(R.string.group_name_hint),
                                        color = AppColors.inputHint,
                                        fontSize = 14.sp
                                    )
                                }
                                innerTextField()
                            }
                        }
                    )
                }
            }

            // 已选成员列表
            if (selectedMembers.isNotEmpty()) {
                // 显示选中成员数量
                /*   Text(
                       text = "已选择 ${selectedMembers.size} 个成员",
                       fontSize = 14.sp,
                       color = AppColors.secondaryText,
                       modifier = Modifier.padding(horizontal = 16.dp, vertical = 4.dp)
                   )*/
                LazyRow(
                    state = lazyRowState,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 16.dp, vertical = 8.dp),
                    horizontalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    items(selectedMembers) { member ->
                        Column(
                            horizontalAlignment = Alignment.CenterHorizontally,
                            modifier = Modifier.width(48.dp)
                        ) {
                            Box {
                                CustomAsyncImage(
                                    context = context,
                                    imageUrl = member.avatar,
                                    contentDescription = member.name,
                                    defaultRes = R.drawable.default_avatar,
                                    placeholderRes = R.drawable.default_avatar,
                                    errorRes = R.drawable.default_avatar,
                                    modifier = Modifier
                                        .size(48.dp)
                                        .clip(CircleShape)
                                )

                                // 删除按钮
                                Box(
                                    modifier = Modifier
                                        .size(20.dp)
                                        .background(AppColors.error, CircleShape)
                                        .align(Alignment.TopEnd)
                                        .noRippleClickable {
                                            // 删除成员时同时更新选中状态
                                            val (newSelectedMemberIds, newSelectedMembers) = CreateGroupChatViewModel.removeSelectedMember(
                                                member, selectedMemberIds, selectedMembers
                                            )
                                            selectedMemberIds = newSelectedMemberIds
                                            selectedMembers = newSelectedMembers
                                        },
                                    contentAlignment = Alignment.Center
                                ) {
                                    Text(
                                        text = "×",
                                        color = AppColors.mainText,
                                        fontSize = 14.sp,
                                        fontWeight = FontWeight.Bold
                                    )
                                }
                            }

                            // 名称显示
                            Spacer(modifier = Modifier.height(4.dp))
                            Text(
                                text = if (member.name.length > 5) {
                                    member.name.substring(0, 5) + "..."
                                } else {
                                    member.name
                                },
                                fontSize = 12.sp,

                                color = AppColors.text,
                                maxLines = 1,
                                modifier = Modifier.fillMaxWidth()
                                    .wrapContentWidth(Alignment.CenterHorizontally)
                            )
                        }
                    }
                }
            }

            // Tab切换和成员数量显示
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight()
                    .padding(horizontal = 16.dp, vertical = 8.dp),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically
            ) {
                // Tab左对齐
                Row(
                    horizontalArrangement = Arrangement.Start,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    TabItem(
                        text = stringResource(R.string.chat_ai),
                        isSelected = pagerState.currentPage == 0,
                        onClick = {
                            scope.launch {
                                pagerState.animateScrollToPage(0)
                            }
                        }
                    )
                    TabSpacer()
                    TabItem(
                        text = stringResource(R.string.chat_friend),
                        isSelected = pagerState.currentPage == 1,
                        onClick = {
                            scope.launch {
                                pagerState.animateScrollToPage(1)
                            }
                        }
                    )
                }
                
                // 成员数量显示右对齐（x/x格式）
                Text(
                    text = "${selectedMembers.size}/$maxMemberLimit",
                    fontSize = 14.sp,
                    color = if (selectedMembers.size > maxMemberLimit) AppColors.error else AppColors.secondaryText,
                    fontWeight = FontWeight.W500
                )
            }

            // 内容区域 - 自适应填满剩余高度
            HorizontalPager(
                state = pagerState,
                modifier = Modifier
                    .fillMaxWidth()
                    .weight(1f) // 这里让列表占据剩余空间
            ) {
                when (it) {
                    0 -> {
                        // AI智能体列表
                        AiAgentListScreen(
                            searchText = searchText.text,
                            selectedMemberIds = selectedMemberIds,
                            onMemberSelect = { member ->
                                val (newSelectedMemberIds, newSelectedMembers) = CreateGroupChatViewModel.toggleMemberSelection(
                                    member, selectedMemberIds, selectedMembers
                                )
                                selectedMemberIds = newSelectedMemberIds
                                selectedMembers = newSelectedMembers
                            }
                        )
                    }
                    1 -> {
                        // 朋友列表
                        FriendListScreen(
                            searchText = searchText.text,
                            selectedMemberIds = selectedMemberIds,
                            onMemberSelect = { member ->
                                val (newSelectedMemberIds, newSelectedMembers) = CreateGroupChatViewModel.toggleMemberSelection(
                                    member, selectedMemberIds, selectedMembers
                                )
                                selectedMemberIds = newSelectedMemberIds
                                selectedMembers = newSelectedMembers
                            }
                        )
                    }
                }
            }

            // 余额和扣减积分显示（创建按钮上方）
            val buttonTopPadding = if (cost > 0) 4.dp else 16.dp
            if (cost > 0) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(start = 16.dp, end = 16.dp, bottom = 4.dp),
                    horizontalArrangement = Arrangement.Center
                ) {
                    Text(
                        text = "${stringResource(R.string.create_group_chat_current_balance)}: ${currentBalance.formatNumber()} ${stringResource(R.string.pai_coin)}",
                        fontSize = 12.sp,
                        color = AppColors.secondaryText
                    )
                    Spacer(modifier = Modifier.width(16.dp))
                    Text(
                        text = "${stringResource(R.string.create_group_chat_required_cost)} ${cost.formatNumber()} ${stringResource(R.string.pai_coin)}",
                        fontSize = 12.sp,
                        color = AppColors.secondaryText
                    )
                }
            }

            // 创建群聊按钮 - 固定在底部
            Button(
                onClick = {
                    // 创建群聊逻辑
                    if (selectedMembers.isNotEmpty()) {
                        // 检查是否超过上限
                        if (selectedMembers.size > maxMemberLimit) {
                            CreateGroupChatViewModel.showError(context.getString(R.string.create_group_chat_exceed_limit, maxMemberLimit))
                            return@Button
                        }
                        // 如果费用大于0，显示确认弹窗
                        if (cost > 0) {
                            CreateGroupChatViewModel.showConfirmDialog()
                        } else {
                            // 费用为0，直接创建
                            scope.launch {
                                val success = CreateGroupChatViewModel.createGroupChat(
                                    groupName = groupName.text,
                                    selectedMembers = selectedMembers,
                                    context = context
                                )
                                if (success) {
                                    navController.popBackStack()
                                }
                            }
                        }
                    }
                },
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(start = 16.dp, end = 16.dp, top = buttonTopPadding, bottom = navigationBarPadding + 16.dp),
                colors = ButtonDefaults.buttonColors(
                    containerColor = AppColors.main,
                    contentColor = AppColors.mainText,
                    disabledContainerColor = AppColors.disabledBackground,
                    disabledContentColor = AppColors.text
                ),
                shape = RoundedCornerShape(24.dp),
                enabled = groupName.text.isNotEmpty() && selectedMembers.isNotEmpty() && !CreateGroupChatViewModel.isLoading
            ) {
                if (CreateGroupChatViewModel.isLoading) {
                    Text(
                        text = stringResource(R.string.agent_createing),
                        fontSize = 16.sp,
                        fontWeight = FontWeight.W600
                    )
                } else {
                    Text(
                        text = stringResource(R.string.create_group_chat),
                        fontSize = 16.sp,
                        fontWeight = FontWeight.W600
                    )
                }
            }

        }

        // 消费确认弹窗
        if (CreateGroupChatViewModel.showConfirmDialog) {
            CreateGroupChatConfirmDialog(
                cost = cost,
                currentBalance = currentBalance,
                balanceAfterCost = balanceAfterCost,
                isBalanceSufficient = isBalanceSufficient,
                onConfirm = {
                    // 检查是否超过上限
                    if (selectedMembers.size > maxMemberLimit) {
                        CreateGroupChatViewModel.hideConfirmDialog()
                        CreateGroupChatViewModel.showError(context.getString(R.string.create_group_chat_exceed_limit, maxMemberLimit))
                    } else {
                        CreateGroupChatViewModel.hideConfirmDialog()
                        scope.launch {
                            val success = CreateGroupChatViewModel.createGroupChat(
                                groupName = groupName.text,
                                selectedMembers = selectedMembers,
                                context = context
                            )
                            if (success) {
                                navController.popBackStack()
                            }
                        }
                    }
                },
                onCancel = {
                    CreateGroupChatViewModel.hideConfirmDialog()
                }
            )
        }

        // 居中显示的错误提示弹窗
        CreateGroupChatViewModel.errorMessage?.let { error ->
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(24.dp)
            ) {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .align(Alignment.Center), // 在Box中居中对齐
                    horizontalAlignment = Alignment.CenterHorizontally, // 水平居中
                    verticalArrangement = Arrangement.Center // 垂直居中
                ) {
                    Card(
                        modifier = Modifier
                            .fillMaxWidth(0.8f),
                        shape = RoundedCornerShape(8.dp)
                    ) {
                        Text(
                            text = error,
                            modifier = Modifier
                                .padding(16.dp)
                                .fillMaxWidth(),
                            color = Color.Red,
                            fontSize = 14.sp,
                            textAlign = TextAlign.Center
                        )
                    }
                }
            }
        }
    }
}

/**
 * 创建群聊消费确认弹窗
 */
@Composable
fun CreateGroupChatConfirmDialog(
    cost: Int,
    currentBalance: Int,
    balanceAfterCost: Int,
    isBalanceSufficient: Boolean,
    onConfirm: () -> Unit,
    onCancel: () -> Unit
) {
    val AppColors = LocalAppTheme.current
    
    Dialog(
        onDismissRequest = onCancel,
        properties = DialogProperties(
            dismissOnBackPress = true,
            dismissOnClickOutside = true
        )
    ) {
        Card(
            modifier = Modifier
                .fillMaxWidth(0.9f)
                .padding(16.dp),
            shape = RoundedCornerShape(16.dp)
        ) {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(24.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                // 硬币图标（使用文本代替，实际项目中可以使用图片资源）
                Box(
                    modifier = Modifier
                        .size(64.dp)
                        .background(
                            brush = Brush.linearGradient(
                                colors = listOf(
                                    Color(0xFFFFD700), // 金色
                                    Color(0xFFFFA500)  // 橙色
                                )
                            ),
                            shape = CircleShape
                        ),
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = "pai",
                        color = Color.White,
                        fontSize = 20.sp,
                        fontWeight = FontWeight.Bold
                    )
                }
                
                Spacer(modifier = Modifier.height(16.dp))
                
                // 标题
                Text(
                    text = stringResource(R.string.create_group_chat_confirm_title),
                    fontSize = 18.sp,
                    fontWeight = FontWeight.Bold,
                    color = AppColors.text
                )
                
                Spacer(modifier = Modifier.height(24.dp))
                
                // 需要消耗
                CostInfoRow(
                    label = stringResource(R.string.create_group_chat_required_cost),
                    amount = cost,
                    AppColors = AppColors
                )
                
                Spacer(modifier = Modifier.height(12.dp))
                
                // 当前余额
                CostInfoRow(
                    label = stringResource(R.string.create_group_chat_current_balance),
                    amount = currentBalance,
                    AppColors = AppColors
                )
                
                Spacer(modifier = Modifier.height(12.dp))
                
                // 消耗后余额
                CostInfoRow(
                    label = stringResource(R.string.create_group_chat_balance_after),
                    amount = balanceAfterCost,
                    AppColors = AppColors
                )
                
                // 余额不足提示
                if (!isBalanceSufficient) {
                    Spacer(modifier = Modifier.height(12.dp))
                    Text(
                        text = stringResource(R.string.create_group_chat_insufficient_balance),
                        color = Color.Red,
                        fontSize = 14.sp
                    )
                }
                
                Spacer(modifier = Modifier.height(24.dp))
                
                // 按钮行
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(12.dp)
                ) {
                    // 取消按钮
                    OutlinedButton(
                        onClick = onCancel,
                        modifier = Modifier
                            .weight(1f)
                            .height(48.dp)
                            .border(
                                width = 1.dp,
                                color = AppColors.secondaryText.copy(alpha = 0.3f),
                                shape = RoundedCornerShape(24.dp)
                            ),
                        colors = ButtonDefaults.outlinedButtonColors(
                            contentColor = AppColors.text
                        ),
                        shape = RoundedCornerShape(24.dp)
                    ) {
                        Text(
                            text = stringResource(R.string.cancel),
                            fontSize = 16.sp,
                            fontWeight = FontWeight.W600
                        )
                    }
                    
                    // 确认消耗按钮
                    Button(
                        onClick = onConfirm,
                        modifier = Modifier
                            .weight(1f)
                            .height(48.dp),
                        enabled = isBalanceSufficient,
                        colors = ButtonDefaults.buttonColors(
                            containerColor = AppColors.main,
                            contentColor = AppColors.mainText,
                            disabledContainerColor = AppColors.disabledBackground,
                            disabledContentColor = AppColors.text
                        ),
                        shape = RoundedCornerShape(24.dp)
                    ) {
                        Text(
                            text = stringResource(R.string.create_group_chat_confirm_consume),
                            fontSize = 16.sp,
                            fontWeight = FontWeight.W600
                        )
                    }
                }
            }
        }
    }
}

/**
 * 费用信息行组件
 */
@Composable
fun CostInfoRow(
    label: String,
    amount: Int,
    AppColors: com.aiosman.ravenow.AppThemeData
) {
    Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(
            text = label,
            fontSize = 14.sp,
            color = AppColors.text
        )
        
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(4.dp)
        ) {
            // 小硬币图标（使用简单的圆形）
            Box(
                modifier = Modifier
                    .size(16.dp)
                    .background(
                        brush = Brush.linearGradient(
                            colors = listOf(
                                Color(0xFFFFD700),
                                Color(0xFFFFA500)
                            )
                        ),
                        shape = CircleShape
                    )
            )
            
            Text(
                text = "${amount.formatNumber()} ${stringResource(R.string.pai_coin)}",
                fontSize = 14.sp,
                fontWeight = FontWeight.W600,
                color = AppColors.text
            )
        }
    }
}

/**
 * 格式化数字，添加千位分隔符
 */
fun Int.formatNumber(): String {
    return this.toString().reversed().chunked(3).joinToString(",").reversed()
}
