package com.gh.gamecenter.servers.patch

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.gh.gamecenter.R
import com.gh.gamecenter.entity.ServerCalendarEntity
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import com.lightgame.utils.Utils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.MediaType
import okhttp3.RequestBody
import okhttp3.ResponseBody
import org.json.JSONObject
import retrofit2.HttpException

class PatchKaifuViewModel(application: Application) : AndroidViewModel(application) {
    var apiService = RetrofitManager.getInstance().api

    var serverEntity: ServerCalendarEntity? = null

    val postResult = MutableLiveData<ServerCalendarEntity>()

    var gameId: String? = null

    fun initData(serverEntity: ServerCalendarEntity?, gameId: String) {
        this.serverEntity = serverEntity
        this.gameId = gameId
    }

    fun post(isPatch: Boolean, patchName: String, patchRemark: String) {
        val observable = if (isPatch) {
            val jsonObject = JSONObject()
            jsonObject.put("note", patchName)
            jsonObject.put("remark", patchRemark)
            val body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString())
            apiService.patchKaifu(body, gameId, serverEntity!!.id)
        } else {
            apiService.deleteKaifu(gameId, serverEntity!!.id)
        }
        observable
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    super.onResponse(response)
                    val kaiFuCalendarEntity = ServerCalendarEntity()
                    kaiFuCalendarEntity.id = serverEntity?.id
                    if (isPatch) {
                        kaiFuCalendarEntity.setNote(patchName)
                        kaiFuCalendarEntity.remark = patchRemark
                        kaiFuCalendarEntity.setTime(serverEntity?.getTime()!!)
                        postResult.postValue(kaiFuCalendarEntity)
                    } else {
                        postResult.postValue(kaiFuCalendarEntity)
                    }

                    Utils.toast(getApplication(), "提交成功")
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    if (e != null) {
                        val string = e.response().errorBody()?.string()
                        val objects = JSONObject(string)
                        val code = objects.getInt("code")
                        if (code == 403043) {
                            Utils.toast(getApplication(), "提交失败，可能由于数据没有刷新，请5-10分钟后再试")
                            return
                        } else if (code == 400001) {
                            val kaiFuCalendarEntity = ServerCalendarEntity()
                            kaiFuCalendarEntity.id = serverEntity?.id
                            postResult.postValue(kaiFuCalendarEntity)
                            Utils.toast(getApplication(), "删除成功")
                            return
                        }
                    }
                    Utils.toast(getApplication(), R.string.post_failure_hint)
                }
            })
    }
}
