package com.aiosman.ravenow.ui.index.tabs.profile.composable

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
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.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
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.layout.ContentScale
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.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.ravenow.AppState
import com.aiosman.ravenow.GuestLoginCheckOut
import com.aiosman.ravenow.GuestLoginCheckOutScene
import com.aiosman.ravenow.LocalAppTheme
import com.aiosman.ravenow.LocalNavController
import com.aiosman.ravenow.R
import com.aiosman.ravenow.ui.NavigationRoute
import com.aiosman.ravenow.entity.AgentEntity
import com.aiosman.ravenow.ui.composables.CustomAsyncImage
import com.aiosman.ravenow.utils.DebounceUtils

@Composable
fun UserAgentsList(
    agents: List<AgentEntity>,
    onAgentClick: (AgentEntity) -> Unit = {},
    onAvatarClick: (AgentEntity) -> Unit = {},
    modifier: Modifier = Modifier
) {
    val AppColors = LocalAppTheme.current

    LazyColumn(
        modifier = modifier.fillMaxSize(),
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        if (agents.isEmpty()) {
            item {
                EmptyAgentsView()
            }
        } else {
            items(agents) { agent ->
                UserAgentCard(
                    agent = agent,
                    onAgentClick = onAgentClick,
                    onAvatarClick = onAvatarClick
                )
            }
        }
        
        // 底部间距
        item {
            Spacer(modifier = Modifier.height(120.dp))
        }
    }
}

@Composable
fun UserAgentCard(
    agent: AgentEntity,
    onAgentClick: (AgentEntity) -> Unit,
    onAvatarClick: (AgentEntity) -> Unit = {}
) {
    val AppColors = LocalAppTheme.current
    val navController = LocalNavController.current
    
    // 防抖状态
    var lastClickTime by remember { mutableStateOf(0L) }

    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp, vertical = 8.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        // 左侧头像
        Box(
            modifier = Modifier
                .size(48.dp)
                .background(AppColors.nonActive, RoundedCornerShape(24.dp))
                .clickable {
                    if (DebounceUtils.simpleDebounceClick(lastClickTime, 500L) {
                        onAvatarClick(agent)
                    }) {
                        lastClickTime = System.currentTimeMillis()
                    }
                },
            contentAlignment = Alignment.Center
        ) {
            if (agent.avatar.isNotEmpty()) {
                CustomAsyncImage(
                    imageUrl = agent.avatar,
                    contentDescription = "Agent头像",
                    modifier = Modifier
                        .size(48.dp)
                        .clip(RoundedCornerShape(24.dp)),
                    contentScale = ContentScale.Crop
                )
            } else {
                Image(
                    painter = painterResource(R.mipmap.rider_pro_agent),
                    contentDescription = "默认头像",
                    modifier = Modifier.size(24.dp),
                    colorFilter = ColorFilter.tint(AppColors.secondaryText)
                )
            }
        }

        Spacer(modifier = Modifier.width(12.dp))

        // 中间文字内容
        Column(
            modifier = Modifier
                .weight(1f)
                .padding(end = 8.dp)
        ) {
            // 标题
            Text(
                text = agent.title,
                fontSize = 14.sp,
                fontWeight = FontWeight.W600,
                color = AppColors.text,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )

            Spacer(modifier = Modifier.height(8.dp))

            // 描述
            Text(
                text = agent.desc,
                fontSize = 12.sp,
                color = AppColors.secondaryText,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )
        }

        // 右侧聊天按钮
        Box(
            modifier = Modifier
                .size(width = 60.dp, height = 32.dp)
                .background(
                    color = AppColors.nonActive,
                    shape = RoundedCornerShape(8.dp)
                )
                .clickable {
                    if (DebounceUtils.simpleDebounceClick(lastClickTime, 500L) {
                        // 检查游客模式，如果是游客则跳转登录
                        if (GuestLoginCheckOut.needLogin(GuestLoginCheckOutScene.CHAT_WITH_AGENT)) {
                            navController.navigate(NavigationRoute.Login.route)
                        } else {
                            onAgentClick(agent)
                        }
                    }) {
                        lastClickTime = System.currentTimeMillis()
                    }
                },
            contentAlignment = Alignment.Center
        ) {
            Text(
                text = stringResource(R.string.chat),
                fontSize = 12.sp,
                color = AppColors.text,
                fontWeight = FontWeight.W500
            )
        }
    }
}

@Composable
fun EmptyAgentsView() {
    val AppColors = LocalAppTheme.current

    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 60.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Image(
            painter = painterResource(
                id =if(AppState.darkMode) R.mipmap.qs_ai_qs_as_img
                else R.mipmap.qs_ai_qs_img),
            contentDescription = "暂无Agent",
            modifier = Modifier.size(181.dp),
        )
        
        Spacer(modifier = Modifier.height(24.dp))
        
        Text(
            text = "专属AI等你召唤",
            fontSize = 16.sp,
            color = AppColors.text,
            fontWeight = FontWeight.W600
        )
        
        Spacer(modifier = Modifier.height(8.dp))
        
        Text(
            text = "AI将成为你的伙伴，而不是工具",
            fontSize = 14.sp,
            color = AppColors.secondaryText,
            fontWeight = FontWeight.W400
        )
    }
}
