package com.aiosman.riderpro.ui.index.tabs.message

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 androidx.paging.map
import com.aiosman.riderpro.data.AccountNotice
import com.aiosman.riderpro.data.AccountService
import com.aiosman.riderpro.entity.CommentEntity
import com.aiosman.riderpro.entity.CommentPagingSource
import com.aiosman.riderpro.data.CommentRemoteDataSource
import com.aiosman.riderpro.data.CommentService
import com.aiosman.riderpro.data.AccountServiceImpl
import com.aiosman.riderpro.data.CommentServiceImpl
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

object MessageListViewModel : ViewModel() {
    val accountService: AccountService = AccountServiceImpl()
    var noticeInfo by mutableStateOf<AccountNotice?>(null)

    private val commentService: CommentService = CommentServiceImpl()
    private val _commentItemsFlow = MutableStateFlow<PagingData<CommentEntity>>(PagingData.empty())
    val commentItemsFlow = _commentItemsFlow.asStateFlow()
    var isLoading by mutableStateOf(false)
    var isFirstLoad = true
    suspend fun initData(force: Boolean = false) {
        if (!isFirstLoad && !force) {
            return
        }
        if (force) {
            isLoading = true
        }
        isFirstLoad = false
        val info = accountService.getMyNoticeInfo()
        noticeInfo = info
        viewModelScope.launch {
            Pager(
                config = PagingConfig(pageSize = 5, enablePlaceholders = false),
                pagingSourceFactory = {
                    CommentPagingSource(
                        CommentRemoteDataSource(commentService),
                        selfNotice = true,
                        order = "latest"
                    )
                }
            ).flow.cachedIn(viewModelScope).collectLatest {
                _commentItemsFlow.value = it
            }
        }
        isLoading = false

    }

    val likeNoticeCount
        get() = noticeInfo?.likeCount ?: 0
    val followNoticeCount
        get() = noticeInfo?.followCount ?: 0
    val favouriteNoticeCount
        get() = noticeInfo?.favoriteCount ?: 0
    val commentNoticeCount
        get() = noticeInfo?.commentCount ?: 0

    private fun updateIsRead(id: Int) {
        val currentPagingData = _commentItemsFlow.value
        val updatedPagingData = currentPagingData.map { commentEntity ->
            if (commentEntity.id == id) {
                commentEntity.copy(unread = false)
            } else {
                commentEntity
            }
        }
        _commentItemsFlow.value = updatedPagingData
    }

    fun updateReadStatus(id: Int) {
        viewModelScope.launch {
            commentService.updateReadStatus(id)
            updateIsRead(id)
        }
    }

    fun clearLikeNoticeCount() {
        noticeInfo = noticeInfo?.copy(likeCount = 0)
    }

    fun clearFollowNoticeCount() {
        noticeInfo = noticeInfo?.copy(followCount = 0)
    }
    fun clearFavouriteNoticeCount() {
        noticeInfo = noticeInfo?.copy(favoriteCount = 0)
    }
}