package com.zxbw.modulemain.view


import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.zxbw.modulemain.R
import com.zxbw.modulemain.adapter.HuafeiCouponsAdapter
import com.zxbw.modulemain.box.adapter.SelectBoxAdapter
import com.zxhl.cms.AppContext
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.model.qy.CouponsEntity
import com.zxhl.cms.utils.AdCallback
import com.zxhl.cms.utils.EventUtils
import com.zxhl.cms.utils.PhoneUtils
import com.zxhl.cms.utils.Utils


object DialogUtils {
    fun showBottomDialog(
        layout: Int,
        activity: Context,
        setHeadDialog: Dialog,
        animation: Int,
        gravity: Int
    ): View {

        val mDialogView = View.inflate(activity, layout, null)
        setHeadDialog.setContentView(mDialogView)
        val window = setHeadDialog.window
        window?.setGravity(gravity)
        val lp = window?.attributes
        window?.decorView?.setPadding(0, 0, 0, 0)
        lp?.width = PhoneUtils.getScreenWidth(AppContext.get())
        if (gravity == Gravity.TOP) {
            lp?.height = PhoneUtils.getScreenHeight(AppContext.get())
        }
        window?.attributes = lp
        if (animation > 0)
            window?.setWindowAnimations(animation)//设置动画
        return mDialogView
    }

    /**
     * 1重抽卡:重新抽取当前盲盒,每次抽取最多只能使用一次重抽卡
    ·触发条件:每抽4次可获得

    2暴击卡:盲盒抽取获取商品后,使用该卡片可再获得该盲盒下的随机一商品
    ·触发条件:每抽5次可获得

    3回血卡:提升回收商品获取的幸运币数额,默认70%,使用后可增加10%-20%
    ·触发条件:每抽6次可获得

    4史诗卡:抽取史诗商品
    ·触发条件:每抽6次可获得
     */
    //道具卡
    fun showPropsCard(
        context: Activity?,
        cardType: Int,
        cardNum: Int,
        useStatus: Int,
        desc1: String,
        desc2: String, leftClick: View.OnClickListener, rightClick: View.OnClickListener
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_props_card,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        var dialog_bg = mDialogView.findViewById<LinearLayout>(R.id.id_props_card_bg)
        var desc1_tv1 = mDialogView.findViewById<TextView>(R.id.id_props_card_desc1)
        var desc1_tv2 = mDialogView.findViewById<TextView>(R.id.id_props_card_desc2)
        var leftBtn = mDialogView.findViewById<TextView>(R.id.btn_left_tv)
        var rightBtn = mDialogView.findViewById<TextView>(R.id.btn_right_tv)
        var tips = mDialogView.findViewById<TextView>(R.id.id_dialog_tips)
        desc1_tv1.text = desc1;
        desc1_tv2.text = desc2;

        //-1 隐藏右边按钮 0 不可使用 1刚刚获取 2 可以使用 4 可以使用(2) 3 刚刚获取(2)
        if (useStatus == -1) {
            leftBtn.text = context.resources.getString(R.string.box_detail_open_box_result_btn_konw)
            leftBtn.setTextColor(Color.parseColor("#4C84FF"))
            leftBtn.background = ContextCompat.getDrawable(
                context,
                R.drawable.shape_4c84ff_r25_stroke
            )
            rightBtn.text =
                context.resources.getString(R.string.box_detail_open_box_result_btn_one_again)
            rightBtn.visibility = View.GONE

            tips.visibility = View.GONE
        } else if (useStatus == 0) {
            leftBtn.text = context.resources.getString(R.string.box_detail_open_box_result_btn_konw)
            leftBtn.setTextColor(Color.parseColor("#4C84FF"))
            leftBtn.background = ContextCompat.getDrawable(
                context,
                R.drawable.shape_4c84ff_r25_stroke
            )
            rightBtn.text =
                context.resources.getString(R.string.box_detail_open_box_result_btn_one_again)
            rightBtn.visibility = View.VISIBLE
            tips.visibility = View.GONE
        } else if (useStatus == 1) {
            leftBtn.text =
                context.resources.getString(R.string.box_detail_open_box_result_btn_receive)
            leftBtn.setTextColor(Color.parseColor("#4C84FF"))
            leftBtn.background = ContextCompat.getDrawable(
                context,
                R.drawable.shape_4c84ff_r25_stroke
            )
            rightBtn.text =
                context.resources.getString(R.string.box_detail_open_box_result_btn_one_again)
            rightBtn.visibility = View.VISIBLE
            tips.visibility = View.GONE
        } else if (useStatus == 2) {
            leftBtn.text = "下次使用"
            leftBtn.setTextColor(Color.parseColor("#FF2E31"))
            leftBtn.background = ContextCompat.getDrawable(
                context,
                R.drawable.shape_ff2e31_r25_stoke
            )
            rightBtn.text = "立即使用"
            rightBtn.visibility = View.VISIBLE
//            tips.visibility = View.VISIBLE
        } else if (useStatus == 3) {
            leftBtn.text =
                context.resources.getString(R.string.box_detail_open_box_result_btn_receive)
            leftBtn.setTextColor(Color.parseColor("#4C84FF"))
            leftBtn.background = ContextCompat.getDrawable(
                context,
                R.drawable.shape_4c84ff_r25_stroke
            )
            rightBtn.text =
                context.resources.getString(R.string.box_detail_open_box_result_btn_one_again)
            rightBtn.visibility = View.GONE
            tips.visibility = View.GONE
        } else {
            leftBtn.text = "再想想～"
            leftBtn.setTextColor(Color.parseColor("#4C84FF"))
            leftBtn.background = ContextCompat.getDrawable(
                context,
                R.drawable.shape_4c84ff_r25_stroke
            )
            rightBtn.text = "一发入魂"
            rightBtn.visibility = View.VISIBLE
//            tips.visibility = View.VISIBLE
        }

        if (cardNum > 0) {
            when (cardType) {
                1 -> {
                    dialog_bg.background = ContextCompat.getDrawable(
                        context,
                        R.drawable.props_card_bj_dialog_chongchou
                    )
                }
//                2 -> {
//                    dialog_bg.background = ContextCompat.getDrawable(
//                        context,
//                        R.drawable.props_card_bj_dialog_baoji
//                    )
//                }
//                3 -> {
//                    dialog_bg.background = ContextCompat.getDrawable(
//                        context,
//                        R.drawable.props_card_bj_dialog_huixue
//                    )
//                }
                4 -> {
                    dialog_bg.background = ContextCompat.getDrawable(
                        context,
                        R.drawable.props_card_bj_dialog_shishi
                    )
                }
            }
        } else {
            when (cardType) {
                1 -> {
                    dialog_bg.background = ContextCompat.getDrawable(
                        context,
                        R.drawable.props_card_bj_dialog_chongchou_weihuode
                    )
                }
//                2 -> {
//                    dialog_bg.background = ContextCompat.getDrawable(
//                        context,
//                        R.drawable.props_card_bj_dialog_baoji_weihuode
//                    )
//                }
//                3 -> {
//                    dialog_bg.background = ContextCompat.getDrawable(
//                        context,
//                        R.drawable.props_card_bj_dialog_huixue_weihuode
//                    )
//                }
                4 -> {
                    dialog_bg.background = ContextCompat.getDrawable(
                        context,
                        R.drawable.props_card_bj_dialog_shishi_weihuode
                    )
                }
            }
        }
        when (cardType) {
            1 -> {
                EventUtils.onEvent("chongchou_show")
            }
            2 -> {
                EventUtils.onEvent("baoji_card_show")
            }
            3 -> {
                EventUtils.onEvent("huixue_card_show")
            }
            4 -> {
                EventUtils.onEvent("shishi_card_show")
            }
        }
        leftBtn.setOnClickListener {
            mDialog.dismiss()
            leftClick.onClick(it)
        }
        rightBtn.setOnClickListener {
            mDialog.dismiss()
            rightClick.onClick(it)
        }
        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(false)
        mDialog.show()
    }

    //商品详情弹窗
    fun showGoodsDetailDialog(context: Activity, goodsImgList: List<String>) {
        if (context.isFinishing) {
            return
        }
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_goods_dtail,
                context,
                mDialog,
                0,
                Gravity.BOTTOM
            )

        mDialogView.findViewById<View>(R.id.id_img_close_btn).setOnClickListener {
            mDialog.dismiss()
        }

        var imgListView = mDialogView.findViewById<LinearLayout>(R.id.id_dialog_goods_img_ll)
        imgListView.removeAllViews()
        for (goodsImg in goodsImgList) {
            if (!context.isFinishing) {
                var detailImageView = ImageView(context)
                detailImageView.scaleType = ImageView.ScaleType.FIT_XY
                detailImageView.adjustViewBounds = true
                Glide.with(context).load(goodsImg).into(detailImageView)
                imgListView.addView(detailImageView)
            }
        }


        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(true)
        mDialog.show()
    }

    //抽盒列表弹窗
    fun showBoxSelectDialog(
        context: Activity,
        oneBoxPrice: Double,
        boxName: String,
        boxType: Int,
        callBack: AdCallback<Double>
    ) {
        if (context.isFinishing) {
            return
        }
        if (oneBoxPrice == 0.0) {
            return
        }
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_box_select,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )

        val boxListPriceTv = mDialogView.findViewById<TextView>(R.id.id_tv_select_box_list_price)
        val boxNameTV = mDialogView.findViewById<TextView>(R.id.id_tv_select_box_name)
        boxNameTV.text = boxName
        var mBoxPriceConfig = SettingPreference.getConfig()
        var selectNum = 0;
        var boxListPrice = 0.0;
        var mSelectBoxList = mDialogView.findViewById<RecyclerView>(R.id.id_rcl_select_box_list)
        var mSelectAdapter = SelectBoxAdapter(
            context
        ) { view, position, data ->
            if (data) {
                selectNum++;
            } else {
                selectNum--;
            }
            if (selectNum < 0) {
                selectNum = 0;
            }
            //0<=抽三个盒子 9.7折；<=抽五个盒子 9.5折<=抽10个盒子 9折；<=抽16个盒子 8.5折；
            if (selectNum == 0) {
                boxListPrice = 0.0;
            } else if (selectNum <= 1) {
                boxListPrice = oneBoxPrice;
            } else {
                if (mBoxPriceConfig == null || mBoxPriceConfig.box_price_config.isNullOrEmpty()) {
                    boxListPrice = Utils.mul(
                        oneBoxPrice,
                        1.0,
                        selectNum
                    )
                } else {
                    for (i in 0 until mBoxPriceConfig.box_price_config?.size!!) {
                        if (selectNum <= mBoxPriceConfig.box_price_config!![i].boxNum) {
                            boxListPrice = Utils.mul(
                                oneBoxPrice,
                                mBoxPriceConfig.box_price_config!![i].boxDiscount,
                                selectNum
                            )
                            break
                        }
                    }
                }
            }
            boxListPriceTv.text = "${boxListPrice}/${selectNum}"
        }
        mSelectBoxList.layoutManager = GridLayoutManager(context, 4)
        mSelectBoxList.adapter = mSelectAdapter
        if (boxType == 0 || boxType == 4) {
            mSelectAdapter.setMaxSelect(-1)
        } else {
            mSelectAdapter.setMaxSelect(1)
        }
        //换一组
        mDialogView.findViewById<TextView>(R.id.id_tv_select_box_reset_btn).setOnClickListener {
            mSelectAdapter.resetBoxList()
            boxListPrice = 0.0;
            selectNum = 0;
            boxListPriceTv.text = "${boxListPrice}/${selectNum}"
        }

        //购买
        mDialogView.findViewById<LinearLayout>(R.id.id_select_box_buy_btn).setOnClickListener {
            callBack.onResult(selectNum, boxListPrice)
            if (selectNum > 0) {
                mDialog.dismiss()
            }
        }
        mDialogView.findViewById<View>(R.id.id_img_select_box_close_btn).setOnClickListener {
            mDialog.dismiss()
        }
        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(true)
        mDialog.show()
    }

    //显示充话费提示弹窗
    fun showRechargeTips(
        context: Activity?,
        phone: String,
        makeSureListener: View.OnClickListener
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_recharge_tips,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        mDialogView.findViewById<TextView>(R.id.id_tv_phone).text = "本次充值号码为：${phone}"
        mDialogView.findViewById<TextView>(R.id.id_btn_ok).setOnClickListener {
            mDialog.dismiss()
            makeSureListener.onClick(it)
        }
        mDialog.setCanceledOnTouchOutside(true)
        mDialog.setCancelable(true)
        mDialog.show()
    }


    //显示删除用户提示弹窗
    fun showDeleteUserIdTips(context: Activity?, deleteListener: View.OnClickListener) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_delete_id_tips,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        mDialogView.findViewById<TextView>(R.id.id_btn_cancel).setOnClickListener {
            mDialog.dismiss()
        }
        mDialogView.findViewById<TextView>(R.id.id_btn_ok).setOnClickListener {
            mDialog.dismiss()
            deleteListener.onClick(it)
        }
        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(false)
        mDialog.show()
    }

    //退出挽留弹窗
    fun showExitSkillBuy(context: Activity?, listener: View.OnClickListener) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_exit_skill_buy,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        mDialogView.findViewById<ImageView>(R.id.id_img_ok).setOnClickListener {
            listener.onClick(it)
            mDialog.dismiss()
        }
        mDialogView.findViewById<ImageView>(R.id.id_img_cance).setOnClickListener {
            mDialog.dismiss()

        }
        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(false)
        mDialog.show()
    }

    //失败提示弹窗
    fun showLootAll(context: Activity?, listener: View.OnClickListener) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_loot_all,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        mDialogView.findViewById<ImageView>(R.id.id_img_i_know).setOnClickListener {
            listener.onClick(it)
            mDialog.dismiss()
        }

        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(false)
        mDialog.show()
    }

    //话费提示
    fun showHuafeiTips(context: Activity?) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_huafei_youhui_tishi,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )

        SettingPreference.setShowUseDiscount("1")
        mDialog.setCanceledOnTouchOutside(true)
        mDialog.setCancelable(true)
        mDialog.show()
    }

    interface OnFetchCardListener {
        fun onSelectCoupons(coupons: CouponsEntity?);
    }

    //话费优惠券列表
    fun showFetchAllCard(
        context: Activity,
        originalPrice: String,
        defaultCoupon: CouponsEntity?,
        result: List<CouponsEntity>?,
        listener: OnFetchCardListener
    ) {
        if (result.isNullOrEmpty()) {
            return
        }
        if (context.isDestroyed) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_fetch_all_card,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )

        val btnBg = mDialogView.findViewById<RelativeLayout>(R.id.id_rl_bottom_btn_view);
        val btnTv = mDialogView.findViewById<TextView>(R.id.id_tv_bottom_btn);


        var selectCoupon: CouponsEntity? = null
        val youhuiList = mDialogView.findViewById<RecyclerView>(R.id.id_rcl_huafei_list);
        val huafeiCouponsAdapter =
            HuafeiCouponsAdapter(
                context
            ) { view, position, data ->
                selectCoupon = data
                if (selectCoupon == null) {
                    btnBg.setBackgroundResource(R.drawable.icon_huafei_bottom_bg_unselect)
                    btnTv.setTextColor(Color.parseColor("#999999"))
                    btnTv.text = "暂不使用"
                } else {
                    btnBg.setBackgroundResource(R.drawable.icon_huafei_bottom_bg)
                    btnTv.setTextColor(Color.parseColor("#F8E8CC"))
                    btnTv.text = "立即支付"
                }
            }
        if (defaultCoupon == null) {
            huafeiCouponsAdapter.setSelectPos(-1, originalPrice)
            btnBg.setBackgroundResource(R.drawable.icon_huafei_bottom_bg_unselect)
            btnTv.setTextColor(Color.parseColor("#999999"))
            btnTv.text = "暂不使用"
        } else {
            for (i in result.indices) {
                if (defaultCoupon.id == result[i].id) {
                    huafeiCouponsAdapter.setSelectPos(i, originalPrice)
                    btnBg.setBackgroundResource(R.drawable.icon_huafei_bottom_bg)
                    btnTv.setTextColor(Color.parseColor("#F8E8CC"))
                    btnTv.text = "立即支付"
                    break
                }
            }
        }
        youhuiList.layoutManager = GridLayoutManager(context, 2)
        youhuiList.adapter = huafeiCouponsAdapter
        huafeiCouponsAdapter.appendToList(result)
        mDialogView.findViewById<ImageView>(R.id.id_img_close_dialog).setOnClickListener {
            mDialog.dismiss()
        }
        mDialogView.findViewById<TextView>(R.id.id_tv_bottom_btn).setOnClickListener {
            mDialog.dismiss()
            listener.onSelectCoupons(selectCoupon)
        }

        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(false)
        mDialog.show()
    }

    //话费券提示
    fun showifshowCoup(
        context: Activity,
        result: List<CouponsEntity>?,
        listener: View.OnClickListener
    ) {
        if (context.isDestroyed) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_if_hava_coups,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        var fiveNums: Int = 0
        var tenNUms: Int = 0
        for (i in result!!) {
            if (i.amount.equals("5.0")) {
                fiveNums++
            }
            if (i.amount.equals("10.0")) {
                tenNUms++
            }
        }
        val tenTvnums = mDialogView.findViewById<TextView>(R.id.id_tv_ten_nums)
        val fiveTvnums = mDialogView.findViewById<TextView>(R.id.id_five_ten_nums)
        tenTvnums.text = "x${tenNUms}"
        fiveTvnums.text = "x${fiveNums}"
        mDialogView.findViewById<ImageView>(R.id.id_img_close_dialog).setOnClickListener {
            mDialog.dismiss()
        }
        mDialogView.findViewById<RelativeLayout>(R.id.id_rl_have_coup).setOnClickListener {
            mDialog.dismiss()
            listener.onClick(it)
        }

        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(false)
        mDialog.show()
        SettingPreference.setShowCoups("1")
    }

    //提现弹窗
    fun showWithDraw(
        context: Activity,
        listener: View.OnClickListener
    ) {
        if (context.isDestroyed) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_with_draw,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )

        val id_img_one_front = mDialogView.findViewById<ImageView>(R.id.id_img_one_front)
        val id_img_one_no_have = mDialogView.findViewById<ImageView>(R.id.id_img_one_no_have)
        val id_ll_one_back = mDialogView.findViewById<LinearLayout>(R.id.id_ll_one_back)
        val id_img_two_front = mDialogView.findViewById<ImageView>(R.id.id_img_two_front)
        val id_img_two_no_have = mDialogView.findViewById<ImageView>(R.id.id_img_two_no_have)
        val id_ll_two_back = mDialogView.findViewById<LinearLayout>(R.id.id_ll_two_back)
        val id_img_three_front = mDialogView.findViewById<ImageView>(R.id.id_img_three_front)
        val id_img_three_no_have = mDialogView.findViewById<ImageView>(R.id.id_img_three_no_have)
        val id_ll_three_back = mDialogView.findViewById<LinearLayout>(R.id.id_ll_three_back)
        val id_img_four_front = mDialogView.findViewById<ImageView>(R.id.id_img_four_front)
        val id_img_four_no_have = mDialogView.findViewById<ImageView>(R.id.id_img_four_no_have)
        val id_ll_four_back = mDialogView.findViewById<LinearLayout>(R.id.id_ll_four_back)
        mDialogView.findViewById<ImageView>(R.id.id_img_close_dialog).setOnClickListener {
            mDialog.dismiss()
        }
        id_img_one_front?.setOnClickListener {
            flipAnimation(id_img_one_front, id_ll_one_back)
        }
        id_img_two_front?.setOnClickListener {
            flipAnimation(id_img_two_front, id_ll_two_back)
        }
        id_img_three_front?.setOnClickListener {
            flipAnimation(id_img_three_front, id_ll_three_back)
        }
        id_img_four_front?.setOnClickListener {
            flipAnimation(id_img_four_front, id_ll_four_back)
        }
        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(false)
        mDialog.show()
    }

    fun flipAnimation(imageView: ImageView, layout: LinearLayout) {
//        if (imageView.getVisibility() == View.GONE) {
//            visibletext = textview2;
//            invisibletext = textview1;
//        } else {
//            invisibletext = textview2;
//            visibletext = textview1;
//        }
        var visToInvis = ObjectAnimator.ofFloat(imageView, "rotationY", 0f, 90f)
        visToInvis.setDuration(500)
        visToInvis.interpolator = AccelerateInterpolator()
        var invisToVis = ObjectAnimator.ofFloat(layout, "rotationY", -90f, 0f)
        invisToVis.setDuration(500)
        invisToVis.interpolator = AccelerateInterpolator()
        visToInvis?.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                imageView.setVisibility(View.GONE)
                invisToVis.start()
                layout.setVisibility(View.VISIBLE)
            }
        })
        visToInvis?.start()
    }
}