package com.aiosman.ravenow.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.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.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.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.paging.compose.collectAsLazyPagingItems
import com.aiosman.ravenow.AppState
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.ui.comment.NoticeScreenHeader
import com.aiosman.ravenow.ui.composables.CustomAsyncImage
import com.aiosman.ravenow.ui.composables.FollowButton
import com.aiosman.ravenow.ui.composables.StatusBarMaskLayout
import com.aiosman.ravenow.ui.modifiers.noRippleClickable
import com.aiosman.ravenow.ui.network.NetworkErrorContent
import kotlinx.coroutines.launch
import com.aiosman.ravenow.utils.NetworkUtils

/**
 * 关注消息列表
 */
@Composable
fun FollowerNoticeScreen(includeStatusBarPadding: Boolean = true) {
    val scope = rememberCoroutineScope()
    val AppColors = LocalAppTheme.current
    StatusBarMaskLayout(
        modifier = Modifier.background(color = AppColors.background).padding(horizontal = 16.dp),
        darkIcons = !AppState.darkMode,
        maskBoxBackgroundColor = AppColors.background,
        includeStatusBarPadding = includeStatusBarPadding
    ) {
        val model = FollowerNoticeViewModel
        var dataFlow = model.followerItemsFlow
        var followers = dataFlow.collectAsLazyPagingItems()
        LaunchedEffect(Unit) {
            model.reload()
            model.updateNotice()
        }
        val isNetworkAvailable = NetworkUtils.isNetworkAvailable(LocalContext.current)

        if (!isNetworkAvailable) {
            NetworkErrorContent(
                onReload = {
                    model.reload(force = true)
                }
            )
        } else if (followers.itemCount == 0) {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(top=149.dp),
                contentAlignment = Alignment.TopCenter
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Image(
                        painter = painterResource(id = R.mipmap.invalid_name_5),
                        contentDescription = "No Followers",
                        modifier = Modifier
                            .size(width = 181.dp, height = 153.dp)
                    )
                    Spacer(modifier = Modifier.height(9.dp))
                    androidx.compose.material.Text(
                        text = stringResource(R.string.no_one_pinged_yet),
                        color = AppColors.text,
                        fontSize = 16.sp,
                        fontWeight = FontWeight.W600,
                        textAlign = TextAlign.Center,
                        modifier = Modifier.padding(horizontal = 24.dp),
                        maxLines = 2,
                        overflow = TextOverflow.Ellipsis
                    )
                }
            }
        }else{
            LazyColumn(
                modifier = Modifier.weight(1f)
                    .background(color = AppColors.background)
            ) {
                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 AppColors = LocalAppTheme.current
    val context = LocalContext.current
    val navController = LocalNavController.current
    Box(
        modifier = Modifier
            .padding(vertical = 16.dp)
            .noRippleClickable {
                navController.navigate(
                    NavigationRoute.AccountProfile.route.replace(
                        "{id}",
                        userId.toString()
                    )
                )
            }
    ) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            CustomAsyncImage(
                context = context,
                imageUrl = avatar,
                contentDescription = nickname,
                modifier = Modifier
                    .size(40.dp)
                    .clip(CircleShape)
            )
            Spacer(modifier = Modifier.width(12.dp))
            Column(
                modifier = Modifier.weight(1f)
            ) {
                Text(
                    text = nickname,
                    fontWeight = FontWeight.Bold,
                    fontSize = 16.sp,
                    color = AppColors.text,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis
                )
            }
            if (!isFollowing && userId != AppState.UserId) {
                FollowButton(
                    isFollowing = false,
                ) {
                    onFollow()
                }
            }
        }
    }
}