package com.aiosman.ravenow.ui.group

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import com.airbnb.lottie.compose.LottieAnimation
import com.airbnb.lottie.compose.LottieCompositionSpec
import com.airbnb.lottie.compose.LottieConstants
import com.airbnb.lottie.compose.rememberLottieComposition
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.ravenow.LocalAppTheme
import com.aiosman.ravenow.ui.composables.CustomAsyncImage
import com.aiosman.ravenow.ui.modifiers.noRippleClickable
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun FriendListScreen(
    searchText: String,
    selectedMemberIds: Set<String> = emptySet(),
    onMemberSelect: (GroupMember) -> Unit
) {
    val AppColors = LocalAppTheme.current
    val context = LocalContext.current
    val scope = rememberCoroutineScope()
    val listState = rememberLazyListState()
    
    // 使用ViewModel
    val viewModel = remember { FriendListViewModel() }
    
    // 获取过滤后的数据
    val filteredFriends = viewModel.getFilteredFriends(searchText)
    
    // 下拉刷新状态
    val pullRefreshState = rememberPullRefreshState(
        refreshing = viewModel.isRefreshing,
        onRefresh = {
            viewModel.refresh()
        }
    )
    
    // 上拉加载更多
    LaunchedEffect(listState) {
        snapshotFlow { listState.layoutInfo.visibleItemsInfo }
            .collect { visibleItems ->
                if (visibleItems.isNotEmpty()) {
                    val lastVisibleItem = visibleItems.last()
                    if (lastVisibleItem.index >= filteredFriends.size - 3 && viewModel.hasMoreData && !viewModel.isLoadingMore && !viewModel.isLoading) {
                        viewModel.loadMore()
                    }
                }
            }
    }
    
    // 显示错误信息
    viewModel.errorMessage?.let { error ->
        LaunchedEffect(error) {
            // 可以在这里显示错误提示
        }
    }
    
    Box(
        modifier = Modifier
            .fillMaxSize()
            .pullRefresh(pullRefreshState)
    ) {
        if (viewModel.isLoading && filteredFriends.isEmpty()) {
            Box(
                modifier = Modifier.fillMaxSize(),
                contentAlignment = Alignment.Center
            ) {
                LottieAnimation(
                    composition = rememberLottieComposition(LottieCompositionSpec.Asset("star_Loader.lottie")).value,
                    iterations = LottieConstants.IterateForever,
                    modifier = Modifier.size(80.dp)
                )
            }
        } else {
            LazyColumn(
                state = listState,
                modifier = Modifier.fillMaxSize(),
                contentPadding = PaddingValues(16.dp),
                verticalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                items(
                    items = filteredFriends,
                    key = { it.id }
                ) { friend ->
                    MemberItem(
                        member = friend,
                        isSelected = selectedMemberIds.contains(friend.id),
                        onSelect = { onMemberSelect(friend) }
                    )
                }
                
                // 加载更多指示器
                if (viewModel.isLoadingMore) {
                    item {
                        Box(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(16.dp),
                            contentAlignment = Alignment.Center
                        ) {
                            LottieAnimation(
                                composition = rememberLottieComposition(LottieCompositionSpec.Asset("star_Loader.lottie")).value,
                                iterations = LottieConstants.IterateForever,
                                modifier = Modifier.size(80.dp)
                            )
                        }
                    }
                }
            }
        }
        
        // 下拉刷新指示器
        PullRefreshIndicator(
            refreshing = viewModel.isRefreshing,
            state = pullRefreshState,
            modifier = Modifier.align(Alignment.TopCenter),
            backgroundColor = AppColors.background,
            contentColor = AppColors.main
        )
    }
}
