package com.aiosman.ravenow.ui.group

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.aiosman.ravenow.AppStore
import com.aiosman.ravenow.data.UserService
import com.aiosman.ravenow.data.UserServiceImpl
import com.aiosman.ravenow.data.api.ApiClient
import kotlinx.coroutines.launch

class FriendListViewModel : ViewModel() {
    private val userService: UserService = UserServiceImpl()
    
    // 状态管理
    var friends by mutableStateOf<List<GroupMember>>(emptyList())
        private set
    
    var isLoading by mutableStateOf(false)
        private set
    
    var isRefreshing by mutableStateOf(false)
        private set
    
    var isLoadingMore by mutableStateOf(false)
        private set
    
    var currentPage by mutableStateOf(1)
        private set
    
    var hasMoreData by mutableStateOf(true)
        private set
    
    var errorMessage by mutableStateOf<String?>(null)
        private set
    
    private val pageSize = 20
    
    init {
        loadFriends(1)
    }
    
    // 加载朋友数据
    fun loadFriends(page: Int, isRefresh: Boolean = false) {
        viewModelScope.launch {
            try {
                if (isRefresh) {
                    isRefreshing = true
                } else if (page == 1) {
                    isLoading = true
                } else {
                    isLoadingMore = true
                }
                
                errorMessage = null
                
                val userData = userService.getUsers(pageSize, page)
                val newFriends: List<GroupMember> = userData.list.map { user ->
                    GroupMember(
                        id = user.chatAIId,
                        name = user.nickName,
                        avatar = user.avatar,
                        isAi = false
                    )
                }
                    
                    if (isRefresh || page == 1) {
                        friends = newFriends
                        currentPage = 1
                    } else {
                        friends = friends + newFriends
                        currentPage = page
                    }
                    
                    hasMoreData = newFriends.size >= pageSize
                } catch (e: Exception) {
                    errorMessage = "获取朋友列表失败: ${e.message}"
            } finally {
                isLoading = false
                isRefreshing = false
                isLoadingMore = false
            }
        }
    }
    
    // 刷新数据
    fun refresh() {
        loadFriends(1, true)
    }
    
    // 加载更多数据
    fun loadMore() {
        if (hasMoreData && !isLoadingMore && !isLoading) {
            loadFriends(currentPage + 1)
        }
    }
    
    // 清除错误信息
    fun clearError() {
        errorMessage = null
    }
    
    // 搜索过滤
    fun getFilteredFriends(searchText: String): List<GroupMember> {
        return if (searchText.isEmpty()) {
            friends
        } else {
            friends.filter { it.name.contains(searchText, ignoreCase = true) }
        }
    }
}
