package com.aiosman.ravenow.ui.group

import androidx.compose.foundation.Image
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.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
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.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.aiosman.ravenow.LocalAppTheme
import com.aiosman.ravenow.LocalNavController
import com.aiosman.ravenow.R
import com.aiosman.ravenow.ui.composables.CustomAsyncImage
import com.aiosman.ravenow.ui.composables.StatusBarSpacer
import com.aiosman.ravenow.ui.index.NavItem
import com.aiosman.ravenow.ui.modifiers.noRippleClickable
import kotlinx.coroutines.launch

@Composable
fun GroupChatInfoScreen(groupId: String) {
    val navController = LocalNavController.current
    val context = LocalContext.current
    val AppColors = LocalAppTheme.current
    
    val viewModel = viewModel<GroupChatInfoViewModel>(
        key = "GroupChatInfoViewModel_$groupId",
        factory = object : ViewModelProvider.Factory {
            override fun <T : ViewModel> create(modelClass: Class<T>): T {
                return GroupChatInfoViewModel(groupId) as T
            }
        }
    )
    
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(AppColors.background)
    ) {
        // 顶部导航栏
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .background(AppColors.background)
        ) {
            StatusBarSpacer()
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = 16.dp, horizontal = 16.dp),
                horizontalArrangement = Arrangement.Start,
                verticalAlignment = Alignment.CenterVertically
            ) {
                Image(
                    painter = painterResource(R.drawable.rider_pro_back_icon),
                    modifier = Modifier
                        .size(24.dp)
                        .noRippleClickable {
                            navController.navigateUp()
                        },
                    contentDescription = null,
                    colorFilter = ColorFilter.tint(AppColors.text)
                )
                Spacer(modifier = Modifier.width(138.dp))
                Text(
                    modifier = Modifier.fillMaxWidth(),
                    textAlign = TextAlign.Start,
                    text = stringResource(R.string.group_info),
                    style = androidx.compose.ui.text.TextStyle(
                        color = AppColors.text,
                        fontSize = 17.sp,
                        fontWeight = FontWeight.Bold
                    )

                )
                Spacer(modifier = Modifier.width(40.dp))
            }
        }
        
        // 内容区域
        LazyColumn(
            modifier = Modifier.fillMaxSize(),
            contentPadding = PaddingValues(16.dp)
        ) {
            // 群聊头像和名称
            item {
                Column(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    if (viewModel.groupInfo?.groupAvatar?.isNotEmpty() == true) {
                        CustomAsyncImage(
                            imageUrl = viewModel.groupInfo!!.groupAvatar,
                            modifier = Modifier
                                .size(80.dp)
                                .clip(RoundedCornerShape(12.dp)),
                            contentDescription = "群聊头像"
                        )
                    } else {
                        Box(
                            modifier = Modifier
                                .size(80.dp)
                                .clip(CircleShape)
                                .background(AppColors.decentBackground),
                            contentAlignment = Alignment.Center
                        ) {
                            Text(
                                text = viewModel.groupInfo?.groupName?.firstOrNull()?.toString() ?: "",
                                style = androidx.compose.ui.text.TextStyle(
                                    color = AppColors.text,
                                    fontSize = 24.sp,
                                    fontWeight = FontWeight.Bold
                                )
                            )
                        }
                    }
                    
                    Spacer(modifier = Modifier.height(16.dp))
                    
                    Text(
                        text = viewModel.groupInfo?.groupName ?: "群聊",
                        style = androidx.compose.ui.text.TextStyle(
                            color = AppColors.text,
                            fontSize = 18.sp,
                            fontWeight = FontWeight.Bold
                        )
                    )
                }
            }
            
            // 操作按钮
            item {
                Spacer(modifier = Modifier.height(32.dp))
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceEvenly
                ) {
                    // 添加其他人
                    Column(
                        horizontalAlignment = Alignment.CenterHorizontally,
                        modifier = Modifier.noRippleClickable {
                            // TODO: 实现添加其他人功能
                        }
                    ) {
                        Box(
                            modifier = Modifier
                                .size(48.dp)
                                .clip(CircleShape),
                            contentAlignment = Alignment.Center
                        ) {
                            Image(
                                painter = painterResource(R.drawable.rider_pro_add_other),
                                modifier = Modifier.size(24.dp),
                                contentDescription = null,
                                colorFilter = ColorFilter.tint(
                                    AppColors.text)
                            )
                        }
                        Spacer(modifier = Modifier.height(8.dp))
                        Text(
                            text = stringResource(R.string.group_info_add_other),
                            style = androidx.compose.ui.text.TextStyle(
                                color = AppColors.text,
                                fontSize = 12.sp
                            )
                        )
                    }
                    
                    // 通知设置
                    Column(
                        horizontalAlignment = Alignment.CenterHorizontally,
                        modifier = Modifier.noRippleClickable {
                            viewModel.viewModelScope.launch {
                                /*if (viewModel.notificationStrategy == "mute") {
                                    viewModel.updateNotificationStrategy("active")
                                } else {
                                    viewModel.updateNotificationStrategy("mute")
                                }*/
                            }
                        }
                    ) {
                        Box(
                            modifier = Modifier
                                .size(48.dp)
                                .clip(CircleShape),
                            contentAlignment = Alignment.Center
                        ) {
                            Image(
                                painter = painterResource(if (viewModel.notificationStrategy == "mute") R.drawable.rider_pro_notice_mute else R.drawable.rider_pro_notice_active,),
                                modifier = Modifier.size(24.dp),
                                contentDescription = null,
                                colorFilter = ColorFilter.tint(
                                    AppColors.text)
                            )
                        }
                        Spacer(modifier = Modifier.height(8.dp))
                        Text(
                            text = stringResource(R.string.group_info_notice_setting),
                            style = androidx.compose.ui.text.TextStyle(
                                color = AppColors.text,
                                fontSize = 12.sp
                            )
                        )
                    }
                    
                    // 退出群聊
                    Column(
                        horizontalAlignment = Alignment.CenterHorizontally,
                        modifier = Modifier.noRippleClickable {
                            // TODO: 实现退出群聊功能
                        }
                    ) {
                        Box(
                            modifier = Modifier
                                .size(48.dp)
                                .clip(CircleShape),
                            contentAlignment = Alignment.Center
                        ) {
                            Image(
                                painter = painterResource(R.drawable.group_info_exit
                                ),
                                modifier = Modifier.size(24.dp),
                                contentDescription = null,
                                colorFilter = ColorFilter.tint(
                                    AppColors.text)
                            )
                        }
                        Spacer(modifier = Modifier.height(8.dp))
                        Text(
                            text = stringResource(R.string.group_info_exit),
                            style = androidx.compose.ui.text.TextStyle(
                                color = AppColors.text,
                                fontSize = 12.sp
                            )
                        )
                    }
                }
            }
            
            // 设置选项
            item {
                Spacer(modifier = Modifier.height(32.dp))
                
                // 设置聊天主题
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .clip(RoundedCornerShape(8.dp))
                        .padding(16.dp)
                        .noRippleClickable {
                            // TODO: 实现设置聊天主题功能
                        },
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Image(
                        painter = painterResource(R.drawable.group_info_edit),
                        modifier = Modifier.size(24.dp),
                        contentDescription = null,
                        colorFilter = ColorFilter.tint(
                            AppColors.text)
                    )
                    Spacer(modifier = Modifier.width(12.dp))
                    Text(
                        text = stringResource(R.string.group_info_edit),
                        style = androidx.compose.ui.text.TextStyle(
                            color = AppColors.text,
                            fontSize = 16.sp
                        ),
                        modifier = Modifier.weight(1f)
                    )
                    Image(
                        painter = painterResource(R.drawable.rave_now_nav_right),
                        modifier = Modifier.size(18.dp),
                        contentDescription = null,
                    )
                }
                
                Spacer(modifier = Modifier.height(1.dp))
                
                // 群聊成员
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .clip(RoundedCornerShape(8.dp))
                        .padding(16.dp)
                        .noRippleClickable {
                            // TODO: 实现查看群聊成员功能
                        },
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Image(
                        painter = painterResource(R.drawable.group_info_users),
                        modifier = Modifier.size(24.dp),
                        contentDescription = null,
                        colorFilter = ColorFilter.tint(
                            AppColors.text)
                    )
                    Spacer(modifier = Modifier.width(12.dp))
                    Text(
                        text = "群聊成员 (${viewModel.groupInfo?.memberCount ?: 0})",
                        style = androidx.compose.ui.text.TextStyle(
                            color = AppColors.text,
                            fontSize = 16.sp
                        ),
                        modifier = Modifier.weight(1f)
                    )
                    Image(
                        painter = painterResource(R.drawable.rave_now_nav_right),
                        modifier = Modifier.size(18.dp),
                        contentDescription = null,
                    )
                }

                Spacer(modifier = Modifier.height(1.dp))
                //仅自己可见
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .clip(RoundedCornerShape(8.dp))
                        .padding(16.dp)
                        .noRippleClickable {
                            // TODO: 实现仅自己可见功能
                        },
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Image(
                        painter = painterResource(R.mipmap.rider_pro_change_password),
                        modifier = Modifier.size(24.dp),
                        contentDescription = null,
                        colorFilter = ColorFilter.tint(
                            AppColors.text)
                    )
                    Spacer(modifier = Modifier.width(12.dp))
                    Text(
                        text = "仅自己可见",
                        style = androidx.compose.ui.text.TextStyle(
                            color = AppColors.text,
                            fontSize = 16.sp
                        ),
                        modifier = Modifier.weight(1f)
                    )
                    Switch(
                        checked = true,
                        onCheckedChange = {
                        // TODO: 实现群聊仅自己可见功能
                        },
                        colors = SwitchDefaults.colors(
                            checkedThumbColor = Color.White,
                            checkedTrackColor = Color.Green,
                            uncheckedThumbColor = Color.White,
                            uncheckedTrackColor = AppColors.main.copy(alpha = 0.5f),
                            uncheckedBorderColor = Color.Transparent
                        ),
                        modifier = Modifier.scale(1f).height(18.dp)
                    )
                }
            }
        }
    }
}
