package com.aiosman.riderpro.ui.profile

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.aiosman.riderpro.data.AccountService
import com.aiosman.riderpro.data.AccountServiceImpl
import com.aiosman.riderpro.data.MomentService
import com.aiosman.riderpro.data.UserServiceImpl
import com.aiosman.riderpro.entity.AccountProfileEntity
import com.aiosman.riderpro.entity.MomentEntity
import com.aiosman.riderpro.entity.MomentPagingSource
import com.aiosman.riderpro.entity.MomentRemoteDataSource
import com.aiosman.riderpro.entity.MomentServiceImpl
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class AccountProfileViewModel : ViewModel() {
    var profileId by mutableStateOf(0)
    val accountService: AccountService = AccountServiceImpl()
    val momentService: MomentService = MomentServiceImpl()
    val userService = UserServiceImpl()
    var profile by mutableStateOf<AccountProfileEntity?>(null)
    private var _momentsFlow = MutableStateFlow<PagingData<MomentEntity>>(PagingData.empty())
    var momentsFlow = _momentsFlow.asStateFlow()
    var refreshing by mutableStateOf(false)
    fun loadProfile(id: String, pullRefresh: Boolean = false) {
        viewModelScope.launch {
            if (pullRefresh) {
                refreshing = true
            }
            if (profileId == profile?.id) {
                return@launch
            }
            profile = userService.getUserProfile(id)
            refreshing = false
            Pager(
                config = PagingConfig(pageSize = 5, enablePlaceholders = false),
                pagingSourceFactory = {
                    MomentPagingSource(
                        MomentRemoteDataSource(momentService),
                        author = profile!!.id
                    )
                }
            ).flow.cachedIn(viewModelScope).collectLatest {
                _momentsFlow.value = it
            }
        }
    }

    fun followUser(userId: String) {
        viewModelScope.launch {
            userService.followUser(userId)
            profile = profile?.copy(followerCount = profile!!.followerCount + 1, isFollowing = true)
        }
    }

    fun unFollowUser(userId: String) {
        viewModelScope.launch {
            userService.unFollowUser(userId)
            profile =
                profile?.copy(followerCount = profile!!.followerCount - 1, isFollowing = false)
        }
    }

    val followerCount get() = profile?.followerCount ?: 0
    val followingCount get() = profile?.followingCount ?: 0
    val bio get() = profile?.bio ?: ""
    val nickName get() = profile?.nickName ?: ""
    val avatar get() = profile?.avatar

}