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

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
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.LocalAppTheme
import com.aiosman.ravenow.LocalNavController
import com.aiosman.ravenow.R
import com.aiosman.ravenow.entity.AccountProfileEntity
import com.aiosman.ravenow.ui.NavigationRoute
import com.aiosman.ravenow.ui.composables.CustomAsyncImage
import com.aiosman.ravenow.ui.composables.rememberDebouncer
import com.aiosman.ravenow.ui.modifiers.noRippleClickable

@Composable
fun UserItem(
    accountProfileEntity: AccountProfileEntity,
    postCount: Int = 0
) {
    val navController = LocalNavController.current
    val AppColors = LocalAppTheme.current
    val followerDebouncer = rememberDebouncer()
    val followingDebouncer = rememberDebouncer()

    Column(
        modifier = Modifier
            .fillMaxWidth()
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically
        ) {
            // 头像
            CustomAsyncImage(
                LocalContext.current,
                accountProfileEntity.avatar,
                modifier = Modifier
                    .clip(CircleShape)
                    .size(48.dp),
                contentDescription = "",
                contentScale = ContentScale.Crop
            )
            Spacer(modifier = Modifier.width(32.dp))
            //个人统计
            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier.weight(1f)
            ) {
                // 帖子数
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.weight(1f)
                ) {
                    Text(
                        text = postCount.toString(),
                        fontWeight = FontWeight.W600,
                        fontSize = 16.sp,
                        color = AppColors.text
                    )
                    Spacer(modifier = Modifier.height(8.dp))
                    Text(
                        text = "帖子",
                        color = AppColors.text
                    )
                }

                // 粉丝数
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier
                        .weight(1f)
                        .noRippleClickable {
                            followerDebouncer {
                                navController.navigate(
                                    NavigationRoute.FollowerList.route.replace(
                                        "{id}",
                                        accountProfileEntity.id.toString()
                                    )
                                )
                            }
                        }
                ) {
                    Text(
                        text = accountProfileEntity.followerCount.toString(),
                        fontWeight = FontWeight.W600,
                        fontSize = 16.sp,
                        color = AppColors.text
                    )
                    Spacer(modifier = Modifier.height(8.dp))
                    Text(
                        text = "粉丝",
                        color = AppColors.text
                    )
                }

                // 关注数
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier
                        .weight(1f)
                        .noRippleClickable {
                            followingDebouncer {
                                navController.navigate(
                                    NavigationRoute.FollowingList.route.replace(
                                        "{id}",
                                        accountProfileEntity.id.toString()
                                    )
                                )
                            }
                        }
                ) {
                    Text(
                        text = accountProfileEntity.followingCount.toString(),
                        fontWeight = FontWeight.W600,
                        fontSize = 16.sp,
                        color = AppColors.text
                    )
                    Spacer(modifier = Modifier.height(8.dp))
                    Text(
                        text = "关注",
                        color = AppColors.text
                    )
                }
            }
        }
        Spacer(modifier = Modifier.height(12.dp))
        // 昵称
        Text(
            text = accountProfileEntity.nickName,
            fontWeight = FontWeight.W600,
            fontSize = 16.sp,
            color = AppColors.text
        )
        Spacer(modifier = Modifier.height(4.dp))
        // 个人简介
        if (accountProfileEntity.bio.isNotEmpty()){
            Text(
                text = accountProfileEntity.bio,
                fontSize = 14.sp,
                color = AppColors.secondaryText,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )
        }else{
            Text(
                text = "No bio here.",
                fontSize = 14.sp,
                color = AppColors.secondaryText,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )
        }
    }

}
