package com.gh.common.util

import android.annotation.SuppressLint
import android.content.Context
import com.gh.gamecenter.common.constant.Constants
import com.gh.common.repository.ReservationRepository
import com.gh.gamecenter.R
import com.gh.gamecenter.WebActivity
import com.gh.gamecenter.common.callback.CancelListener
import com.gh.gamecenter.common.callback.ConfirmListener
import com.gh.gamecenter.common.entity.WechatConfigEntity
import com.gh.gamecenter.core.utils.*
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.common.utils.*
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

object ReservationHelper {

    @JvmStatic
    fun deleteReservation(game: GameEntity, refreshCallback: EmptyCallback) {
        deleteOrCancelReservation(game, true, refreshCallback)
    }

    @JvmStatic
    fun cancelReservation(game: GameEntity, refreshCallback: EmptyCallback) {
        deleteOrCancelReservation(game, false, refreshCallback)
    }

    @SuppressLint("CheckResult")
    private fun deleteOrCancelReservation(
        game: GameEntity,
        deleteReservation: Boolean,
        refreshCallback: EmptyCallback
    ) {
        val retrofit = RetrofitManager.getInstance()
        val requestMap = hashMapOf<String, String>()
        requestMap["game_id"] = game.id

        val single = if (deleteReservation) {
            retrofit.api
                .deleteGameReservation(requestMap.createRequestBody())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
        } else {
            retrofit.api
                .cancelGameReservation(requestMap.createRequestBody())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
        }

        single.subscribe(object : BiResponse<ResponseBody>() {
            override fun onSuccess(data: ResponseBody) {
                ReservationRepository.removeReservationFromMemoryAndRefresh(game.id)
                refreshCallback.onCallback()
            }

            override fun onFailure(exception: Exception) {
                Utils.toast(HaloApp.getInstance().application, exception.message)
                exception.printStackTrace()
            }
        })
    }

    @JvmStatic
    @SuppressLint("CheckResult")
    fun reserve(context: Context, gameId: String, callback: EmptyCallback) {
        val requestMap = hashMapOf<String, String>()
        requestMap["game_id"] = gameId
        RetrofitManager.getInstance().api
            .createNewGameReservation(requestMap.createRequestBody())
            .compose(singleToMain())
            .subscribe(object : BiResponse<ResponseBody>() {

                override fun onSuccess(data: ResponseBody) {
                    ReservationRepository.addReservationToMemoryAndRefresh(gameId)
                    callback.onCallback()
                    val wechatConfig = SPUtils.getString(Constants.SP_WECHAT_CONFIG).toObject<WechatConfigEntity>()
                    wechatConfig?.run {
                        NewLogUtils.logReserveGameSuccess(wechatConfig)
                        if (bind && follow && notice) {
                            NewLogUtils.logReserveWechatSuccessPopShow()
                            DialogUtils.showReserveSuccessDialog(context)
                        } else {
                            NewLogUtils.logReserveWechatRemindPopShow(wechatConfig)
                            DialogUtils.showReserveSuccess2WechatBindDialog(context, object : ConfirmListener {
                                override fun onConfirm() {
                                    NewLogUtils.logReserveWechatRemindPopClick(wechatConfig, "开启微信提醒")
                                    context.startActivity(WebActivity.getBindWechatIntent(context))
                                }
                            }, object : CancelListener {
                                override fun onCancel() {
                                    NewLogUtils.logReserveWechatRemindPopClick(wechatConfig, "关闭弹窗")
                                }
                            })
                        }
                    }
                }

                override fun onFailure(exception: Exception) {
                    ToastUtils.showToast(exception.message ?: "")
                }
            })
    }

    @JvmStatic
    fun showDeleteReservationDialog(context: Context, emptyCallback: EmptyCallback) {
        DialogUtils.showCancelOrDeleteReservationDialog(
            context,
            "删除预约",
            "游戏已上线，你可以删除此预约记录，确定删除吗？",
            "确定删除",
            "暂不删除", object : ConfirmListener {
                override fun onConfirm() {
                    emptyCallback.onCallback()
                }
            }, null
        )
    }

    @JvmStatic
    fun showCancelReservationDialog(context: Context, emptyCallback: EmptyCallback) {
        showCancelReservationDialog(context, emptyCallback, null)
    }

    @JvmStatic
    fun showCancelReservationDialog(context: Context, emptyCallback: EmptyCallback, cancelListener: CancelListener?) {
        DialogHelper.showDialog(context, "取消预约",
            "取消之后你将无法收到游戏上线的通知，确定取消预约吗？",
            "确定取消",
            "暂不取消", confirmClickCallback = {
                emptyCallback.onCallback()
            }, cancelClickCallback = {
                cancelListener?.onCancel()
            }, uiModificationCallback = {
                it.confirmTv.setTextColor(R.color.theme_red.toColor(context))
            }, extraConfig = DialogHelper.Config(centerContent = true, centerTitle = true)
        )
    }


}