package com.gh.gamecenter.qa.article.detail

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.common.baselist.LoadStatus
import com.gh.gamecenter.common.utils.toJson
import com.gh.gamecenter.core.utils.GsonUtils
import com.gh.gamecenter.feature.entity.CommentEntity
import com.gh.gamecenter.feature.entity.MeEntity
import com.gh.gamecenter.qa.comment.base.BaseCommentViewModel
import com.gh.gamecenter.retrofit.RetrofitManager
import com.google.gson.reflect.TypeToken
import io.reactivex.Observable

class ArticleDetailCommentListViewModel(
    application: Application,
    articleId: String = "",
    communityId: String = "",
    topCommentId: String
) : BaseCommentViewModel(
    application = application,
    articleId = articleId,
    communityId = communityId,
    videoId = "",
    questionId = "",
    gameCollectionId = "",
    topCommentId = topCommentId) {

    val total = MutableLiveData<Int>()

    override fun provideDataObservable(page: Int): Observable<MutableList<CommentEntity>> {
        val map = hashMapOf<String, Any>()
        if (!isHandleTopComment && topCommentId.isNotEmpty()) {
            map["top_comment_id"] = topCommentId
        }
        return RetrofitManager.getInstance().api.getCommunityArticleCommentList(
            articleId,
            currentSortType.value,
            page,
            map
        ).map {
            mTotal = it.headers().get("total")?.toInt() ?: 0
            total.postValue(mTotal)
            val type = object : TypeToken<List<CommentEntity>>() {}.type
            GsonUtils.gson.fromJson(it.body()?.toJson() ?: "", type)
        }
    }

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) {
            mergeListData(mListLiveData.value)
        }
    }

    override fun mergeListData(commentList: List<CommentEntity>?, hasFilter: Boolean) {
        val mergedList = arrayListOf<CommentItemData>().apply {
            if (commentList.isNullOrEmpty() && mLoadStatusLiveData.value == LoadStatus.INIT_EMPTY) {
                add(CommentItemData(errorEmpty = true))
            } else if (commentList.isNullOrEmpty() && mLoadStatusLiveData.value == LoadStatus.INIT_FAILED) {
                add(CommentItemData(errorConnection = true))
            } else {
                commentList?.forEachIndexed { index, entity ->
                    // 没有 me 会导致不能跨页面更新点赞
                    if (entity.me == null) {
                        entity.me = MeEntity()
                    }
                    handleTopComment(index, entity)
                    add(CommentItemData(commentNormal = entity))
                }
                add(CommentItemData(footer = true))
            }
        }
        mResultLiveData.postValue(mergedList)
    }

    class Factory(
        private val application: Application,
        private val articleId: String = "",
        private val communityId: String = "",
        private val topCommentId: String = ""
    ) : ViewModelProvider.NewInstanceFactory() {

        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return ArticleDetailCommentListViewModel(
                application = application,
                articleId = articleId,
                communityId = communityId,
                topCommentId = topCommentId
            ) as T
        }
    }
}