package com.gh.gamecenter.gamedetail.fuli.kaifu

import android.annotation.SuppressLint
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.common.util.CheckLoginUtils
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.livedata.NonStickyMutableLiveData
import com.gh.gamecenter.common.utils.toRequestBody
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.core.utils.UrlFilterUtils
import com.gh.gamecenter.feature.entity.ServerCalendarNotifySetting
import com.gh.gamecenter.feature.entity.WXSubscribeMsgConfig
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class ServersCalendarDetailNoDataViewModel(
    val serverTimeInMills: Long
) : ViewModel() {

    val error = NonStickyMutableLiveData<Throwable>()

    val wxSubscribeMsgConfigLiveData = MutableLiveData<WXSubscribeMsgConfig>()

    val serverCalendarRemindLiveData: MutableLiveData<ServerCalendarNotifySetting?> = MutableLiveData()

    private var isDataInit = false

    var appRemind: Boolean = false

    val wechatRemind: Boolean = false

    val timeInSeconds: Int = (serverTimeInMills / 1000).toInt()

    fun initData(notifySetting: ServerCalendarNotifySetting?) {
        if (isDataInit) return
        appRemind = notifySetting?.byApp ?: SPUtils.getBoolean(Constants.SP_SERVERS_CALENDAR_BY_APP, true)
//        wechatRemind = notifySetting?.byWechat ?: SPUtils.getBoolean(Constants.SP_SERVERS_CALENDAR_BY_WECHAT, true)
        isDataInit = true
    }

    @SuppressLint("CheckResult")
    fun getServerCalenderRemind(gameId: String) {
        if (!CheckLoginUtils.isLogin()) {
            serverCalendarRemindLiveData.postValue(null)
            return
        }
        RetrofitManager.getInstance().newApi
            .getServerCalendarRemind(gameId, timeInSeconds.toString())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                serverCalendarRemindLiveData.postValue(it)
            }, {
                serverCalendarRemindLiveData.postValue(null)
            })
    }

    @SuppressLint("CheckResult")
    fun addServerCalendarRemind(gameId: String) {
        val requestBody = mapOf(
            "by_app" to appRemind,
            "by_wechat" to wechatRemind
        ).toRequestBody()
        RetrofitManager.getInstance().newApi
            .addServerCalendarRemind(gameId, timeInSeconds.toString(), requestBody)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                getServerCalenderRemind(gameId)
            }, {
                error.postValue(it)
            })
    }

    @SuppressLint("CheckResult")
    fun removeServerCalendarRemind(gameId: String) {
        RetrofitManager.getInstance().newApi
            .removeServerCalendarRemind(gameId, timeInSeconds.toString())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                getServerCalenderRemind(gameId)
            }, {
                error.postValue(it)
            })
    }

    @SuppressLint("CheckResult")
    fun getWXSubscribeMsgConfig() {
        RetrofitManager.getInstance().newApi
            .getWxSubscribeMsgConfig(UrlFilterUtils.getFilterQuery("type", "server_new"))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                {
                    wxSubscribeMsgConfigLiveData.postValue(it)
                }, {
                    error.postValue(it)
                })
    }

    @SuppressLint("CheckResult")
    fun addServerCalendarRemindWithWechat(
        gameId: String,
        openId: String?,
        templateId: String?,
        action: String,
        reserved: String,
        scene: Int,
    ) {
        val requestBody = mapOf(
            "openid" to openId,
            "template_id" to templateId,
            "action" to action,
            "scene" to scene,
            "user_id" to reserved
        ).toRequestBody()

        RetrofitManager.getInstance().newApi
            .postWxSubscribeMsgCallback(UrlFilterUtils.getFilterQuery("type", "server_new"), requestBody)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                addServerCalendarRemind(gameId)
            }, {
                error.postValue(it)
            })
    }

    class Factory(
        private val serverTimeInMills: Long
    ) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return ServersCalendarDetailNoDataViewModel(serverTimeInMills) as T
        }
    }
}