package com.zxhl.main.page.view

import android.app.Activity
import android.app.Dialog
import android.app.SearchManager
import android.graphics.Color
import android.text.Html
import android.text.TextUtils
import android.util.Log
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.zxhl.cms.common.Constant
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.model.box.*
import com.zxhl.cms.utils.EventUtils
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.UserDataUtils
import com.zxhl.cms.widget.LoadingDialog
import com.zxhl.main.R
import com.zxhl.main.page.contract.LotteryContract
import com.zxhl.main.page.presenter.LotteryPresenter
import kotlinx.android.synthetic.main.activity_layout_lottery_reslut.*

/**
 * @author (wangXuewei)
 * @datetime 2022-03-04 14:08 GMT+8
 * @detail :
 */
object BoxResultDialog : LotteryContract.View {
    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 mRePumpingCardNum = 0//重抽卡数量
    private var dialogDismissCallback: DialogDismissCallback? = null
    fun setDialogDismissCallback(callback: DialogDismissCallback?) {
        this.dialogDismissCallback = callback
    }

    interface DialogDismissCallback {
        fun onDismiss()
    }

    fun showFiveLotteryResult(
        mActivity: Activity,
        boxId: String,
        onePrice: Double,
        fivePrice: Double,
        cardNum: Int = 0
    ) {
        if (!isFast()) {
            return
        }
        if (boxId == "" || fivePrice == 0.0) {
            return
        }
        mRePumpingCardNum = cardNum
        var ownCoin = SettingPreference.getUserCoinNum().toDouble()
        if (ownCoin < fivePrice) {
            if (mDialog != null) {
                if (mDialog!!.isShowing) {
                    mDialog!!.dismiss()
                }
            }
            JumpUtils.payJump(boxId, (fivePrice - ownCoin).toString(), Constant.Key.FIVE_LOTTERY)
            return
        }
        this.mActivity = mActivity
        this.boxId = boxId
        this.onePrice = onePrice
        this.fivePrice = fivePrice
        mPresenter.fiveLottery(boxId)
    }

    fun showOneLotteryResult(
        mActivity: Activity, boxId: String, onePrice: Double, fivePrice: Double, cardNum: Int = 0
    ) {
        if (!isFast()) {
            return
        }
        if (boxId == "" || onePrice == 0.0) {
            return
        }
        mRePumpingCardNum = cardNum
        var ownCoin = SettingPreference.getUserCoinNum().toDouble()
        //钱数不够 去充值
        if (ownCoin < onePrice) {
            if (mDialog != null) {
                if (mDialog!!.isShowing) {
                    mDialog!!.dismiss()
                }
            }
            JumpUtils.payJump(boxId, (onePrice - ownCoin).toString(), Constant.Key.FIVE_LOTTERY)
            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)
    }

    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.CenterCompatDialogTheme)
        }
        val mDialogView =
            DialogUtils.showBottomDialog(
                R.layout.dialog_layout_box_result,
                mActivity,
                mDialog!!,
                0,
                Gravity.CENTER
            )
        var btnClose = mDialogView.findViewById<ImageView>(R.id.id_result_close)
        var oneGoodsImg = mDialogView.findViewById<ImageView>(R.id.id_result_one_goods)
        var goodsImg1 = mDialogView.findViewById<ImageView>(R.id.id_result_list_goods_1)
        var goodsImg2 = mDialogView.findViewById<ImageView>(R.id.id_result_list_goods_2)
        var goodsImg3 = mDialogView.findViewById<ImageView>(R.id.id_result_list_goods_3)
        var goodsImg4 = mDialogView.findViewById<ImageView>(R.id.id_result_list_goods_4)
        var goodsImg5 = mDialogView.findViewById<ImageView>(R.id.id_result_list_goods_5)
        var fiveGoodsLine1 = mDialogView.findViewById<LinearLayout>(R.id.id_result_five_goods_line1)
        var fiveGoodsLine2 = mDialogView.findViewById<LinearLayout>(R.id.id_result_five_goods_line2)
        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_result_desc3_re_pumping)
        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.TEST_LOTTERY) {
            resultDescRePumping.visibility = View.VISIBLE
        } else {
            resultDescRePumping.visibility = View.GONE
        }
        var depotIds = StringBuilder()
        for (dataBean in lotteryList) {
            if (depotIds.length > 2) {
                depotIds.append(",")
            }
            depotIds.append(dataBean.depotId)
        }
        //
        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)
        }
        oneGoodsImg.visibility = View.GONE
        fiveGoodsLine1.visibility = View.GONE
        fiveGoodsLine2.visibility = View.GONE
        btnOk.visibility = View.GONE
        btnOneAgain.visibility = View.GONE
        btnFiveAgain.visibility = View.GONE
        when (type) {
            Constant.Key.TEST_LOTTERY -> {
                //试用
                oneGoodsImg.visibility = View.VISIBLE
                Glide.with(mActivity).load(lotteryList[0].mainImage).into(oneGoodsImg)
                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 -> {
                //单抽
                oneGoodsImg.visibility = View.VISIBLE
                Glide.with(mActivity).load(lotteryList[0].mainImage).into(oneGoodsImg)
                btnOneAgain.visibility = View.VISIBLE
                btnFiveAgain.visibility = View.VISIBLE
                btnOneAgain.text =
                    mActivity.resources.getString(R.string.box_detail_open_box_result_btn_one_again)

            }
            Constant.Key.FIVE_LOTTERY -> {
                //五抽
                fiveGoodsLine1.visibility = View.VISIBLE
                fiveGoodsLine2.visibility = View.VISIBLE
                if (lotteryList.size >= 5) {
                    Glide.with(mActivity).load(lotteryList[0].mainImage).into(goodsImg1)
                    Glide.with(mActivity).load(lotteryList[1].mainImage).into(goodsImg2)
                    Glide.with(mActivity).load(lotteryList[2].mainImage).into(goodsImg3)
                    Glide.with(mActivity).load(lotteryList[3].mainImage).into(goodsImg4)
                    Glide.with(mActivity).load(lotteryList[4].mainImage).into(goodsImg5)
                }
                btnOneAgain.visibility = View.VISIBLE
                btnFiveAgain.visibility = View.VISIBLE
                btnOneAgain.text =
                    mActivity.resources.getString(R.string.box_detail_open_box_result_btn_one_again)
            }
        }

        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())
            }
        }
        //单抽
        btnOneAgain.setOnClickListener {
            showOneLotteryResult(mActivity, boxId, onePrice, fivePrice, mRePumpingCardNum)
        }
        //五抽
        btnFiveAgain.setOnClickListener {
            showFiveLotteryResult(mActivity, boxId, onePrice, fivePrice, mRePumpingCardNum)
        }
        mDialog?.setOnDismissListener {
            mDialog = null
            this.mActivity = null
            if (dialogDismissCallback != null) {
                dialogDismissCallback?.onDismiss()
            }
        }
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog?.setCancelable(false)
        if (!mActivity.isFinishing) {
            mDialog?.show()
            return
        }

    }

    override fun setOneLotterResult(result: OneLotteryEntity?) {
        UserDataUtils.updateUserInfo(null)
        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 setPropsCardList(result: PropsCardList?, cardEntity: PropsCardEntity?) {
        if (result != null && result.show) {
            initPropsCard(result, cardEntity)
        }
    }

    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(result: PropsCardList, cardEntity: PropsCardEntity?) {
        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
                })
        }

    }


}