package com.ym.admodule.gdt

import android.app.Activity
import android.os.Handler
import android.os.Message
import android.os.SystemClock
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import com.qq.e.ads.cfg.VideoOption
import com.qq.e.ads.interstitial2.UnifiedInterstitialAD
import com.qq.e.ads.interstitial2.UnifiedInterstitialADListener
import com.qq.e.ads.interstitial2.UnifiedInterstitialMediaListener
import com.qq.e.ads.nativ.ADSize
import com.qq.e.ads.nativ.NativeExpressAD
import com.qq.e.ads.nativ.NativeExpressADView
import com.qq.e.ads.rewardvideo.RewardVideoAD
import com.qq.e.ads.rewardvideo.RewardVideoADListener
import com.qq.e.ads.splash.SplashAD
import com.qq.e.ads.splash.SplashADListener
import com.qq.e.comm.constants.AdPatternType
import com.qq.e.comm.util.AdError
import com.ym.admodule.config.*
import com.ym.admodule.listener.ZXADFullVideoListener
import com.ym.admodule.listener.ZXADSplashListener
import com.ym.admodule.listener.ZXADVideoListener
import com.ym.modulecommon.module.NewsEntity
import com.ym.modulecommon.utils.AdEventUtils
import com.ym.modulecommon.utils.LogUtils
import java.util.HashMap
import java.util.concurrent.ConcurrentLinkedQueue


object GDTAdUtils {
    private val TAG = "GDTAdUtils"
    private var actionName = ""

    /**
     * 加载开屏广告
     */
    fun loadSplashAd(
        newsEntity: NewsEntity,
        activity: Activity,
        container: ViewGroup,
        zxadListener: ZXADSplashListener,
        skipview: TextView?,
        zxadCallback: ZXADCallback
    ) {
        val splashAD = SplashAD(activity, container, newsEntity.codeId, object : SplashADListener {
            override fun onADExposure() {
                AdEventUtils.onEventShow(newsEntity)
            }

            override fun onADDismissed() {
            }

            override fun onADPresent() {
                skipview?.setVisibility(View.VISIBLE)
            }

            override fun onADLoaded(p0: Long) {
                AdEventUtils.onEventPullSuccess(newsEntity, AdEventUtils.AD_MODEL_SPLAH)
                zxadCallback.onAdSuccess()
                zxadListener.onAdShow()
            }

            override fun onNoAD(p0: AdError?) {
                AdEventUtils.onEventPullFail(newsEntity, AdEventUtils.AD_MODEL_SPLAH)
                AdEventUtils.onEvent(newsEntity, p0?.errorCode, p0?.errorMsg)
                skipview?.visibility = View.VISIBLE
                zxadCallback.onAdFail()
            }

            override fun onADClicked() {
                AdEventUtils.onEventClick(newsEntity, AdEventUtils.AD_MODEL_SPLAH)
            }

            override fun onADTick(p0: Long) {
                skipview?.setText("跳过 ：" + p0 / 1000)
                if ((p0 / 1000) == 0L) {
                    zxadListener.onAdSkip()
                }
            }
        }, 3000)
        splashAD.fetchAndShowIn(container)
        skipview?.setOnClickListener {
            zxadListener.onAdSkip()
        }
    }


    private var currentInterstitialAd: UnifiedInterstitialAD? = null

    /**
     * 加载插屏广告
     */
    fun loadInteractionAd(
        activity: Activity,
        newsEntity: NewsEntity,
        callback: ZXADCallback,
        listener: ZXADVideoListener
    ) {
        var iad: UnifiedInterstitialAD? = null


        iad = UnifiedInterstitialAD(
            activity,
            newsEntity.codeId,
            object : UnifiedInterstitialADListener {
                override fun onADExposure() {
                    AdEventUtils.onEventShow(newsEntity)
                    listener?.onAdShow()
                    LogUtils.e(TAG, "GDT 插屏 onAdShow")
                }

                override fun onVideoCached() {
                }

                override fun onADOpened() {
                    LogUtils.e(TAG, "GDT 插屏 onADOpened")
                }

                override fun onADClosed() {
                    listener?.onAdClose(AdInfo(newsEntity.codeId))
                    LogUtils.e(TAG, "GDT 插屏 onADClosed")
                }

                override fun onADLeftApplication() {
                }

                override fun onADReceive() {
                    LogUtils.e(TAG, "GDT 插屏 onADReceive" + iad?.getAdPatternType())
                    if (newsEntity.adType == 8) {
                        iad?.showFullScreenAD(activity)//已全屏的形式展现
                    } else {
                        iad?.show(activity)//已popwindow的形式展现
                    }
                    AdEventUtils.onEventPullSuccess(newsEntity, newsEntity?.slotName)
                    callback?.onAdSuccess()

                    if (iad?.getAdPatternType() == AdPatternType.NATIVE_VIDEO) {
//                        setMediaListener(iad,newsEntity,listener)
                    }
                }

                override fun onNoAD(p0: AdError?) {
                    AdEventUtils.onEvent(newsEntity, p0?.errorCode, p0?.errorMsg)
                    callback.onAdFail()
                    LogUtils.e(TAG, "GDT 插屏：errorMsg:${p0?.errorMsg} -- errorCode:${p0?.errorCode}")
                }

                override fun onADClicked() {
                    listener?.onAdVideoBarClick()
                    LogUtils.e(TAG, "GDT 插屏 onADClicked")
                    AdEventUtils?.onEventClick(newsEntity, newsEntity?.slotName)
                }
            })
        val builder = VideoOption.Builder()
        var option = builder.build()
        option = builder.setAutoPlayMuted(false)
            .setAutoPlayPolicy(VideoOption.AutoPlayPolicy.ALWAYS)
            .setDetailPageMuted(false)
            .build()
        iad.setVideoPlayPolicy(VideoOption.VideoPlayPolicy.AUTO)
        iad.setVideoOption(option)
        if (newsEntity.adType == 8) {
            //已全屏的形式展现
            iad.loadFullScreenAD()
        } else {
            iad.loadAD()
        }

        currentInterstitialAd = iad
//        iad.setMinVideoDuration(10)
//        iad.setMaxVideoDuration(30)
//        Handler(activity.mainLooper).postDelayed(
//            Runnable {
//                iad.show(activity)//已popwindow的形式展现
//            }, 0
//        )

    }

    fun destroyInteractionAd() {
        if (currentInterstitialAd != null) {
            currentInterstitialAd?.close()
            currentInterstitialAd?.destroy()
        }
    }

    //全屏视屏是否播放完成
    private var isComplete = false
    private fun setMediaListener(
        iad: UnifiedInterstitialAD?,
        newsEntity: NewsEntity,
        listener: ZXADVideoListener
    ) {
        if (iad != null) {
            iad?.setMediaListener(object : UnifiedInterstitialMediaListener {
                override fun onVideoPageOpen() {
                    LogUtils.d(TAG, "GDT 全屏 onVideoPageOpen")
                }

                override fun onVideoLoading() {
                }

                override fun onVideoReady(p0: Long) {
                }

                override fun onVideoInit() {
                }

                override fun onVideoPause() {
                }

                override fun onVideoPageClose() {
                    LogUtils.d(TAG, "GDT 全屏 onVideoPageClose")
                }

                override fun onVideoStart() {
                    LogUtils.d(TAG, "GDT 全屏 onVideoStart")
                }

                override fun onVideoComplete() {
                    isComplete = true
                    LogUtils.d(TAG, "GDT 全屏 onVideoComplete")
                }

                override fun onVideoError(p0: AdError?) {
                }
            })
        }
    }


    fun loadFeedAd(
        activity: Activity,
        newsEntity: NewsEntity,
        zxadModelConfig: ZXADSizeConfig,
        container: FrameLayout,
        callback: ZXADCallback
    ) {
        var nativeExpressAD = NativeExpressAD(
            activity,
            ADSize(ADSize.FULL_WIDTH, zxadModelConfig.height),
            newsEntity.codeId,
            object : NativeExpressAD.NativeExpressADListener {
                override fun onADCloseOverlay(p0: NativeExpressADView?) {
                }

                override fun onADLoaded(adList: MutableList<NativeExpressADView>?) {
                    if (adList?.size == 0) {
                    } else {
                        AdEventUtils.onEventPullSuccess(newsEntity, AdEventUtils.AD_MODEL_BIGIMG)
                    }

                    var nativeExpressADView = adList?.get(0);
                    nativeExpressADView?.render()
                    if (container.getChildCount() > 0) {
                        container.removeAllViews();
                    }
                    container.visibility = View.VISIBLE
                    container.addView(nativeExpressADView);
                    callback.onAdSuccess()
                }

                override fun onADOpenOverlay(p0: NativeExpressADView?) {

                }

                override fun onRenderFail(p0: NativeExpressADView?) {
                    AdEventUtils.onEventShowFail(newsEntity, AdEventUtils.AD_MODEL_BIGIMG)
                    callback.onAdFail()
                }

                override fun onADExposure(p0: NativeExpressADView?) {
                    AdEventUtils.onEventShow(newsEntity)
                }

                override fun onADClosed(p0: NativeExpressADView?) {
                }

                override fun onADLeftApplication(p0: NativeExpressADView?) {
                }

                override fun onNoAD(p0: AdError?) {
                    AdEventUtils.onEventPullFail(newsEntity, AdEventUtils.AD_MODEL_BIGIMG)
                    AdEventUtils.onEvent(newsEntity, p0?.errorCode, p0?.errorMsg)
                    callback.onAdFail()
                }

                override fun onADClicked(p0: NativeExpressADView?) {
                    AdEventUtils.onEventClick(newsEntity, AdEventUtils.AD_MODEL_BIGIMG)
                }

                override fun onRenderSuccess(p0: NativeExpressADView?) {

                }

            })
        nativeExpressAD.loadAD(1);
    }

    //预加载
    fun loadFeedAd(
        activity: Activity,
        newsEntity: NewsEntity,
        zxadModelConfig: ZXADSizeConfig,
        callback: ZXADCallback
    ) {
        var nativeExpressAD = NativeExpressAD(
            activity,
            ADSize(ADSize.FULL_WIDTH, zxadModelConfig.height),
            newsEntity.codeId,
            object : NativeExpressAD.NativeExpressADListener {
                override fun onADCloseOverlay(p0: NativeExpressADView?) {
                }

                override fun onADLoaded(adList: MutableList<NativeExpressADView>?) {
                    if (adList?.size == 0) {
                    } else {
                        AdEventUtils.onEventPullSuccess(newsEntity, AdEventUtils.AD_MODEL_BIGIMG)
                    }

                    var nativeExpressADView = adList?.get(0)
                    if (nativeExpressADView != null) {
                        if (mFeedCacheMap.size > AdCache.feedAdNumber) {
                            val get = mFeedCacheMap[0]
                            if (get != null) {
                                if (get?.parent != null) {
                                    (get?.parent as ViewGroup).removeView(get)
                                }
                                get?.removeAllViews()
                                get?.destroy()
                                LogUtils.d(TAG, "广点通大图 释放第一个 NativeExpressADView 的资源")
                            }
                        }
                        mFeedCacheMap.add(nativeExpressADView)
                    }
                    callback.onAdSuccess()
                }

                override fun onADOpenOverlay(p0: NativeExpressADView?) {
                }

                override fun onRenderFail(p0: NativeExpressADView?) {
                    AdEventUtils.onEventShowFail(newsEntity, AdEventUtils.AD_MODEL_BIGIMG)
                    callback.onAdFail()
                }

                override fun onADExposure(p0: NativeExpressADView?) {
                    AdEventUtils.onEventShow(newsEntity)
                }

                override fun onADClosed(p0: NativeExpressADView?) {
                }

                override fun onADLeftApplication(p0: NativeExpressADView?) {
                }

                override fun onNoAD(p0: AdError?) {
                    AdEventUtils.onEventPullFail(newsEntity, AdEventUtils.AD_MODEL_BIGIMG)
                    AdEventUtils.onEvent(newsEntity, p0?.errorCode, p0?.errorMsg)
                    callback.onAdFail()
                }

                override fun onADClicked(p0: NativeExpressADView?) {
                    AdEventUtils.onEventClick(newsEntity, AdEventUtils.AD_MODEL_BIGIMG)
                }

                override fun onRenderSuccess(p0: NativeExpressADView?) {
                }

            })

        nativeExpressAD.loadAD(1);
    }

    fun showFeedAd(
        activity: Activity,
        newsEntity: NewsEntity,
        container: FrameLayout
    ) {
        LogUtils.d(TAG, "run 广点通 信息流 showFeedAd ")
        if (activity == null || newsEntity.codeId == null) return
        if (container.getChildCount() > 0) {
            container.removeAllViews();
        }
        if (mFeedCacheMap != null && mFeedCacheMap.isNotEmpty() && mFeedCacheMap[mFeedCacheMap.size - 1] != null) {
            val nativeExpressADView = mFeedCacheMap[mFeedCacheMap.size - 1]
            LogUtils.d(TAG, "run 广点通 信息流 render 渲染")
            nativeExpressADView?.render()
            if (container.getChildCount() > 0) {
                if (container.parent != null) {
                    (container.parent as ViewGroup).removeView(container)
                }
                container.removeAllViews();
            }
            if (nativeExpressADView.parent != null) {
                val parent = nativeExpressADView.parent
                (parent as ViewGroup).removeAllViews()
            }
            container.addView(nativeExpressADView);
            container.visibility = View.VISIBLE
            LogUtils.d(TAG, "run 广点通 信息流 显示")

        } else {
        }

    }

    /**
     * 广点通激励视频分两步，
     * 第一步：加载激励视频
     * 第二部：加载成功之后展示视频
     */
    fun loadRewardVideoAd(
        activity: Activity,
        entity: NewsEntity,
        listener: ZXADVideoListener,
        callback: ZXADCallback
    ) {
        var adLoaded = false

        var rewardVideoAD = RewardVideoAD(activity, entity.codeId, object : RewardVideoADListener {
            override fun onADExpose() {
            }

            override fun onADClick() {
                listener.onAdVideoBarClick()
                AdEventUtils.onEventClick(entity, AdEventUtils.AD_MODEL_REWARD)
            }

            override fun onVideoCached() {
            }

            override fun onReward(p0: MutableMap<String, Any>?) {
                AdEventUtils.onEventComplete(entity, AdEventUtils.AD_MODEL_REWARD)

            }

            override fun onADClose() {
                listener.onAdClose(AdInfo(entity.codeId))
            }


            //如果这里
            override fun onADLoad() {
                AdEventUtils.onEventPullSuccess(entity, AdEventUtils.AD_MODEL_REWARD)
                adLoaded = true
            }

            override fun onVideoComplete() {
            }

            override fun onError(p0: AdError?) {
                AdEventUtils.onEventPullFail(entity, AdEventUtils.AD_MODEL_REWARD)
                AdEventUtils.onEvent(entity, p0?.errorCode, p0?.errorMsg)
                callback.onAdFail()
                LogUtils.d(
                    TAG,
                    "${entity.adPlatform}: errorCode:" + p0?.errorCode + " -- errorMsg:" + p0?.errorMsg
                )
            }

            override fun onADShow() {
                listener.onAdShow()
                callback.onAdSuccess()
                AdEventUtils.onEventShow(entity)
            }
        }) // 有声播放

        rewardVideoAD.loadAD()
        val handler = object : Handler() {
            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                if (adLoaded) { //广告展示检查1：广告成功加载，此处也可以使用videoCached来实现视频预加载完成后再展示激励视频广告的逻辑
                    if (!rewardVideoAD.hasShown()) { //广告展示检查2：当前广告数据还没有展示过
                        val delta: Long = 1000 //建议给广告过期时间加个buffer，单位ms，这里demo采用1000ms的buffer
                        if (SystemClock.elapsedRealtime() < rewardVideoAD.expireTimestamp - delta) {
                            rewardVideoAD.showAD()
                        } else {
                            callback.onAdFail()
                        }
                        removeCallbacksAndMessages(null)
                    } else {
                        callback.onAdFail()
                        removeCallbacksAndMessages(null)
                    }
                } else {
                    sendEmptyMessageDelayed(1, 20)
                }
            }

        }
        handler.sendEmptyMessageDelayed(1, 20)


    }

    //=============================================预加载===========================================
    private var zxVideoListener: ZXADVideoListener? = null
    private var isShow = false
    fun loadCacheRewardAd(
        activity: Activity,
        entity: NewsEntity,
        callback: ZXADCallback
    ) {
        var ad: RewardVideoAD? = null
        var cacheRewardVideoAD =
            RewardVideoAD(activity, entity.codeId, object : RewardVideoADListener {
                override fun onADExpose() {
                }

                override fun onADClick() {
                    AdEventUtils.onEventClick(entity, AdEventUtils.AD_MODEL_REWARD)
                    //播放外部回调
                    zxVideoListener?.onAdVideoBarClick()
                }

                override fun onVideoCached() {
                }

                override fun onReward(p0: MutableMap<String, Any>?) {
                    zxVideoListener?.onSuccess()
                    AdEventUtils.onEventComplete(entity, AdEventUtils.AD_MODEL_REWARD)
                }

                override fun onADClose() {
                    //播放外部回调
                    zxVideoListener?.onAdClose(AdInfo(entity.codeId))
                    zxVideoListener = null
                }


                //如果这里
                override fun onADLoad() {
                    AdEventUtils.onEventPullSuccess(entity, AdEventUtils.AD_MODEL_REWARD)
                    if (ad != null) {
                        //加载内部回调
                        callback.onAdSuccess()
                        //缓存成功，保存
                        setRewardVideo(entity.codeId, ad!!)
                    } else {
                        callback?.onAdFail()
                    }
                }

                override fun onVideoComplete() {
                }

                override fun onError(p0: AdError?) {
                    AdEventUtils.onEventPullFail(entity, AdEventUtils.AD_MODEL_REWARD)
                    AdEventUtils.onEvent(entity, p0?.errorCode, p0?.errorMsg)
                    callback.onAdFail()
                    LogUtils.d(
                        TAG,
                        "${entity.adPlatform}: errorCode:" + p0?.errorCode + " -- errorMsg:" + p0?.errorMsg
                    )
                    if (!isShow) {
                        zxVideoListener?.onError("${entity?.adPlatform},errorMsg:${p0?.errorMsg},errorCode:${p0?.errorCode}")
                        isShow
                    }
                }

                override fun onADShow() {
//                    callback?.onAdSuccess()
                    isShow = true
                    zxVideoListener?.onAdShow()
                    if (!TextUtils.isEmpty(actionName)) {
                        LogUtils.e(TAG, "${entity.adPlatform}: actionName:" + entity.actionName)
                        entity.actionName = actionName
                        AdEventUtils.onEventShow(entity)
                    } else {
                        AdEventUtils.onEventShow(entity)
                    }
                }
            })
        if (activity != null) {
            activity?.runOnUiThread {
                cacheRewardVideoAD.loadAD()
            }
        }
        ad = cacheRewardVideoAD;
    }

    fun playCacheRewardAd(
        activity: Activity,
        newsEntity: NewsEntity,
        listener: ZXADVideoListener,
        callback: ZXADCallback
    ) {
        zxVideoListener = listener
        var rewardVideoAD = getRewardVideo(activity, newsEntity.codeId)
        if (rewardVideoAD != null) {
            if (!rewardVideoAD.hasShown()) {
                actionName = newsEntity.actionName
                rewardVideoAD.showAD()

            } else {
                callback?.onAdFail()
            }
        } else {
            callback?.onAdFail()
        }
    }

    private val mRewardCacheMap = HashMap<String, ConcurrentLinkedQueue<RewardVideoAD>>()
    private val mFeedCacheMap = arrayListOf<NativeExpressADView>()//缓存十个对象

    private fun setRewardVideo(codeId: String?, ksRewardAD: RewardVideoAD) {
        if (codeId == null)
            return
        if (mRewardCacheMap[codeId] != null) {
            var ksRewardVideoAds = mRewardCacheMap[codeId]
            if (ksRewardVideoAds == null) {
                ksRewardVideoAds = ConcurrentLinkedQueue()
            }
            ksRewardVideoAds.offer(ksRewardAD)
            mRewardCacheMap[codeId] = ksRewardVideoAds
        } else {
            val concurrentLinkedQueue = ConcurrentLinkedQueue<RewardVideoAD>()
            concurrentLinkedQueue.offer(ksRewardAD)
            mRewardCacheMap[codeId] = concurrentLinkedQueue
        }
        LogUtils.d(TAG, "广点通${codeId} 缓存数量" + mRewardCacheMap[codeId]?.size)
    }

    private fun getRewardVideo(activity: Activity, codeId: String?): RewardVideoAD? {
        if (mRewardCacheMap[codeId] != null) {
            val ksRewardVideoAds = mRewardCacheMap[codeId]
            if (ksRewardVideoAds != null && ksRewardVideoAds.size > 0) {
                var ad = ksRewardVideoAds.poll()
                LogUtils.d(TAG, "播放 广点通${codeId}")
                return ad;
            }
            LogUtils.d(TAG, "播放 广点通失败${codeId}")
        }
        return null
    }


    //全屏预加载监听
    var listenerFull: ZXADVideoListener? = null

    /**
     * 加载插屏广告
     */
    fun loadCacheFullVideoAd(
        activity: Activity,
        newsEntity: NewsEntity,
        callback: ZXADCallback
    ) {
        var iad: UnifiedInterstitialAD? = null
        iad = UnifiedInterstitialAD(
            activity,
            newsEntity.codeId,
            object : UnifiedInterstitialADListener {
                override fun onADExposure() {
                    AdEventUtils.onEventShow(newsEntity)
                    listenerFull?.onAdShow()
                    LogUtils.e(TAG, "GDT 插屏 onAdShow")
                }

                override fun onVideoCached() {
                }

                override fun onADOpened() {
                    LogUtils.e(TAG, "GDT 插屏 onADOpened")
                }

                override fun onADClosed() {
                    listenerFull?.onAdClose(AdInfo(newsEntity.codeId))
                    LogUtils.e(TAG, "GDT 插屏 onADOpened")
                }

                override fun onADLeftApplication() {
                }

                override fun onADReceive() {
                    LogUtils.e(TAG, "GDT 插屏 onADReceive")
                    if (iad != null) {
                        callback?.onAdSuccess()
                        setFullVideo(newsEntity?.codeId, iad!!)
                    }
                    AdEventUtils.onEventPullSuccess(newsEntity, newsEntity?.slotName)
                }

                override fun onNoAD(p0: AdError?) {
                    AdEventUtils.onEvent(newsEntity, p0?.errorCode, p0?.errorMsg)
                    callback.onAdFail()
                    LogUtils.e(TAG, "GDT 插屏：errorMsg:${p0?.errorMsg} -- errorCode:${p0?.errorCode}")
                }

                override fun onADClicked() {
                    listenerFull?.onAdVideoBarClick()
                    LogUtils.e(TAG, "GDT 插屏 onADClicked")
                    AdEventUtils?.onEventClick(newsEntity, newsEntity?.slotName)
                }
            })
        val builder = VideoOption.Builder()
        var option = builder.build()
        option = builder.setAutoPlayMuted(false)
            .setAutoPlayPolicy(VideoOption.AutoPlayPolicy.ALWAYS)
            .setDetailPageMuted(false)
            .build()
        iad.setVideoPlayPolicy(VideoOption.VideoPlayPolicy.AUTO)
        iad.setVideoOption(option)
        activity?.runOnUiThread {
            if (newsEntity.adType == 8) {
                //已全屏的形式展现
                iad.loadFullScreenAD()
            } else {
                iad.loadAD()
            }
        }
    }

    fun playCacheFullVideoAd(
        activity: Activity,
        newsEntity: NewsEntity,
        listener: ZXADVideoListener,
        callback: ZXADCallback
    ) {
        listenerFull = listener
        var fullVideoAD = getFullVideo(activity, newsEntity.codeId)
        if (fullVideoAD != null) {
            actionName = newsEntity.actionName
            if (newsEntity.adType == 8) {
                fullVideoAD?.showFullScreenAD(activity)//已全屏的形式展现
            } else {
                fullVideoAD?.show(activity)//已popwindow的形式展现
            }
        } else {
            callback?.onAdFail()
        }
    }

    private val mFullCacheMap = HashMap<String, ConcurrentLinkedQueue<UnifiedInterstitialAD>>()

    private fun setFullVideo(codeId: String?, ksFullAD: UnifiedInterstitialAD) {
        if (codeId == null)
            return
        var ksFullVideoAds = mFullCacheMap[codeId]
        if (ksFullVideoAds != null) {
            ksFullVideoAds.offer(ksFullAD)
            mFullCacheMap[codeId] = ksFullVideoAds
        } else {
            val concurrentLinkedQueue = ConcurrentLinkedQueue<UnifiedInterstitialAD>()
            concurrentLinkedQueue.offer(ksFullAD)
            mFullCacheMap[codeId] = concurrentLinkedQueue
        }
        LogUtils.d(TAG, "广点通${codeId} 缓存数量" + mFullCacheMap[codeId]?.size)
    }

    private fun getFullVideo(activity: Activity, codeId: String?): UnifiedInterstitialAD? {
        if (mFullCacheMap[codeId] != null) {
            val ksRewardVideoAds = mFullCacheMap[codeId]
            if (ksRewardVideoAds != null && ksRewardVideoAds.size > 0) {
                var ad = ksRewardVideoAds.poll()
                LogUtils.d(TAG, "播放 广点通${codeId}")
                return ad;
            }
            LogUtils.d(TAG, "播放 广点通失败${codeId}")
        }
        return null
    }


    fun cleanRewardCache() {
        mRewardCacheMap.clear()
    }


    private val arrlistInters = arrayListOf<UnifiedInterstitialAD>()

    fun loadInteractionAd2(
        activity: Activity,
        newsEntity: NewsEntity,
        zxadModelConfig: ZXADSizeConfig,
        callback: ZXADCallback
    ) {
        var iad: UnifiedInterstitialAD? = null
        iad = UnifiedInterstitialAD(
            activity,
            newsEntity.codeId,
            object : UnifiedInterstitialADListener {
                override fun onADExposure() {
                    AdEventUtils.onEventShow(newsEntity)
                    listener?.onAdShow()
                    LogUtils.e(TAG, "GDT 插屏 onAdShow")
                }

                override fun onVideoCached() {
                }

                override fun onADOpened() {
                    LogUtils.e(TAG, "GDT 插屏 onADOpened")
                }

                override fun onADClosed() {
                    listener?.onAdClose(AdInfo(newsEntity.codeId))
                    LogUtils.e(TAG, "GDT 插屏 onADClosed")
                }

                override fun onADLeftApplication() {
                }

                override fun onADReceive() {
                    LogUtils.e(TAG, "GDT 插屏 onADReceive" + iad?.getAdPatternType())
                    if (arrlistInters.size > AdCache.feedAdNumber) {
                        val get = arrlistInters[0]
                        if (get != null) {
                            get?.destroy()
                        }
                    }
                    arrlistInters.add(iad!!)
                    AdEventUtils.onEventPullSuccess(newsEntity, newsEntity?.slotName)
                    callback?.onAdSuccess()
                    if (iad?.getAdPatternType() == AdPatternType.NATIVE_VIDEO) {
//                        setMediaListener(iad,newsEntity,listener)
                    }
                }

                override fun onNoAD(p0: AdError?) {
                    AdEventUtils.onEvent(newsEntity, p0?.errorCode, p0?.errorMsg)
                    callback.onAdFail()
                    LogUtils.e(TAG, "GDT 插屏：errorMsg:${p0?.errorMsg} -- errorCode:${p0?.errorCode}")
                }

                override fun onADClicked() {
                    listener?.onAdVideoBarClick()
                    LogUtils.e(TAG, "GDT 插屏 onADClicked")
                    AdEventUtils?.onEventClick(newsEntity, newsEntity?.slotName)
                }
            })
        val builder = VideoOption.Builder()
        var option = builder.build()
        option = builder.setAutoPlayMuted(false)
            .setAutoPlayPolicy(VideoOption.AutoPlayPolicy.ALWAYS)
            .setDetailPageMuted(false)
            .build()
        iad.setVideoPlayPolicy(VideoOption.VideoPlayPolicy.AUTO)
        iad.setVideoOption(option)
        iad.loadAD()


    }

    private var listener: ZXADFullVideoListener? = null
    fun showInteri(
        activity: Activity,
        newsEntity: NewsEntity,
        callback: ZXADCallback,
        listener: ZXADFullVideoListener
    ) {
        this.listener = listener
        if (arrlistInters != null && arrlistInters.isNotEmpty() && arrlistInters[arrlistInters.size - 1] != null) {
            val mTTad = arrlistInters[arrlistInters.size - 1]
            mTTad?.show(activity)//已popwindow的形式展现
        } else {
            loadInteractionAd(activity, newsEntity, callback, listener)
        }
    }

}