package com.ym.game.view

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Debug
import android.util.Log
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.bytedance.sdk.openadsdk.TTNativeExpressAd
import com.qq.e.ads.nativ.NativeExpressADView
import com.ym.admodule.config.AdManager
import com.ym.admodule.config.ZXADSizeConfig
import com.ym.game.listener.IDialogViewCloseCallback
import com.ym.modulecommon.AppliContext
import com.ym.ddhy.R
import com.ym.library.Constant
import com.ym.library.module.FlyBoxEntity
import com.ym.library.module.TurntableEntity
import com.ym.library.utils.*

object MainDialog {


    var adView: Any? = null

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

        adView = null

        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(AppliContext.get())
        lp?.height = ViewGroup.LayoutParams.MATCH_PARENT
        if (gravity == Gravity.TOP) {
            lp?.height = PhoneUtils.getScreenHeight(AppliContext.get())
        }
        window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        window?.attributes = lp
        if (animation > 0)
            window?.setWindowAnimations(animation)//设置动画
        return mDialogView
    }

    /**
     * 领取红包奖励
     */
    fun getTaskRewardDialog(
        activity: Activity,
        reward: String,
        withdrawClick: View.OnClickListener
    ) {
        val mDialog = Dialog(activity, com.ym.library.R.style.UpdateVersionCompatDialogTheme)
        val mDialogView =
            CenterDialog.showBottomDialog(
                R.layout.dialog_get_water_drop2,
                activity,
                mDialog,
                0,
                Gravity.CENTER
            )
        val layoutAd = mDialogView?.findViewById<FrameLayout>(R.id.layout_get_drop_ad2)

        AdUtils.showFeedAd(activity, "receive_redPack_double_click", layoutAd)

        mDialog.setOnDismissListener(object : DialogInterface.OnDismissListener {
            override fun onDismiss(p0: DialogInterface?) {
                onDestroy()
                AdUtils.loadFeedCacheAdToCache(activity)
            }

        })
        mDialogView.findViewById<ImageView>(R.id.lv_get_drop_close2).setOnClickListener {
            EventUtils.onEvent("click_close_receive_redPack_dialog", "点击关闭领取红包弹窗")
            mDialog.dismiss()
        }
        mDialogView.findViewById<TextView>(R.id.tv_get_drop_count4)?.text = reward
        mDialogView.findViewById<TextView>(R.id.btn_get_six_reward)?.setOnClickListener {
            if (Utils.isFastClick()) {
                withdrawClick.onClick(it)
                mDialog.dismiss()
            }
        }
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog?.setCancelable(false)
        if(activity.isFinishing){
            return
        }
        mDialog?.show()
    }

    /**
     * 转盘奖励
     * type:   奖励类型
     * waterDropCount:   奖励数量
     * adType: 广告的类型
     */
    fun showgetWaterDrop(
        context: Activity?,
        type: Int,
        adType: String,
        rewardCount: Int,
        list: TurntableEntity.SeedListEntity?
    ) {
        if (context == null) {
            return
        }
        val mDialog = Dialog(context, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_get_water_drop,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )

        val ivGetDropBg = mDialogView?.findViewById<ImageView>(R.id.iv_get_drop_bg)
        val ivtitle = mDialogView?.findViewById<ImageView>(R.id.id_img_title)
        val tvGetDropCount = mDialogView?.findViewById<TextView>(R.id.tv_get_drop_count)
        val layoutAd = mDialogView?.findViewById<FrameLayout>(R.id.layout_get_drop_ad)
        val llcontent2 = mDialogView?.findViewById<LinearLayout>(R.id.id_ll_content_2)
        val ivGetDropBg2 = mDialogView?.findViewById<ImageView>(R.id.iv_get_drop_bg2)
        val tvGetDropCount2 = mDialogView?.findViewById<TextView>(R.id.tv_get_drop_count2)
        val llcontent3 = mDialogView?.findViewById<LinearLayout>(R.id.id_ll_content_3)
        val ivGetDropBg3 = mDialogView?.findViewById<ImageView>(R.id.iv_get_drop_bg3)
        val tvGetDropCount3 = mDialogView?.findViewById<TextView>(R.id.tv_get_drop_count3)
        val tvget = mDialogView?.findViewById<TextView>(R.id.id_tv_gongxi_get)
        //3 种子 4 金币 5 加速剂
        when (type) {
            3 -> {
                //tvget.text = "恭喜获得种子"
                ivtitle.setImageResource(R.mipmap.icon_img_title_get_zhongzi)
                llcontent2.visibility = View.GONE
                llcontent3.visibility = View.GONE
                if (list?.seedList?.size == 1) {
                    setImage(ivGetDropBg, list?.seedList[0].id)
                    tvGetDropCount.text = "+${list?.seedList[0].num}"
                } else if (list?.seedList?.size == 2) {
                    llcontent2.visibility = View.VISIBLE
                    setImage(ivGetDropBg, list?.seedList[0].id)
                    tvGetDropCount.text = "+${list?.seedList[0].num}"
                    setImage(ivGetDropBg2, list?.seedList[1].id)
                    tvGetDropCount2.text = "+${list?.seedList[1].num}"

                } else if (list?.seedList?.size == 3) {
                    llcontent2.visibility = View.VISIBLE
                    llcontent3.visibility = View.VISIBLE
                    setImage(ivGetDropBg, list?.seedList[0].id)
                    setImage(ivGetDropBg2, list?.seedList[1].id)
                    setImage(ivGetDropBg3, list?.seedList[2].id)
                    tvGetDropCount.text = "+${list?.seedList[0].num}"
                    tvGetDropCount2.text = "+${list?.seedList[1].num}"
                    tvGetDropCount3.text = "+${list?.seedList[2].num}"
                }

            }
            4 -> {
                ivtitle.setImageResource(R.mipmap.icon_img_title_get_coin_)
                // tvget.text = "恭喜获得金币"
                ivGetDropBg.setImageResource(R.mipmap.icon_coin)
                tvGetDropCount.text = "+${rewardCount}"
            }
            5 -> {
                //tvget.text = "恭喜获得加速剂"
                ivtitle.setImageResource(R.mipmap.icon_img_title_get_speed_water)
                ivGetDropBg.setImageResource(R.mipmap.icon_speed_water)
                tvGetDropCount.text = "+${rewardCount}"
            }
        }

        AdUtils.showFeedAd(context, adType, layoutAd)

        mDialogView.findViewById<TextView>(R.id.btn_get_drop_happy_accept).setOnClickListener {
            mDialog.dismiss()
        }
        mDialogView.findViewById<ImageView>(R.id.lv_get_drop_close).setOnClickListener {
            mDialog.dismiss()
        }

        mDialog.setOnDismissListener(object : DialogInterface.OnDismissListener {
            override fun onDismiss(p0: DialogInterface?) {
                onDestroy()
                AdUtils.loadFeedCacheAdToCache(context)
            }

        })
        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(false)
        if(context.isFinishing){
            return
        }
        mDialog.show()
    }

    fun setImage(img: ImageView, id: Int) {
        when (id) {
            1 -> {
                img.setImageResource(R.mipmap.icon_zhongzi_one)
            }
            2 -> {
                img.setImageResource(R.mipmap.icon_zhongzi_two)
            }
            3 -> {
                img.setImageResource(R.mipmap.icon_zhongzi_three)
            }
            4 -> {
                img.setImageResource(R.mipmap.icon_zhongzi_four)
            }
            5 -> {
                img.setImageResource(R.mipmap.icon_zhongzi_five)
            }
            6 -> {
                img.setImageResource(R.mipmap.icon_zhongzi_six)
            }
            7 -> {
                img.setImageResource(R.mipmap.icon_zhongzi_seven)
            }
            8 -> {
                img.setImageResource(R.mipmap.icon_zhongzi_eight)
            }
            9 -> {
                img.setImageResource(R.mipmap.icon_zhongzi_eight)
            }

        }
    }

    //
    fun showopenBox(
        context: Activity?,
        type: Int,
        adType: String,
        rewardCount: Int,
        result: FlyBoxEntity,
        mListener: IDialogViewCloseCallback
    ) {
        if (context == null) {
            return
        }
        val mDialog = Dialog(context, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_get_water_drop,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )

        val ivGetDropBg = mDialogView?.findViewById<ImageView>(R.id.iv_get_drop_bg)
        val ivtitle = mDialogView?.findViewById<ImageView>(R.id.id_img_title)
        val tvGetDropCount = mDialogView?.findViewById<TextView>(R.id.tv_get_drop_count)
        val layoutAd = mDialogView?.findViewById<FrameLayout>(R.id.layout_get_drop_ad)
        val llcontent2 = mDialogView?.findViewById<LinearLayout>(R.id.id_ll_content_2)
        val ivGetDropBg2 = mDialogView?.findViewById<ImageView>(R.id.iv_get_drop_bg2)
        val tvGetDropCount2 = mDialogView?.findViewById<TextView>(R.id.tv_get_drop_count2)
        val llcontent3 = mDialogView?.findViewById<LinearLayout>(R.id.id_ll_content_3)
        val ivGetDropBg3 = mDialogView?.findViewById<ImageView>(R.id.iv_get_drop_bg3)
        val tvGetDropCount3 = mDialogView?.findViewById<TextView>(R.id.tv_get_drop_count3)
        var tvget = mDialogView?.findViewById<TextView>(R.id.id_tv_gongxi_get)

        //3 种子 4 金币 5 加速剂
        when (type) {
            1 -> {
                tvget.text = "恭喜获得种子"
                ivtitle.setImageResource(R.mipmap.icon_img_title_get_zhongzi)
                llcontent2.visibility = View.GONE
                llcontent3.visibility = View.GONE
                if (result.seedList?.size == 1) {
                    setImage(ivGetDropBg, result.seedList[0].id)
                    tvGetDropCount.text = "+${result.seedList[0].num}"
                } else if (result.seedList?.size == 2) {
                    llcontent2.visibility = View.VISIBLE
                    setImage(ivGetDropBg, result.seedList[0].id)
                    setImage(ivGetDropBg2, result.seedList[1].id)
                    tvGetDropCount.text = "+${result.seedList[0].num}"
                    tvGetDropCount2.text = "+${result.seedList[1].num}"

                } else if (result.seedList?.size == 3) {
                    llcontent2.visibility = View.VISIBLE
                    llcontent3.visibility = View.VISIBLE
                    setImage(ivGetDropBg, result.seedList[0].id)
                    setImage(ivGetDropBg2, result.seedList[1].id)
                    setImage(ivGetDropBg3, result.seedList[2].id)
                    tvGetDropCount.text = "+${result.seedList[0].num}"
                    tvGetDropCount2.text = "+${result.seedList[1].num}"
                    tvGetDropCount3.text = "+${result.seedList[2].num}"
                }

            }
            2 -> {
                ivtitle.setImageResource(R.mipmap.icon_img_title_get_coin_)
                tvget.text = "恭喜获得金币"
                ivGetDropBg.setImageResource(R.mipmap.icon_coin)
                tvGetDropCount.text = "+${rewardCount}"
            }
            3 -> {
                ivtitle.setImageResource(R.mipmap.icon_img_title_get_speed_water)
                tvget.text = "恭喜获得加速剂"
                ivGetDropBg.setImageResource(R.mipmap.icon_speed_water)
                tvGetDropCount.text = "+${rewardCount}"
            }
        }

        //shwoAd(adType, context, layoutAd)

        mDialogView.findViewById<TextView>(R.id.btn_get_drop_happy_accept).setOnClickListener {
            mDialog.dismiss()
            mListener.onCloseViewSuccess(true)
        }
        mDialogView.findViewById<ImageView>(R.id.lv_get_drop_close).setOnClickListener {
            mDialog.dismiss()
            mListener.onCloseViewSuccess(true)
        }

        AdUtils.showFeedAd((context as Activity), adType, layoutAd)

//        AdManager.loadFeedAd(
//            ADConfig.AD_IMG_FADE,
//            activity,
//            ZXADSizeConfig(
//                Utils.px2dip(PhoneUtils.getScreenWidth(activity).toFloat()) - 56,
//                ((Utils.px2dip(activity.windowManager?.defaultDisplay?.width!!.toFloat()) - 56) * 0.66).toInt()
//            ),
//            layouAd
//        )
        mDialog.setOnDismissListener(object : DialogInterface.OnDismissListener {
            override fun onDismiss(p0: DialogInterface?) {
                onDestroy()
                AdUtils.loadFeedCacheAdToCache(context)
            }

        })
        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(false)
        if(context.isFinishing){
            return
        }
        mDialog.show()
    }

    //福利中心开宝箱
    fun showWelfareOpenBox(
        context: Activity?,
        results: List<FlyBoxEntity>,
        mListener: IDialogViewCloseCallback
    ) {
        if (context == null) {
            return
        }
        EventUtils.onEvent("welfare_task_reward_box_show")
        val mDialog = Dialog(context, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_get_welfare_drop,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )

        val layout_seed = mDialogView?.findViewById<LinearLayout>(R.id.layout_seed)
        val layout_coins = mDialogView?.findViewById<LinearLayout>(R.id.layout_coins)
        val layout_accelerator = mDialogView?.findViewById<LinearLayout>(R.id.layout_accelerator)

        val tv_seed = mDialogView?.findViewById<TextView>(R.id.tv_seed)
        val iv_seed_bg = mDialogView?.findViewById<ImageView>(R.id.iv_seed_bg)
        val tv_coin = mDialogView?.findViewById<TextView>(R.id.tv_coin)
        val tv_accelerator = mDialogView?.findViewById<TextView>(R.id.tv_accelerator)

        val layoutAd = mDialogView?.findViewById<FrameLayout>(R.id.layout_get_drop_ad)

        for (entity in results) {
            //1种子 2金币 3加速剂
            when (entity.awardType) {
                1 -> {
                    layout_seed.visibility = View.VISIBLE
                    setImage(iv_seed_bg, entity.id)
                    tv_seed.text = "+${entity.awardNum}"
                }
                2 -> {
                    layout_coins.visibility = View.VISIBLE
                    tv_coin.text = "+${entity.awardNum}"
                }
                3 -> {
                    layout_accelerator.visibility = View.VISIBLE
                    tv_accelerator.text = "+${entity.awardNum}"
                }

            }
        }

        mDialogView.findViewById<TextView>(R.id.btn_get_drop_happy_accept).setOnClickListener {
            mDialog.dismiss()
            mListener.onCloseViewSuccess(true)
        }
        mDialogView.findViewById<ImageView>(R.id.lv_get_drop_close).setOnClickListener {
            mDialog.dismiss()
            mListener.onCloseViewSuccess(true)
        }

        AdUtils.showFeedAd((context as Activity), "welfare_openbox", layoutAd)

        mDialog.setOnDismissListener(object : DialogInterface.OnDismissListener {
            override fun onDismiss(p0: DialogInterface?) {
                onDestroy()
                AdUtils.loadFeedCacheAdToCache((context as Activity))
            }

        })
        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(false)
        if(context.isFinishing){
            return
        }
        mDialog.show()
    }

    //提现弹窗
    fun showWithDraw(
        context: Activity?,
        status: Int,
        title: String,
        reason: String,
        buttonText: String,
        buttonStatus: Int,
        id: Int?,
        type: Int?,
        jine: String?,
        isWelfarePage: Boolean,
        dialog: Dialog

    ) {
        if (context == null) {
            return
        }
        val mDialog = Dialog(context, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_with_draw_success,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )

        val imgtitle = mDialogView?.findViewById<ImageView>(R.id.id_img_title)
        val tvcashcontent = mDialogView?.findViewById<TextView>(R.id.id_tv_cash_content)
        val imggoon = mDialogView?.findViewById<ImageView>(R.id.id_tv_go_on_make_money)
        when (status) {
            1 -> {
                imgtitle.setImageResource(R.mipmap.img_duihuan_success)
                imggoon.setImageResource(R.mipmap.img_go_zhongcai)
                if (type == 1) {
                    EventUtils.onEvent("show_xinshou0.3", "新手0.3")
                } else if (type == 4) {
                    EventUtils.onEvent("show_fuli0.3", "福利0.3")
                } else if (type == 7) {
                    EventUtils.onEvent("welfare_withdraw_success_dialog_show")
                } else if (type == 8) {
                    EventUtils.onEvent("attendance_withdraw_success_dialog_show")
                } else {
                    if (id != null) {
                        when (id) {
                            1 -> {
                                EventUtils.onEvent("show_hongmeigui", "红玫瑰" + jine)
                            }
                            2 -> {
                                EventUtils.onEvent("show_zimeigui", "紫玫瑰" + jine)
                            }
                            3 -> {
                                EventUtils.onEvent("show_linglan", "铃兰" + jine)
                            }
                            4 -> {
                                EventUtils.onEvent("show_juhua", "菊花" + jine)
                            }
                        }
                    }
                }
            }
            2 -> {
                imgtitle.setImageResource(R.mipmap.img_duihuan_success)
                imggoon.setImageResource(R.mipmap.img_go_zhongcai)
                // tvtitle.text = title
            }
            3 -> {
                imgtitle.setImageResource(R.mipmap.img_wenxin_trip)
                imggoon.setImageResource(R.mipmap.img_i_konw)
                // tvtitle.text = title
            }
            4 -> {
                imgtitle.setImageResource(R.mipmap.img_wenxin_trip)
                imggoon.setImageResource(R.mipmap.img_go_get_diamand)
            }
            5 -> {
                imgtitle.setImageResource(R.mipmap.img_wenxin_trip)
                imggoon.setImageResource(R.mipmap.img_go_zhongcai)
            }
            6 -> {
                imgtitle.setImageResource(R.mipmap.img_wenxin_trip)
                imggoon.setImageResource(R.mipmap.img_go_zhongcai)
            }

        }

        tvcashcontent.text = reason
        imggoon.addClickScale()
        imggoon.setOnClickListener {
            if (buttonStatus == 0) {
                mDialog.dismiss()
            } else if (buttonStatus == 1) {
                mDialog.dismiss()
                dialog.dismiss()
                if (!isWelfarePage) {
                    JumpUtils.jumpWelfareActivity()
                }
//                JumpUtils.h5Jump("每日福利", Constant.Param.WelfareCenter, false, context as Activity)
            } else if (buttonStatus == 2) {
                mDialog.dismiss()
                dialog.dismiss()
            }

        }
        mDialogView.findViewById<ImageView>(R.id.id_img_dialog_close).setOnClickListener {
            mDialog.dismiss()
        }
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog?.setCancelable(false)
        if(context.isFinishing){
            return
        }
        mDialog?.show()
    }

    fun showActivityDesc(activity: Activity) {
        if (activity == null) return
        val mDialog = Dialog(activity, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView = showBottomDialog(
            R.layout.dialog_activity_desc,
            activity,
            mDialog,
            0,
            Gravity.CENTER
        )
        mDialogView.findViewById<ImageView>(R.id.id_img_dialog_activity_desc).setOnClickListener {
            mDialog.dismiss()
        }
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog?.setCancelable(false)
        if(activity.isFinishing){
            return
        }
        mDialog?.show()
    }

    // 获取碎片红包时的弹窗
    fun showDialoggetPuzzle(
        activity: Activity?,
        diffCardsNum: Int,
        onClickListener: View.OnClickListener
    ) {
        if (activity == null) return
        val mDialog = Dialog(activity, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView = showBottomDialog(
            R.layout.dialog_get_puzzle,
            activity,
            mDialog,
            0,
            Gravity.CENTER
        )
        EventUtils.onEvent("show_suipianbuzu_dialog", "碎片不足弹窗曝光")
        mDialogView.findViewById<TextView>(R.id.id_tv_num_puzzle)?.text = "${diffCardsNum}"
        mDialogView.findViewById<ImageView>(R.id.id_img_close_get_puzzle).setOnClickListener {
            mDialog.dismiss()
        }
        mDialogView.findViewById<ImageView>(R.id.id_img_canle).setOnClickListener {
            mDialog.dismiss()
        }
        mDialogView.findViewById<ImageView>(R.id.id_img_sure).setOnClickListener {
            mDialog.dismiss()
            EventUtils.onEvent("click_pintu_quhuode_video", "去获得视频按钮点击")
            onClickListener.onClick(it)
        }
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog?.setCancelable(false)
        if(activity.isFinishing){
            return
        }
        mDialog?.show()
    }

    // 云朵加速的弹窗
    fun showDialogCloud(
        activity: Activity?,
        speedtime: Int,
        speedtimes: Int,
        onClickListener: View.OnClickListener
    ) {
        if (activity == null) return
        val mDialog = Dialog(activity, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView = showBottomDialog(
            R.layout.dialog_cloud_speed,
            activity,
            mDialog,
            0,
            Gravity.CENTER
        )
        EventUtils.onEvent("show_cloud_speed_up_dialog", "云朵加速弹窗")
        val layoutAd = mDialogView?.findViewById<FrameLayout>(R.id.layout_get_drop_ad)
        AdUtils.showFeedAd(activity, "cloud_speed_bigimage", layoutAd)
        mDialogView.findViewById<TextView>(R.id.id_tv_speed_time)?.text = "所有土地获得${speedtime}分钟加速"
        mDialogView.findViewById<TextView>(R.id.id_cloud_speed_num)?.text =
            "每天晚上0点重置视频次数 (剩余${speedtimes}次)"
        mDialogView.findViewById<ImageView>(R.id.id_img_close_cloud_speed).setOnClickListener {
            mDialog.dismiss()
        }

        mDialogView.findViewById<ImageView>(R.id.id_img_all_speed).setOnClickListener {
            EventUtils.onEvent("click_cloud_speed_up_video_btn", "云朵加速视频点击")
            onClickListener.onClick(it)
            mDialog.dismiss()
        }
        mDialog.setOnDismissListener(object : DialogInterface.OnDismissListener {
            override fun onDismiss(p0: DialogInterface?) {
                onDestroy()
                AdUtils.loadFeedCacheAdToCache(activity)
            }

        })
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog?.setCancelable(false)
        if(activity.isFinishing){
            return
        }
        mDialog?.show()
    }

    //生虫弹窗
    fun showDialogWorm(
        activity: Activity?,
        onClickListener: View.OnClickListener
    ) {
        if (activity == null) return
        val mDialog = Dialog(activity, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView = showBottomDialog(
            R.layout.dialog_worm,
            activity,
            mDialog,
            0,
            Gravity.CENTER
        )
        EventUtils.onEvent("show_clean_worm_dialog", "除虫弹窗")
        val layoutAd = mDialogView?.findViewById<FrameLayout>(R.id.layout_get_drop_ad)
        AdUtils.showFeedAd(activity, "clean_worm", layoutAd)
        mDialogView.findViewById<ImageView>(R.id.id_img_close_clean_worm).setOnClickListener {
            mDialog.dismiss()
        }

        mDialogView.findViewById<ImageView>(R.id.id_img_clean_worm).setOnClickListener {
            EventUtils.onEvent("click_clean_worm_video_btn", "除虫看视频点击")
            onClickListener.onClick(it)
            mDialog.dismiss()
        }
        mDialog.setOnDismissListener(object : DialogInterface.OnDismissListener {
            override fun onDismiss(p0: DialogInterface?) {
                onDestroy()
                AdUtils.loadFeedCacheAdToCache(activity)
            }
        })
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog?.setCancelable(false)
        if(activity.isFinishing){
            return
        }
        mDialog?.show()
    }


    //种子不足弹窗
    fun showDialogSeedNotEnough(
        activity: Activity?,
        id: Int?,
        seedPrice: Int?,
        seedVideo: Int?,
        buySeedOnClickListener: View.OnClickListener,
        receiveSeedOnClickListener: View.OnClickListener
    ) {
        if (activity == null) return
        val mDialog = Dialog(activity, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView = showBottomDialog(
            R.layout.dialog_seed_not_enough,
            activity,
            mDialog,
            0,
            Gravity.CENTER
        )
        val layoutAd = mDialogView?.findViewById<FrameLayout>(R.id.layout_get_drop_ad)
        AdUtils.showFeedAd(activity, "seed_not_enough_bigimage", layoutAd)
        mDialogView.findViewById<TextView>(R.id.id_tv_seed_price)?.text = "$seedPrice"
        mDialogView.findViewById<TextView>(R.id.id_tv_seed_video_num)?.text =
            "每天晚上0点重置视频次数 (剩余${seedVideo}次)"
        var seedImg = mDialogView.findViewById<ImageView>(R.id.id_seed_not_enough_img);
        if (id != null) {
            setImage(seedImg, id)
        }
        mDialogView.findViewById<ImageView>(R.id.id_img_close_seed_dialog).setOnClickListener {
            mDialog.dismiss()
        }
        //买种子
        mDialogView.findViewById<RelativeLayout>(R.id.id_img_bug_seed).setOnClickListener {
            buySeedOnClickListener.onClick(it)
            mDialog.dismiss()
        }
        //看视频领取种子
        mDialogView.findViewById<ImageView>(R.id.id_img_receive_seed).setOnClickListener {
            receiveSeedOnClickListener.onClick(it)
            mDialog.dismiss()
        }
        mDialog.setOnDismissListener(object : DialogInterface.OnDismissListener {
            override fun onDismiss(p0: DialogInterface?) {
                onDestroy()
                AdUtils.loadFeedCacheAdToCache(activity)
            }
        })
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog?.setCancelable(false)
        if(activity.isFinishing){
            return
        }
        mDialog?.show()
    }


    /**
     * 添加点击缩放效果
     */
    fun View.addClickScale(scale: Float = 0.9f, duration: Long = 150) {
        this.setOnTouchListener { _, event ->
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    this.animate().scaleX(scale).scaleY(scale).setDuration(duration).start()
                }
                MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                    this.animate().scaleX(1f).scaleY(1f).setDuration(duration).start()
                }
            }
            // 点击事件处理，交给View自身
            this.onTouchEvent(event)
        }
    }

    fun onDestroy() {
        if (adView != null) {
            if (adView is NativeExpressADView) {
                (adView as NativeExpressADView)?.destroy()
            } else if (adView is TTNativeExpressAd) {
                (adView as TTNativeExpressAd)?.destroy()
            }
        }
    }
}