package com.gh.gamecenter.gamecollection.detail.conversation

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.feature.entity.CommentEntity
import com.gh.gamecenter.gamecollection.detail.GameCollectionDetailViewModel
import com.gh.gamecenter.qa.article.detail.CommentItemData
import com.gh.gamecenter.common.retrofit.BiResponse
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
import retrofit2.HttpException

class GameCollectionCommentConversationViewModel(
    application: Application,
    gameCollectionId: String = "",
    var commentId: String = "",
    topCommentId: String = ""
) : GameCollectionDetailViewModel(application, gameCollectionId, topCommentId) {
    var commentDetail: CommentEntity? = null
    var positionInOriginList = -1

    override fun provideDataObservable(page: Int) = null

    override fun provideDataSingle(page: Int): Single<MutableList<CommentEntity>> {
        val map = hashMapOf<String, Any>()
        map["sort"] = if (currentSortType == SortType.LATEST) "latest" else "earliest"
        if (!isHandleTopComment && topCommentId.isNotEmpty()) {
            map["top_comment_id"] = topCommentId
        }
        return mApi.getGameCollectionCommentReply(gameCollectionId, commentId, page, map)
    }

    @SuppressLint("CheckResult")
    fun getComment() {
        mApi.getGameCollectionComment(gameCollectionId, commentId)
            .subscribeOn(Schedulers.io())
            .subscribe(object : BiResponse<CommentEntity>() {
                @SuppressLint("CheckResult")
                override fun onSuccess(data: CommentEntity) {
                    commentDetail = data
                    commentDetail?.floor = positionInOriginList
                    commentCount = data.reply
                    topItemData = CommentItemData(commentTop = data)
                    loadResultLiveData.postValue(LoadResult.SUCCESS)
                    mergeListData(mListLiveData.value, hasFilter = topCommentId.isNotBlank())
                }

                override fun onFailure(exception: Exception) {
                    if (exception is HttpException && exception.code().toString().contains("404")) {
                        loadResultLiveData.postValue(LoadResult.DELETED)
                    } else {
                        loadResultLiveData.postValue(LoadResult.NETWORK_ERROR)
                    }
                }
            })
    }

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) { mergeListData(it, hasFilter = topCommentId.isNotBlank()) }
    }

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

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

        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return GameCollectionCommentConversationViewModel(
                application = application,
                gameCollectionId = gameCollectionId,
                commentId = commentId,
                topCommentId = topCommentId
            ) as T
        }
    }
}