package com.gh.gamecenter.forum.moderator

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.R
import com.gh.gamecenter.entity.ApplyModeratorStatusEntity
import com.gh.gamecenter.entity.PersonalEntity
import com.gh.gamecenter.eventbus.EBUserFollow
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import com.google.gson.JsonObject
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 org.greenrobot.eventbus.EventBus
import retrofit2.HttpException

class ModeratorListViewModel(application: Application, val bbsId: String) :
    AndroidViewModel(application) {

    val moderators = MutableLiveData<ArrayList<PersonalEntity>>()
    val qqGroupNumber = MutableLiveData<String>()
    val qqGroupKey = MutableLiveData<String>()
    val isModerators = MutableLiveData<Boolean>()
    val statusEntity = MutableLiveData<ApplyModeratorStatusEntity>()

    init {
        getModerators()
    }

    fun getModeratorsApplyStatus() {
        RetrofitManager.getInstance().api.getApplyModeratorStatus(bbsId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ApplyModeratorStatusEntity>() {
                override fun onResponse(response: ApplyModeratorStatusEntity?) {
                    super.onResponse(response)
                    statusEntity.postValue(response)
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    statusEntity.postValue(null)
                }
            })
    }

    fun getModerators() {
        RetrofitManager.getInstance().api.getModerators(bbsId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ArrayList<PersonalEntity>>() {
                override fun onResponse(response: ArrayList<PersonalEntity>?) {
                    super.onResponse(response)
                    moderators.postValue(response)
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    moderators.postValue(null)
                }
            })
    }

    @SuppressLint("CheckResult")
    fun getModeratorsInfo() {
        RetrofitManager.getInstance().api.getModeratorsInfo(bbsId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<JsonObject>() {
                override fun onSuccess(data: JsonObject) {
                    isModerators.postValue(data["is_moderators"].asBoolean)
                    qqGroupNumber.postValue(data["moderators_qq_group"].asString)
                    qqGroupKey.postValue(data["moderators_qq_group_key"].asString)
                }

                override fun onFailure(exception: Exception) {
                    super.onFailure(exception)
                    Utils.log(exception)
                }
            })
    }

    fun followingCommand(userId: String, isFollow: Boolean, onSuccess: () -> Unit) {
        val observable = if (isFollow) {
            RetrofitManager.getInstance().api.postFollowing(userId)
        } else {
            RetrofitManager.getInstance().api.deleteFollowing(userId)
        }
        observable
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ResponseBody>() {
                override fun onResponse(response: ResponseBody?) {
                    super.onResponse(response)
                    onSuccess.invoke()
                    if (isFollow) {
                        Utils.toast(getApplication(), R.string.concern_success)
                    } else {
                        Utils.toast(getApplication(), R.string.concern_cancel)
                    }
                    EventBus.getDefault().post(EBUserFollow(userId, isFollow))
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    Utils.toast(getApplication(), R.string.loading_failed_hint)
                }
            })
    }

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