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.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.lazy.LazyColumn
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
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.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.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.collectAsLazyPagingItems
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.exp.viewModelFactory
import com.aiosman.ravenow.ui.NavigationRoute
import com.aiosman.ravenow.ui.comment.NoticeScreenHeader
import com.aiosman.ravenow.ui.composables.StatusBarMaskLayout
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun FollowingListScreen(userId: Int) {
    val appColors = LocalAppTheme.current
    val navController = LocalNavController.current
    val model : FollowingListViewModel = viewModel(factory = viewModelFactory {
        FollowingListViewModel(userId)
    }, key = "viewModel_${userId}")
    val scope = rememberCoroutineScope()
    val refreshState = rememberPullRefreshState(model.isLoading, onRefresh = {
        model.loadData(userId, true)
    })
    LaunchedEffect(Unit) {
        model.loadData(userId)
    }
    StatusBarMaskLayout(
        modifier = Modifier.background(color = appColors.background).padding(horizontal = 16.dp),
        darkIcons = !AppState.darkMode,
        maskBoxBackgroundColor = appColors.background
    ) {
        var dataFlow = model.usersFlow
        var users = dataFlow.collectAsLazyPagingItems()
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .padding(vertical = 16.dp)
        ) {
            NoticeScreenHeader(stringResource(R.string.following_upper), moreIcon = false)

        }
        if(users.itemCount == 0) {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(top=149.dp),
                contentAlignment = Alignment.TopCenter
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Image(
                        painter = painterResource(
                            id =if(AppState.darkMode) R.mipmap.qst_gz_qs_as_img_my
                            else R.mipmap.qst_gz_qs_img_my),
                        contentDescription = null,
                        modifier = Modifier.size(181.dp)
                    )
                    Spacer(modifier = Modifier.size(24.dp))
                    androidx.compose.material.Text(
                        text = "没有关注任何灵魂",
                        color = appColors.text,
                        fontSize = 16.sp,
                        fontWeight = FontWeight.W600
                    )
                    Spacer(modifier = Modifier.size(8.dp))
                    androidx.compose.material.Text(
                        text = "探索一下，总有一个你想靠近的光点 ✨",
                        color = appColors.secondaryText,
                        fontSize = 14.sp,
                        fontWeight = FontWeight.W400
                    )
                }
            }
        }else{
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .weight(1f)
                    .pullRefresh(refreshState)
            ) {
                LazyColumn(
                    modifier = Modifier.fillMaxSize()
                ) {
                    items(users.itemCount) { index ->
                        users[index]?.let { user ->
                            FollowItem(
                                avatar = user.avatar,
                                nickname = user.nickName,
                                userId = user.id,
                                isFollowing = user.isFollowing
                            ) {
                                // 检查游客模式，如果是游客则跳转登录
                                if (GuestLoginCheckOut.needLogin(GuestLoginCheckOutScene.FOLLOW_USER)) {
                                    navController.navigate(NavigationRoute.Login.route)
                                } else {
                                    scope.launch {
                                        if (user.isFollowing) {
                                            model.unFollowUser(user.id)
                                        } else {
                                            model.followUser(user.id)
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                PullRefreshIndicator(
                    refreshing = model.isLoading,
                    state = refreshState,
                    modifier = Modifier.align(Alignment.TopCenter)
                )
            }
        }
    }
}