package com.aiosman.riderpro.ui.follower

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
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.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.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
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.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.unit.dp
import androidx.compose.ui.unit.sp
import androidx.paging.compose.collectAsLazyPagingItems
import com.aiosman.riderpro.AppState
import com.aiosman.riderpro.LocalNavController
import com.aiosman.riderpro.R
import com.aiosman.riderpro.ui.NavigationRoute
import com.aiosman.riderpro.ui.composables.StatusBarMaskLayout
import com.aiosman.riderpro.ui.comment.NoticeScreenHeader
import com.aiosman.riderpro.ui.composables.CustomAsyncImage
import com.aiosman.riderpro.ui.composables.FollowButton
import com.aiosman.riderpro.ui.modifiers.noRippleClickable
import kotlinx.coroutines.launch

/**
 * 关注消息列表
 */
@Composable
fun FollowerNoticeScreen() {
    val scope = rememberCoroutineScope()
    StatusBarMaskLayout(
        modifier = Modifier.background(color = Color(0xFFFFFFFF)).padding(horizontal = 16.dp),
        darkIcons = true,
        maskBoxBackgroundColor = Color(0xFFFFFFFF)
    ) {
        val model = FollowerNoticeViewModel
        var dataFlow = model.followerItemsFlow
        var followers = dataFlow.collectAsLazyPagingItems()
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .padding(vertical = 16.dp)
                .background(color = Color(0xFFFFFFFF))
        ) {
            NoticeScreenHeader(stringResource(R.string.followers_upper), moreIcon = false)
        }
        LaunchedEffect(Unit) {
            model.reload()
            model.updateNotice()
        }
        LazyColumn(
            modifier = Modifier.weight(1f)
                .background(color = Color(0xFFFFFFFF))
        ) {
            items(followers.itemCount) { index ->
                followers[index]?.let { follower ->
                    FollowItem(
                        avatar = follower.avatar,
                        nickname = follower.nickname,
                        userId = follower.userId,
                        isFollowing = follower.isFollowing
                    ) {
                        scope.launch {
                            model.followUser(follower.userId)
                        }
                    }
                }
            }
        }
    }
}


@Composable
fun FollowItem(
    avatar: String,
    nickname: String,
    userId: Int,
    isFollowing: Boolean,
    onFollow: () -> Unit = {}
) {
    val context = LocalContext.current
    val navController = LocalNavController.current
    Box(
        modifier = Modifier.padding(vertical = 16.dp)
    ) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            CustomAsyncImage(
                context = context,
                imageUrl = avatar,
                contentDescription = nickname,
                modifier = Modifier
                    .size(40.dp)
                    .clip(CircleShape)
                    .noRippleClickable {
                        navController.navigate(
                            NavigationRoute.AccountProfile.route.replace(
                                "{id}",
                                userId.toString()
                            )
                        )
                    }
            )
            Spacer(modifier = Modifier.width(12.dp))
            Column(
                modifier = Modifier.weight(1f)
            ) {
                Text(nickname, fontWeight = FontWeight.Bold, fontSize = 16.sp)
            }
//            if (userId != AppState.UserId) {
//                FollowButton(
//                    isFollowing = isFollowing,
//                    onFollowClick = onFollow,
//                    fontSize = 14.sp,
//                    imageModifier = Modifier
//                        .width(100.dp)
//                        .height(24.dp)
//                )
//            }

//            Box(
//                modifier = Modifier.noRippleClickable {
//                    onFollow()
//                }
//            ) {
//                Image(
//                    painter = painterResource(id = R.drawable.follow_bg),
//                    contentDescription = "Follow",
//                    modifier = Modifier
//                        .width(79.dp)
//                        .height(24.dp)
//                )
//                Text(
//                    text = if (isFollowing) {
//                        stringResource(R.string.following_upper)
//                    } else {
//                        stringResource(R.string.follow_upper)
//                    },
//                    fontSize = 14.sp,
//                    color = Color(0xFFFFFFFF),
//                    modifier = Modifier.align(
//                        Alignment.Center
//                    )
//                )
//            }


        }
    }
}