package com.gh.gamecenter.qa.video.detail.comment

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.ForumVideoEntity
import com.gh.gamecenter.qa.article.detail.CommentItemData
import com.gh.gamecenter.qa.comment.base.BaseCommentViewModel
import com.google.gson.reflect.TypeToken
import com.halo.assistant.HaloApp
import io.reactivex.Observable

class VideoCommentViewModel(
    application: Application,
    videoId: String,
    bbsId: String,
    topCommentId: String
) : BaseCommentViewModel(application, "", videoId, "", bbsId, "", topCommentId) {
    var videoDetail: ForumVideoEntity? = null
    val deleteCommentLiveData = MutableLiveData<Boolean>()

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

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) { list ->
            val itemDataList = arrayListOf<CommentItemData>()
            if (list.isNullOrEmpty() && mLoadStatusLiveData.value == LoadStatus.INIT_EMPTY) {
                itemDataList.add(CommentItemData(errorEmpty = true))
            } else if (list.isNullOrEmpty() && mLoadStatusLiveData.value == LoadStatus.INIT_FAILED) {
                itemDataList.add(CommentItemData(errorConnection = true))
            } else {
                list.forEachIndexed { index, entity ->
                    handleTopComment(index, entity)
                    itemDataList.add(CommentItemData(commentNormal = entity))
                }
                itemDataList.add(CommentItemData(footer = true))
            }
            mResultLiveData.postValue(itemDataList)
        }
    }

    override fun getHandleTopCommentCondition(index: Int) =
        !isHandleTopComment && topCommentId.isNotBlank() && index == 0

    override fun hideCommentSuccess() {
        videoDetail?.count?.comment = (videoDetail?.count?.comment ?: 0) - 1
        deleteCommentLiveData.postValue(true)
    }

    class Factory(private val videoId: String, private val bbsId: String, private val topCommentId: String) :
        ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return VideoCommentViewModel(HaloApp.getInstance().application, videoId, bbsId, topCommentId) as T
        }
    }

    companion object {
        const val ATTACHED_LIMIT_COUNT = 2 // 获取回复数量
    }
}