package com.gh.common.util

import com.gh.gamecenter.common.utils.createRequestBody
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.common.utils.observableToMain
import com.gh.gamecenter.core.utils.EmptyCallback
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import com.lightgame.utils.Utils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.ResponseBody
import retrofit2.HttpException

/**
 * 可以在此类发送一些无关页面操作的请求
 */
object SimpleRequestHelper {
    private val mApi = RetrofitManager.getInstance().api

    fun reportGameComment(gameId: String, commentId: String, type: String) {
        val map = HashMap<String, String>()
        map["reason"] = type
        mApi.reportGameComment(gameId, commentId, map.createRequestBody())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    Utils.toast(HaloApp.getInstance().application, "举报成功")
                }

                override fun onFailure(e: HttpException?) {
                    ErrorHelper.handleError(HaloApp.getInstance().application, e?.response()?.errorBody()?.string())
                }
            })

    }

    fun reportGameCommentReply(gameId: String, commentId: String, replyId: String, type: String) {
        val map = HashMap<String, String>()
        map["reason"] = type
        mApi.reportGameCommentReply(gameId, commentId, replyId, map.createRequestBody())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    Utils.toast(HaloApp.getInstance().application, "举报成功")
                }

                override fun onFailure(e: HttpException?) {
                    ErrorHelper.handleError(HaloApp.getInstance().application, e?.response()?.errorBody()?.string())
                }
            })

    }

    fun deleteGameComment(gameId: String, commentId: String, callBack: EmptyCallback) {
        mApi.deleteGameComment(gameId, commentId)
            .compose(observableToMain())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    callBack.onCallback()
                }

                override fun onFailure(e: HttpException?) {
                    ErrorHelper.handleError(HaloApp.getInstance().application, e?.response()?.errorBody()?.string())
                }
            })
    }

    fun deleteGameCommentReply(gameId: String, commentId: String, replyId: String, callBack: EmptyCallback) {
        mApi.deleteGameCommentReply(gameId, commentId, replyId)
            .compose(observableToMain())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    callBack.onCallback()
                }

                override fun onFailure(e: HttpException?) {
                    ErrorHelper.handleError(HaloApp.getInstance().application, e?.response()?.errorBody()?.string())
                }
            })
    }
}