package com.zxbw.modulemain.view

import android.app.Activity
import android.app.Dialog
import android.graphics.Paint
import android.text.Html
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.yarolegovich.discretescrollview.DiscreteScrollView
import com.yarolegovich.discretescrollview.transform.Pivot
import com.yarolegovich.discretescrollview.transform.ScaleTransformer
import com.zxbw.modulemain.R
import com.zxbw.modulemain.box.adapter.BoxResultAdapter
import com.zxbw.modulemain.box.contract.LotteryContract
import com.zxbw.modulemain.box.presenter.LotteryPresenter
import com.zxhl.cms.common.Constant
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.box.*
import com.zxhl.cms.pay.PayDialog
import com.zxhl.cms.utils.*
import com.zxhl.cms.widget.RecycleImageView
import com.zxhl.main.page.`interface`.PopupWidowCloseCallBack
import org.greenrobot.eventbus.EventBus
import java.text.DecimalFormat


/**
 * @author (wangXuewei)
 * @datetime 2022-03-04 14:08 GMT+8
 * @detail :
 */
object BoxResultDialog : LotteryContract.View, PopupWidowCloseCallBack {
    private var mPresenter: LotteryPresenter = LotteryPresenter(this);
    private var mActivity: Activity? = null
    private var boxId: String = ""
    private var onePrice: Double = 0.0
    private var fivePrice: Double = 0.0
    private var newUserPrice: String? = ""
    private var page: String? = ""
    private var mRePumpingCardNum = 0//重抽卡数量
    private var ids: String? = ""
    private var recoverprice: String = ""
    private var recytype: String = ""
    private var continuityId: String = ""
    private var isShowFive: Int = 1
    private var rewardCoinNum: String = ""
    private var dialogDismissCallback: DialogDismissCallback? = null
    fun setDialogDismissCallback(callback: DialogDismissCallback?) {
        this.dialogDismissCallback = callback
    }

    interface DialogDismissCallback {
        fun onDismiss(boxId: String)
    }

    var goodsImg: String = ""
    var goodsName: String = ""
    var goodsPrice: String = ""
    var goodsNum: String = ""
    var goodsType: String = ""

    fun setDialogGoodsInfo(
        goodsImg: String,
        goodsName: String,
        goodsPrice: String,
        goodsNum: String,
    ) {
        this.goodsImg = goodsImg
        this.goodsName = goodsName
        this.goodsPrice = goodsPrice
        this.goodsNum = goodsNum
    }

    fun setDialogGoodsNum(
        goodsNum: String,
    ) {
        this.goodsNum = goodsNum
    }

    fun showFiveLotteryResult(
        mActivity: Activity,
        boxId: String,
        onePrice: Double,
        fivePrice: Double,
        cardNum: Int = 0
    ) {
        page = "1"
        if (!isFast()) {
            return
        }
        if (boxId == "" || fivePrice == 0.0) {
            return
        }
        EventUtils.onEvent("five_lottery_boxid", "" + boxId)
        isShowFive = 1
        mRePumpingCardNum = cardNum
        var ownCoin = SettingPreference.getUserCoinNum().toDouble()
        if (ownCoin < fivePrice) {
            if (mDialog != null) {
                if (mDialog!!.isShowing) {
                    mDialog!!.dismiss()
                }
            }
            if (TextUtils.equals(Utils.getChannel(mActivity), "yyb")) {
                showPayDialog(
                    mActivity,
                    (fivePrice - ownCoin).toString(),
                    AdCallback { _, result ->
                        if (result == 0) {
                            showFiveLotteryResult(
                                mActivity,
                                boxId,
                                onePrice,
                                fivePrice,
                                cardNum
                            )
                        }
                    })
            } else {
                JumpUtils.payJump(
                    boxId,
                    (fivePrice - ownCoin).toString(),
                    Constant.Key.FIVE_LOTTERY,
                    onePrice.toString(),
                    fivePrice.toString(),
                    "",
                    page
                )
            }

            return
        } else if (Constant.Switch.isOpenLogin && !Constant.Switch.isBindLogin && boxId != "-2") {
            //开启登录开关 并且未登录绑定
            JumpUtils.AfterLoginJump(
                boxId,
                (onePrice - ownCoin).toString(),
                Constant.Key.FIVE_LOTTERY,
                onePrice.toString(),
                fivePrice.toString(),
                "",
                page
            )
            return
        }
        this.mActivity = mActivity
        this.boxId = boxId
        this.onePrice = onePrice
        this.fivePrice = fivePrice
        mPresenter.fiveLottery(boxId)
    }

    fun showSurpriseLotteryResult(
        mActivity: Activity,
        boxId: String,
        onePrice: Double,
        fivePrice: Double,
        boxPrice: Double,
        discount: String?,
        nums: Int,
        continuityId: String,
        page: String,
        cardNum: Int = 0
    ) {
        if (!isFast()) {
            return
        }
        if (boxId == "") {
            return
        }
        this.page = page
        this.continuityId = continuityId
        var ownCoin = SettingPreference.getUserCoinNum().toDouble()
        if (ownCoin < boxPrice) {
            if (mDialog != null) {
                if (mDialog!!.isShowing) {
                    mDialog!!.dismiss()
                }
            }

            JumpUtils.payJump(
                boxId,
                (boxPrice - ownCoin).toString(),
                Constant.Key.SURPRISE_LOTTERY,
                onePrice.toString(),
                fivePrice.toString(),
                "",
                page
            )
            return
        } else if (Constant.Switch.isOpenLogin && !Constant.Switch.isBindLogin && boxId != "-2") {
            //开启登录开关 并且未登录绑定
            JumpUtils.AfterLoginJump(
                boxId,
                (onePrice - ownCoin).toString(),
                Constant.Key.SURPRISE_LOTTERY,
                onePrice.toString(),
                fivePrice.toString(),
                "",
                page
            )
            return
        }
        this.mActivity = mActivity
        this.boxId = boxId
        this.onePrice = onePrice
        this.fivePrice = fivePrice
        mPresenter.popLottery(boxId, discount, nums)
    }

    fun showDuoChouLotteryResult(
        mActivity: Activity,
        boxId: String,
        duoChouNum: Int,
        onePrice: Double,
        fivePrice: Double,
        cardNum: Int = 0
    ) {
        page = "1"
        if (!isFast()) {
            return
        }
        if (boxId == "" || fivePrice == 0.0) {
            return
        }
        if (Constant.Switch.isOpenLogin && !Constant.Switch.isBindLogin && boxId != "-2") {
            //开启登录开关 并且未登录绑定
            JumpUtils.AfterLoginJump(
                "",
                "",
                "",
                "",
                "",
                "",
                "2"
            )
            return
        }

        mRePumpingCardNum = cardNum
        this.mActivity = mActivity
        this.boxId = boxId
        this.onePrice = onePrice
        this.fivePrice = fivePrice
        mPresenter.duoChouLottery(boxId, duoChouNum)
    }

    fun showOneLotteryResult(
        mActivity: Activity,
        boxId: String,
        onePrice: Double,
        fivePrice: Double,
        newUserPrice: String?,
        page: String?,
        cardNum: Int = 0,
        isShowFive: Int = 1
    ) {
        if (!isFast()) {
            return
        }
        if (boxId == "") {
            return
        }
        EventUtils.onEvent("one_lottery_boxid", "" + boxId)
        this.page = page
        this.isShowFive = isShowFive
        this.newUserPrice = newUserPrice
        mRePumpingCardNum = cardNum
        var ownCoin = SettingPreference.getUserCoinNum().toDouble()
        //钱数不够 去充值
        if (ownCoin < onePrice) {
            if (mDialog != null) {
                if (mDialog!!.isShowing) {
                    mDialog!!.dismiss()
                }
            }
            if (TextUtils.equals(Utils.getChannel(mActivity), "yyb")) {
                showPayDialog(
                    mActivity,
                    (onePrice - ownCoin).toString(),
                    AdCallback { _, result ->
                        if (result == 0) {
                            showOneLotteryResult(
                                mActivity,
                                boxId,
                                onePrice,
                                fivePrice,
                                newUserPrice,
                                page,
                                cardNum,
                                isShowFive
                            )
                        }
                    })
            } else {
                JumpUtils.payJump(
                    boxId,
                    (onePrice - ownCoin).toString(),
                    Constant.Key.ONE_LOTTERY,
                    onePrice.toString(),
                    fivePrice.toString(),
                    "",
                    page
                )
            }
            return
        } else if (Constant.Switch.isOpenLogin && !Constant.Switch.isBindLogin && boxId != "-2") {
            //开启登录开关 并且未登录绑定
            JumpUtils.AfterLoginJump(
                boxId,
                (onePrice - ownCoin).toString(),
                Constant.Key.ONE_LOTTERY,
                onePrice.toString(),
                fivePrice.toString(),
                "",
                page
            )
            return
        }
        this.mActivity = mActivity
        this.boxId = boxId
        this.onePrice = onePrice
        this.fivePrice = fivePrice
        mPresenter.oneLottery(boxId)
    }

    fun showTestLotteryResult(
        mActivity: Activity, boxId: String, onePrice: Double, fivePrice: Double, cardNum: Int = 0
    ) {
        if (!isFast()) {
            return
        }
        if (boxId == "" || fivePrice == 0.0) {
            return
        }
        mRePumpingCardNum = cardNum
        this.mActivity = mActivity
        this.boxId = boxId
        this.onePrice = onePrice
        this.fivePrice = fivePrice
        mPresenter.testLottery(boxId)
    }

    //盒子弹窗选择开启
    fun showSelectLotteryResult(
        mActivity: Activity,
        boxId: String,
        page: String?,
        selectNum: Int,
        selectPrice: Double,
        onePrice: Double,
        fivePrice: Double,
        cardNum: Int = 0
    ) {
        if (boxId == "" || selectNum == 0) {
            return
        }
        EventUtils.onEvent("select_lottery_boxid", "" + boxId)
        this.page = page
        mRePumpingCardNum = cardNum
        this.mActivity = mActivity
        this.boxId = boxId
        this.onePrice = onePrice
        this.fivePrice = fivePrice
        showPayDialog(
            mActivity,
            (selectPrice).toString(),
            AdCallback { _, result ->
                if (result == 0) {
                    mPresenter.selectLottery(boxId, selectNum)
                }
            })
    }

    //盒子页面选择开启
    fun showSelectLotteryActivityResult(
        mActivity: Activity,
        boxId: String,
        page: String?,
        selectNum: String,
        selectPrice: Double
    ) {
        if (boxId == "") {
            return
        }
        EventUtils.onEvent("select_lottery_activity_boxid", "" + boxId)
        this.page = page
        this.isShowFive = 0
        var ownCoin = SettingPreference.getUserCoinNum().toDouble()
        //钱数不够 去充值
        if (ownCoin < selectPrice) {
            if (mDialog != null) {
                if (mDialog!!.isShowing) {
                    mDialog!!.dismiss()
                }
            }
            if (TextUtils.equals(Utils.getChannel(mActivity), "yyb")) {
                showPayDialog(
                    mActivity,
                    (selectPrice - ownCoin).toString(),
                    AdCallback { _, result ->
                        if (result == 0) {
                            showSelectLotteryActivityResult(
                                mActivity,
                                boxId,
                                page,
                                selectNum,
                                selectPrice
                            )
                        }
                    })
            } else {
                JumpUtils.payJump(
                    boxId,
                    (selectPrice - ownCoin).toString(),
                    Constant.Key.SELECT_LOTTERY_2,
                    onePrice.toString(),
                    fivePrice.toString(),
                    selectNum,//借用newpeople字段
                    page, 0, selectPrice
                )
            }
            return
        } else if (Constant.Switch.isOpenLogin && !Constant.Switch.isBindLogin && boxId != "-2") {
            //开启登录开关 并且未登录绑定
            JumpUtils.AfterLoginJump(
                boxId,
                (selectPrice - ownCoin).toString(),
                Constant.Key.SELECT_LOTTERY_2,
                onePrice.toString(),
                fivePrice.toString(),
                selectNum,//借用newpeople字段
                page, 0, selectPrice
            )
            return
        }
        this.mActivity = mActivity
        this.boxId = boxId
        this.onePrice = onePrice
        this.fivePrice = fivePrice
        mPresenter.selectLotteryActivity(boxId, selectNum)
    }

    fun showEpicCardLotteryResult(
        mActivity: Activity,
        boxId: String,
        onePrice: Double,
        fivePrice: Double
    ) {
        EventUtils.onEvent("one_lottery_boxid", "" + boxId)
        EventUtils.onEvent("shishi_card_box_click", "" + boxId)
        if (!isFast()) {
            return
        }
        if (boxId == "") {
            return
        }
        this.page = "1"
        var ownCoin = SettingPreference.getUserCoinNum().toDouble()
        //钱数不够 去充值
        if (ownCoin < onePrice) {
            if (mDialog != null) {
                if (mDialog!!.isShowing) {
                    mDialog!!.dismiss()
                }
            }
            if (TextUtils.equals(Utils.getChannel(mActivity), "yyb")) {
                showPayDialog(
                    mActivity,
                    (onePrice - ownCoin).toString(),
                    AdCallback { _, result ->
                        if (result == 0) {
                            showEpicCardLotteryResult(
                                mActivity,
                                boxId,
                                onePrice,
                                fivePrice
                            )
                        }
                    })
            } else {
                JumpUtils.payJump(
                    boxId,
                    (onePrice - ownCoin).toString(),
                    Constant.Key.EPIC_CARD_LOTTERY,
                    onePrice.toString(),
                    fivePrice.toString(),
                    "",
                    page
                )
            }
            return
        } else if (Constant.Switch.isOpenLogin && !Constant.Switch.isBindLogin && boxId != "-2") {
            //开启登录开关 并且未登录绑定
            JumpUtils.AfterLoginJump(
                boxId,
                (onePrice - ownCoin).toString(),
                Constant.Key.ONE_LOTTERY,
                onePrice.toString(),
                fivePrice.toString(),
                "",
                page
            )
            return
        }
        this.mActivity = mActivity
        this.boxId = boxId
        this.onePrice = onePrice
        this.fivePrice = fivePrice
        mPresenter.epicCardLottery(boxId)
    }

    private var mDialog: Dialog? = null

    //显示抽取结果
    private fun showResultDialog(
        mActivity: Activity,
        type: String,
        lotteryList: List<LotteryEntity>,
        desc1: String,
        desc2: String
    ) {
        if (mDialog == null) {
            mDialog = Dialog(mActivity, R.style.FullScreenCompatDialogTheme)
        }
        val mDialogView =
            DialogUtils.showBottomDialog(
                R.layout.dialog_layout_box_result,
                mActivity,
                mDialog!!,
                0,
                Gravity.FILL
            )

        var btnClose = mDialogView.findViewById<ImageView>(R.id.id_result_close)
        var id_tv_box_index = mDialogView.findViewById<TextView>(R.id.id_tv_box_num_index)
        var id_result_list_rcl =
            mDialogView.findViewById<DiscreteScrollView>(R.id.id_box_result_list)

        var mResultAdapter = BoxResultAdapter(mActivity)
        mResultAdapter.clear()
        mResultAdapter.appendToList(lotteryList)
        id_result_list_rcl.adapter = mResultAdapter
        id_result_list_rcl.setItemTransformer(
            ScaleTransformer.Builder()
                .setMaxScale(1f)
                .setMinScale(0.8f)
                .setPivotX(Pivot.X.CENTER)
                .setPivotY(Pivot.Y.CENTER)
                .build()
        )
        id_result_list_rcl.setSlideOnFling(true)
//        id_result_list_rcl.setSlideOnFlingThreshold(1500)

        var resultDesc1 = mDialogView.findViewById<TextView>(R.id.id_result_desc)
        var resultDesc2 = mDialogView.findViewById<TextView>(R.id.id_result_desc2)
        var resultDescRePumping = mDialogView.findViewById<TextView>(R.id.id_tv_re_pumping)
        var id_ll_use_re_pump = mDialogView.findViewById<LinearLayout>(R.id.id_ll_use_re_pump)

        var mFivePrice2 =
            mDialogView.findViewById<TextView>(R.id.id_five_lottery_goods_price2)
        mFivePrice2.visibility = View.GONE
        var btnOk = mDialogView.findViewById<TextView>(R.id.id_result_ok)
        var btnOneAgain = mDialogView.findViewById<TextView>(R.id.id_result_one_again)
        var btnFiveAgain = mDialogView.findViewById<TextView>(R.id.id_result_five_again)
        if (mRePumpingCardNum > 0 && type == Constant.Key.ONE_LOTTERY) {
            id_ll_use_re_pump.visibility = View.VISIBLE
            resultDescRePumping?.paint?.flags = Paint.UNDERLINE_TEXT_FLAG
        } else if (mRePumpingCardNum > 0 && type == Constant.Key.SELECT_LOTTERY && lotteryList.size == 1) {
            id_ll_use_re_pump.visibility = View.VISIBLE
            resultDescRePumping?.paint?.flags = Paint.UNDERLINE_TEXT_FLAG
        } else {
            id_ll_use_re_pump.visibility = View.GONE

        }

        recytype = type
        var recoverPriceNum = 0.0;
        recoverprice = lotteryList[0].recoverPriceStr.toString()
        var depotIds = StringBuilder()
        for (dataBean in lotteryList) {
            if (depotIds.length > 2) {
                depotIds.append(",")
            }
            depotIds.append(dataBean.depotId)
            recoverPriceNum =
                recoverPriceNum.plus(dataBean.recoverPriceStr?.toDoubleOrNull() ?: 0.0)
        }

        if (TextUtils.equals(
                "flow_tag_xymh_427",
                SettingPreference.getFlowTag()
            ) || TextUtils.equals(
                "flow_tag_xymh_516",
                SettingPreference.getFlowTag()
            ) || TextUtils.equals("flow_tag_xymh_519", SettingPreference.getFlowTag())
        ) {
            recoverprice = recoverPriceNum.toString()
        }
        ids = depotIds.toString()
        //
        if (desc1 == "") {
            resultDesc1.visibility = View.GONE
        } else {
            resultDesc1.visibility = View.VISIBLE
            resultDesc1.text = Html.fromHtml(desc1)
        }
        //
        if (desc2 == "") {
            resultDesc2.visibility = View.GONE
        } else {
            resultDesc2.visibility = View.VISIBLE
            resultDesc2.text = Html.fromHtml(desc2)
        }
        btnOk.visibility = View.GONE
        btnOneAgain.visibility = View.GONE
        btnFiveAgain.visibility = View.GONE
        when (type) {
            Constant.Key.TEST_LOTTERY -> {
                //试用
                btnOk.visibility = View.VISIBLE
                btnOneAgain.visibility = View.VISIBLE
                btnOneAgain.text =
                    mActivity.resources.getString(R.string.box_detail_open_box_result_btn_try)
            }
            Constant.Key.ONE_LOTTERY -> {

                btnOneAgain.visibility = View.VISIBLE
                if (isShowFive == 0) {
                    btnFiveAgain.visibility = View.GONE
                } else {
                    btnFiveAgain.visibility = View.VISIBLE
                }
                btnOneAgain.text =
                    mActivity.resources.getString(R.string.box_detail_open_box_result_btn_one_again)
                EventUtils.onEvent("one_lottery_goodsID" + "" + lotteryList[0].id)
            }
            Constant.Key.TWO_LOTTERY -> {
                btnOneAgain.visibility = View.VISIBLE
                if (isShowFive == 0) {
                    btnFiveAgain.visibility = View.GONE
                } else {
                    btnFiveAgain.visibility = View.VISIBLE
                }
                btnOneAgain.text =
                    mActivity.resources.getString(R.string.box_detail_open_box_result_btn_one_again)

            }
            Constant.Key.THREE_LOTTERY -> {

                btnOneAgain.visibility = View.VISIBLE
                if (isShowFive == 0) {
                    btnFiveAgain.visibility = View.GONE
                } else {
                    btnFiveAgain.visibility = View.VISIBLE
                }
                btnOneAgain.text =
                    mActivity.resources.getString(R.string.box_detail_open_box_result_btn_one_again)
            }
            Constant.Key.FOUR_LOTTERY -> {

                btnOneAgain.visibility = View.VISIBLE
                if (isShowFive == 0) {
                    btnFiveAgain.visibility = View.GONE
                } else {
                    btnFiveAgain.visibility = View.VISIBLE
                }
                btnOneAgain.text =
                    mActivity.resources.getString(R.string.box_detail_open_box_result_btn_one_again)
            }
            Constant.Key.FIVE_LOTTERY -> {

                btnOneAgain.visibility = View.VISIBLE
                if (isShowFive == 0) {
                    btnFiveAgain.visibility = View.GONE
                } else {
                    btnFiveAgain.visibility = View.VISIBLE
                }
                btnOneAgain.text =
                    mActivity.resources.getString(R.string.box_detail_open_box_result_btn_one_again)
            }
            Constant.Key.SURPRISE_LOTTERY -> {

                btnOneAgain.visibility = View.VISIBLE
                if (isShowFive == 0) {
                    btnFiveAgain.visibility = View.GONE
                } else {
                    btnFiveAgain.visibility = View.VISIBLE
                }
                btnOneAgain.text =
                    mActivity.resources.getString(R.string.box_detail_open_box_result_btn_one_again)
                for (i in lotteryList) {
                    EventUtils.onEvent("five_lootter_GoodsId", "" + i.id)
                }
            }
            Constant.Key.SELECT_LOTTERY -> {
                var total = 0f;
                lotteryList.indices.forEach { i ->
                    val one = lotteryList[i].salePriceStr?.toFloatOrNull() ?: 0f
                    total = total.plus(one)
                }

                val df = DecimalFormat("#0.00")
                if (total > 0) {
                    mFivePrice2.text = "总价值约${df.format(total)}元"
                    mFivePrice2.visibility = View.VISIBLE
                } else {
                    mFivePrice2.text = ""
                }
                btnOneAgain.visibility = View.VISIBLE
                if (isShowFive == 0) {
                    btnFiveAgain.visibility = View.GONE
                } else {
                    btnFiveAgain.visibility = View.VISIBLE
                }
                btnOneAgain.text =
                    mActivity.resources.getString(R.string.box_detail_open_box_result_btn_one_again)
                for (i in lotteryList) {
                    EventUtils.onEvent("lottery_goodsID", "" + i.id)
                }
            }
            Constant.Key.SELECT_LOTTERY_2 -> {
                var total = 0f;
                lotteryList.indices.forEach { i ->
                    val one = lotteryList[i].salePriceStr?.toFloatOrNull() ?: 0f
                    total = total.plus(one)
                }
                val df = DecimalFormat("#0.00")
                if (total > 0) {
                    mFivePrice2.text = "总价值约${df.format(total)}元"
                    mFivePrice2.visibility = View.VISIBLE
                } else {
                    mFivePrice2.text = ""
                }
                btnOneAgain.visibility = View.VISIBLE
                btnFiveAgain.visibility = View.GONE
                btnOneAgain.text =
                    mActivity.resources.getString(R.string.box_detail_open_box_result_btn_one_again)
                for (i in lotteryList) {
                    EventUtils.onEvent("lottery_goodsID", "" + i.id)
                    EventUtils.onEvent("20box_goods_show", "" + i.id)
                }
                EventUtils.onEvent("20box_result_show")
                SettingPreference.setIsOpenLuckLottery()
            }
        }
        EventUtils.onEvent("box_result_dialog_show")


        btnOk.setOnClickListener {
            mDialog?.dismiss()
        }
        btnClose.setOnClickListener {
            mDialog?.dismiss()
        }
        //重抽
        resultDescRePumping.setOnClickListener {
            if (type == Constant.Key.ONE_LOTTERY) {
                mRePumpingCardNum--
                mPresenter.rePumpingOneLottery(boxId, depotIds.toString())
            } else if (type == Constant.Key.FIVE_LOTTERY) {
                mRePumpingCardNum--
                mPresenter.rePumpingFiveLottery(boxId, depotIds.toString())
            } else if (type == Constant.Key.SELECT_LOTTERY) {
                mRePumpingCardNum--
                mPresenter.rePumpingOneLottery(boxId, depotIds.toString())
            }
        }
        //单抽
        btnOneAgain.setOnClickListener(object : NoDoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                EventUtils.onEvent("one_more_lottery", "再来一发")
                if (Constant.Key.SELECT_LOTTERY_2 == type) {
                    mDialog?.dismiss()
                } else {

                    mDialog?.dismiss()
                    var boxType = 0;
                    if (boxId == Constant.NewUserBox.newUserBoxId) {
                        boxType = 3
                    }
                    DialogUtils.showBoxSelectDialog(
                        mActivity, onePrice, goodsName, boxType
                    ) { code, result ->
                        if (code == 0) {
                            Utils.showToast(mActivity, "请选择想要开启的盒子~")
                        } else {
                            showSelectLotteryResult(
                                mActivity,
                                boxId,
                                page,
                                code,
                                result ?: onePrice ?: 0.0 * code,
                                onePrice,
                                fivePrice
                            )
                        }
                    }
                }
            }
        })
        //五抽
        btnFiveAgain.setOnClickListener(object : NoDoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                EventUtils.onEvent("five_more_lottery", "再来五发")
                mDialog?.dismiss()
                var boxType = 0;
                if (boxId == Constant.NewUserBox.newUserBoxId) {
                    boxType = 3
                }
                DialogUtils.showBoxSelectDialog(
                    mActivity, onePrice, goodsName, boxType
                ) { code, result ->
                    if (code == 0) {
                        Utils.showToast(mActivity, "请选择想要开启的盒子~")
                    } else {
                        showSelectLotteryResult(
                            mActivity,
                            boxId,
                            page,
                            code,
                            result ?: onePrice ?: 0.0 * code,
                            onePrice,
                            fivePrice
                        )
                    }
                }
            }
        })

        mDialog?.setOnDismissListener {
            mDialog = null
            this.mActivity = null
            if (dialogDismissCallback != null) {
                EventBus.getDefault().unregister(this)
                dialogDismissCallback?.onDismiss(this.boxId)
            }
        }
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog?.setCancelable(false)
        if (!mActivity.isFinishing) {
            mDialog?.show()
            return
        }
        EventBus.getDefault().register(this)
    }

    //史诗卡抽奖结果
    private fun showEpicCardResultDialog(
        mActivity: Activity, type: String, lotteryList: List<LotteryEntity>
    ) {
        var mEpicCardDialog = Dialog(mActivity, R.style.FullScreenCompatDialogTheme)
        val mDialogView =
            DialogUtils.showBottomDialog(
                R.layout.dialog_layout_epic_card_box_result,
                mActivity,
                mEpicCardDialog!!,
                0,
                Gravity.FILL
            )
        var btnClose = mDialogView.findViewById<TextView>(R.id.id_btn_know)
        var goodsValue = mDialogView.findViewById<TextView>(R.id.id_tv_goods_value)
        var goodsImg =
            mDialogView.findViewById<RecycleImageView>(R.id.id_epic_lottery_goods_img)
        var goodsName = mDialogView.findViewById<TextView>(R.id.id_tv_epic_lottery_goods_name)
        if (!lotteryList.isNullOrEmpty()) {
            goodsImg?.setLoadImageUrl(lotteryList[0].mainImage, false)
            goodsName?.text = lotteryList[0].productName
            EventUtils.onEvent("one_lottery_goodsID" + "" + lotteryList[0].id)
            EventUtils.onEvent("shishi_card_box_result_show" + "" + lotteryList[0].id)
            if (!TextUtils.isEmpty(lotteryList[0].salePriceStr)) {
                goodsValue?.text = "总价值约" + lotteryList[0].salePriceStr + "元"
            } else {
                goodsValue?.text = ""
            }
        }

        btnClose.setOnClickListener {
            mEpicCardDialog.dismiss()
            if (dialogDismissCallback != null) {
                dialogDismissCallback?.onDismiss(this.boxId)
            }
        }
        mEpicCardDialog.setCanceledOnTouchOutside(false)
        mEpicCardDialog.setCancelable(false)
        if (!mActivity.isFinishing) {
            mEpicCardDialog.show()
            return
        }
    }

    override fun setOneLotterResult(result: OneLotteryEntity?) {
//        Log.e("wxw", "BoxId" + boxId)
        UserDataUtils.updateUserInfo(null)

        //修正免费盲盒和新人盲盒的下一个盲盒id
        if (boxId == "-2" && Constant.NewUserBox.newUserBoxId != "") {
            boxId = Constant.NewUserBox.newUserBoxId
            onePrice = Constant.NewUserBox.newUserBoxPrice.toDouble();
            fivePrice = Constant.NewUserBox.newUserBoxFivePrice.toDouble();

            goodsImg = Constant.NewUserBox.newUserBoxMainImg
            goodsName = Constant.NewUserBox.newUserBoxMainName
            goodsPrice = Constant.NewUserBox.newUserBoxPrice
            goodsNum = "1"
        } else if (boxId == Constant.NewUserBox.newUserBoxId && !Constant.NewUserBox.firstBoxId.isNullOrEmpty()) {
            boxId = Constant.NewUserBox.firstBoxId;
            onePrice = Constant.NewUserBox.firstBoxPrice.toDouble();
            fivePrice = onePrice.times(5)
//            Constant.NewUserBox.firstBoxId = ""

            goodsImg = Constant.NewUserBox.firstBoxMainImg
            goodsName = Constant.NewUserBox.firstBoxMainName
            goodsPrice = Constant.NewUserBox.firstBoxPrice
            goodsNum = "1"
            val lotteryEvent = LotteryEvent()
            lotteryEvent.newPeople = "0"
            EventBus.getDefault().post(lotteryEvent)
        } else if (boxId == Constant.NewUserBox.firstBoxId ?: "") {
            Constant.NewUserBox.firstBoxId = ""
        } else if (boxId == Constant.NewUserBox.openDisBoxId) {
            boxId = Constant.NewUserBox.tempBoxId;
            onePrice = Constant.NewUserBox.tempBoxPrice.toDouble();
            fivePrice = onePrice.times(5)

            goodsImg = Constant.NewUserBox.tempBoxMainImg
            goodsName = Constant.NewUserBox.tempBoxMainName
            goodsPrice = Constant.NewUserBox.tempBoxPrice
            goodsNum = "1"
            //刷新首页列表
            val lotteryEvent = LotteryEvent()
            lotteryEvent.newPeople = "0"
            EventBus.getDefault().post(lotteryEvent)
        }

        checkRePumCardNum(boxId, result?.card) { _, _ ->
            if (mActivity != null && result?.goods != null) {
                showResultDialog(
                    mActivity!!,
                    Constant.Key.ONE_LOTTERY,
                    listOf(result.goods!!),
                    result.text,
                    result.card?.text ?: ""
                )
            }
        }
    }

    override fun setTestLotterResult(result: LotteryEntity?) {
        if (mActivity != null && result != null) {
            showResultDialog(
                mActivity!!,
                Constant.Key.TEST_LOTTERY,
                listOf(result),
                "",
                mActivity?.resources!!.getString(R.string.box_detail_open_box_result_try_play_desc)
            )
        }
    }

    override fun setFiveLotterResult(result: FiveLotteryEntiy?) {
        UserDataUtils.updateUserInfo(null)
        if (mActivity != null && result?.goods != null) {
            showResultDialog(
                mActivity!!,
                Constant.Key.FIVE_LOTTERY,
                result.goods!!,
                result.text,
                ""
            )
        }
    }

    override fun setEpicCardLotteryResult(result: FiveLotteryEntiy?) {
        UserDataUtils.updateUserInfo(null)

        if (mActivity != null && result?.goods != null) {
            showEpicCardResultDialog(
                mActivity!!,
                Constant.Key.EPIC_CARD_LOTTERY,
                result.goods!!
            )
        }
    }

    override fun setDuoChouResult(result: FiveLotteryEntiy?) {
        UserDataUtils.updateUserInfo(null)
        if (mActivity != null && result?.goods != null) {
            var lotteryType = Constant.Key.TWO_LOTTERY
            if (result.goods?.size == 2) {
                lotteryType = Constant.Key.TWO_LOTTERY
            } else if (result.goods?.size == 3) {
                lotteryType = Constant.Key.THREE_LOTTERY
            } else {
                lotteryType = Constant.Key.FOUR_LOTTERY
            }
            showResultDialog(
                mActivity!!,
                lotteryType,
                result.goods!!,
                result.text,
                ""
            )
        }
    }

    override fun setPopLotteryResult(result: FiveLotteryEntiy?) {
        EventUtils.onEvent("Show_tianjiang_reslut")
        val lotteryEvent = LotteryEvent()
        lotteryEvent.newPeople = "0"
        EventBus.getDefault().post(lotteryEvent)
        UserDataUtils.updateUserInfo(null)
        this.boxId = continuityId
        if (mActivity != null && result?.goods != null) {
            showResultDialog(
                mActivity!!,
                Constant.Key.SURPRISE_LOTTERY,
                result.goods!!,
                result.text,
                result.card?.text ?: ""
            )
        }
    }

    override fun setPropsCardList(result: PropsCardList?, cardEntity: PropsCardEntity?) {
        initPropsCard(cardEntity)

    }

    override fun setSelectLotteryResult(result: FiveLotteryEntiy?) {
        UserDataUtils.updateUserInfo(null)

        //修正免费盲盒和新人盲盒的下一个盲盒id
        if (boxId == "-2" && Constant.NewUserBox.newUserBoxId != "") {
            boxId = Constant.NewUserBox.newUserBoxId
            onePrice = Constant.NewUserBox.newUserBoxPrice.toDouble();
            fivePrice = Constant.NewUserBox.newUserBoxFivePrice.toDouble();

            goodsImg = Constant.NewUserBox.newUserBoxMainImg
            goodsName = Constant.NewUserBox.newUserBoxMainName
            goodsPrice = Constant.NewUserBox.newUserBoxPrice
            goodsNum = "1"
        } else if (boxId == Constant.NewUserBox.newUserBoxId && !Constant.NewUserBox.firstBoxId.isNullOrEmpty()) {
            boxId = Constant.NewUserBox.firstBoxId;
            onePrice = Constant.NewUserBox.firstBoxPrice.toDouble();
            fivePrice = onePrice.times(5)

            goodsImg = Constant.NewUserBox.firstBoxMainImg
            goodsName = Constant.NewUserBox.firstBoxMainName
            goodsPrice = Constant.NewUserBox.firstBoxPrice
            goodsNum = "1"
            val lotteryEvent = LotteryEvent()
            lotteryEvent.newPeople = "0"
            EventBus.getDefault().post(lotteryEvent)
        } else if (boxId == Constant.NewUserBox.firstBoxId ?: "") {
            Constant.NewUserBox.firstBoxId = ""
        } else if (boxId == Constant.NewUserBox.openDisBoxId) {
            boxId = Constant.NewUserBox.tempBoxId;
            onePrice = Constant.NewUserBox.tempBoxPrice.toDouble();
            fivePrice = onePrice.times(5)

            goodsImg = Constant.NewUserBox.tempBoxMainImg
            goodsName = Constant.NewUserBox.tempBoxMainName
            goodsPrice = Constant.NewUserBox.tempBoxPrice
            goodsNum = "1"
            //刷新首页列表
            val lotteryEvent = LotteryEvent()
            lotteryEvent.newPeople = "0"
            EventBus.getDefault().post(lotteryEvent)
        }
        rewardCoinNum = result?.presentLuckyCoin ?: ""
        checkRePumCardNum(boxId, result?.card) { _, _ ->
            if (mActivity != null && result?.goods != null) {
                showResultDialog(
                    mActivity!!,
                    Constant.Key.SELECT_LOTTERY,
                    result.goods!!,
                    result.text,
                    result.card?.text ?: ""
                )
            }
        }
    }

    override fun setSelectLotteryActivityResult(result: FiveLotteryEntiy?) {
        if (mActivity != null && result?.res != null) {
            if (mActivity != null && result?.res != null) {
                showResultDialog(
                    mActivity!!,
                    Constant.Key.SELECT_LOTTERY_2,
                    result.res!!,
                    result.text,
                    result.card?.text ?: ""
                )
            }
        }
    }

    var lastActionTime: Long = 0
    fun isFast(): Boolean {
        var isRun = false
        val curTime = System.currentTimeMillis()
        if ((curTime - lastActionTime) >= 1000) {
            isRun = true
        }
        lastActionTime = curTime
        return isRun
    }

    //道具卡
    private fun initPropsCard(cardEntity: PropsCardEntity?) {
        if (cardEntity == null) {
            return
        }
//        mRePumpingCardNum = result.rePumping?.size ?: 0
        //重抽卡
        if (cardEntity?.rePumping != null) {
            DialogUtils.showPropsCard(
                mActivity,
                1,
                1,
                1,
                mActivity?.resources!!.getString(R.string.box_detail_open_box_result_re_pumping_desc3),
                mActivity?.resources!!.getString(R.string.box_detail_open_box_result_re_pumping_desc4),
                {
                    //left
                }, {
                    //right
                })
        } else if (cardEntity?.epicCard != null) {
            DialogUtils.showPropsCard(
                mActivity,
                4,
                1,
                3,
                "恭喜您获得史诗卡，下次抽盒\n" +
                        "即可使用",
                "使用史诗卡开盒,即可获得史诗商品",
                {
                    //left
                }, {
                    //right
                })
        }

    }

    override fun onClosePopupWindow() {
        mDialog?.dismiss()
    }

    //支付弹窗
    fun showPayDialog(
        activity: Activity,
        payPrice: String,
        callBack: AdCallback<Int>
    ) {
        PayDialog.showPayDialog(
            activity,
            goodsImg,
            goodsName,
            goodsPrice,
            payPrice,
            goodsNum,
            callBack
        )
    }

    //检查重抽卡的数量
    private fun checkRePumCardNum(
        box_id: String,
        result: PropsCardEntity?,
        callBack: AdCallback<Int>
    ) {
        ApiClient.homeApi.getPropsCardList(box_id)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<PropsCardList?>() {
                override fun onSuccess(propResult: PropsCardList?) {
                    if (propResult != null) {
                        mRePumpingCardNum = propResult.rePumping?.size ?: mRePumpingCardNum
                    }
                    if (result?.rePumping != null && mRePumpingCardNum <= 1) {
                        mRePumpingCardNum = 0;
                    }
                    callBack.onResult(0, mRePumpingCardNum)
                    initPropsCard(result)
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    callBack.onResult(-1, mRePumpingCardNum)
                }
            })
    }
}