package com.adealink.weparty.profile.dynamic.fragment.photo.viewmodel

import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.livedata.ExtMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.frame.network.data.PageReq
import com.adealink.weparty.App
import com.adealink.weparty.network.data.NoMoreDataError
import com.adealink.weparty.profile.datasource.remote.DynamicHttpService
import com.adealink.weparty.profile.dynamic.data.DynamicCommentData
import com.adealink.weparty.profile.dynamic.data.DynamicCommentListReq
import com.adealink.weparty.profile.dynamic.data.DynamicDetailData
import com.adealink.weparty.profile.dynamic.fragment.data.BaseDynamicDetailItem
import com.adealink.weparty.profile.dynamic.fragment.data.DynamicDetailCommentItem
import com.adealink.weparty.profile.dynamic.fragment.data.DynamicDetailCommentTitleItem
import com.adealink.weparty.profile.dynamic.fragment.data.DynamicDetailPhotoHeaderItem
import com.adealink.weparty.util.PageHandler
import kotlinx.coroutines.launch

class DynamicPhotoViewModel : BaseViewModel() {

    private val dynamicHttpService by lazy {
        App.instance.networkService.getHttpService(DynamicHttpService::class.java)
    }

    private var dynamicDetail: DynamicDetailData? = null
    private var commentTotal: Long = 0

    private val commentIdSet = mutableSetOf<String>()
    private var commentList = mutableListOf<DynamicCommentData>()

    private val pageHandler = PageHandler()
    private var newCommentCount = 0

    val itemListLD = ExtMutableLiveData<List<BaseDynamicDetailItem>>()
    val itemListRltLD = ExtMutableLiveData<Rlt<Any>>()

    fun loadItemListIfEmpty(dynamicDetail: DynamicDetailData?) {
        viewModelScope.launch {
            if (!itemListLD.value.isNullOrEmpty()) {
                return@launch
            }
            this@DynamicPhotoViewModel.dynamicDetail = dynamicDetail
            notifyItemChanged()
            loadCommentList()
        }
    }

    fun loadCommentList() {
        viewModelScope.launch {
            commentIdSet.clear()
            commentList.clear()
            pageHandler.reset()
            newCommentCount = 0
            loadMoreComment()
        }
    }


    fun reloadCommentList() {
        loadCommentList()
    }

    fun hasMoreData(): Boolean {
        return !pageHandler.isEnd
    }

    fun loadMoreComment() {
        if (pageHandler.isEnd) {
            itemListRltLD.send(Rlt.Failed(NoMoreDataError()))
            return
        }
        viewModelScope.launch {
            val dynamicId = dynamicDetail?.id
            if (dynamicId.isNullOrEmpty()) {
                return@launch
            }
            val rlt = dynamicHttpService.dynamicCommentList(
                DynamicCommentListReq(
                    dynamicId,
                    page = PageReq(size = pageHandler.pageSize, next = pageHandler.currentPage)
                )
            )
            when (rlt) {
                is Rlt.Failed -> {
                    itemListRltLD.send(rlt)
                }

                is Rlt.Success -> {
                    pageHandler.nextPage(rlt.data.data?.next)
                    commentTotal = rlt.data.data?.total ?: 0

                    val nextList = rlt.data.data?.list?.filter {
                        !commentIdSet.contains(it.id)
                    } ?: emptyList()
                    commentIdSet.addAll(nextList.map { it.id })
                    commentList.addAll(nextList)
                    itemListRltLD.send(rlt)
                    notifyItemChanged()
                }
            }
        }
    }

    private fun notifyItemChanged() {
        viewModelScope.launch {
            val dynamicList = mutableListOf<BaseDynamicDetailItem>()
            val dynamicDetail = dynamicDetail
            if (dynamicDetail != null) {
                dynamicList.add(
                    DynamicDetailPhotoHeaderItem(dynamicDetail)
                )
            }

            if (commentList.isNotEmpty()) {
                val commentTotalCount = newCommentCount + commentTotal
                dynamicList.add(
                    DynamicDetailCommentTitleItem(commentTotalCount)
                )
                dynamicList.addAll(
                    commentList.map {
                        DynamicDetailCommentItem(it)
                    }
                )
            }
            itemListLD.send(dynamicList)
        }
    }

    fun onNewCommentAdd(comment: DynamicCommentData) {
        if (commentIdSet.contains(comment.id)) {
            return
        }
        commentIdSet.add(comment.id)
        commentList.add(0, comment)
        newCommentCount++
        notifyItemChanged()
    }

}