package com.gh.gamecenter.personalhome.home.game

import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.common.util.*
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.common.baselist.LoadType
import com.gh.gamecenter.common.utils.toObject
import com.gh.gamecenter.core.utils.UrlFilterUtils
import com.gh.gamecenter.common.entity.ErrorEntity
import com.gh.gamecenter.personalhome.rating.MyRating
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import com.lightgame.utils.Utils
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.ResponseBody
import retrofit2.HttpException

class UserCommentHistoryViewModel(application: Application, var userId: String) :
    ListViewModel<MyRating, MyRating>(application) {

    private val mApi = RetrofitManager.getInstance().api

    var type: TYPE = TYPE.ALL

    override fun provideDataObservable(page: Int): Observable<MutableList<MyRating>>? {
        return null
    }

    override fun provideDataSingle(page: Int): Single<MutableList<MyRating>> {
        return mApi.getMyRating(userId, page, getFilter())
    }

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) { mResultLiveData.postValue(it) }
    }


    fun voteComment(gameId: String, commentId: String, callback: () -> Unit) {
        mApi.voteGameComment(gameId, commentId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    Utils.toast(getApplication(), "点赞成功")
                    callback.invoke()
                }

                override fun onFailure(e: HttpException?) {
                    val string = e?.response()?.errorBody()?.string()
                    val errorEntity = string?.toObject<ErrorEntity>()
                    if (errorEntity?.code == 403008) {
                        onResponse(null)
                        return
                    }
                    ErrorHelper.handleError(getApplication(), string)
                }
            })
    }

    fun unVoteComment(gameId: String, commentId: String, callback: () -> Unit) {
        mApi.unvoteGameComment(gameId, commentId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    super.onResponse(response)
                    Utils.toast(getApplication(), "取消点赞")
                    callback.invoke()
                }

                override fun onFailure(e: HttpException?) {
                    val string = e?.response()?.errorBody()?.string()
                    ErrorHelper.handleError(getApplication(), string)
                }
            })
    }


    fun changeType(type: TYPE) {
        if (this.type != type) {
            this.type = type
            load(LoadType.REFRESH)
        }
    }

    private fun getFilter(): String {
        return UrlFilterUtils.getFilterQuery(
            "view", "halo",
            "type", type.value
        )

    }

    enum class TYPE(val value: String) {
        ALL(""),
        JINGXUAN("jingxuan"),
        ANLIWALL("anliwall");
    }

    class Factory(
        private val mApplication: Application,
        private val mUserId: String
    ) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return UserCommentHistoryViewModel(mApplication, mUserId) as T
        }
    }

}