package com.zx.ad.platform.gdt

import android.app.Activity
import android.content.Context
import android.widget.FrameLayout
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.managers.GDTAdSdk
import com.qq.e.comm.util.AdError
import com.zx.ad.callback.AdLoadCallBack
import com.zx.ad.callback.RewardVideoAdListener
import com.zx.ad.callback.SplashAdListener
import com.zx.ad.modulecommon.module.NewsEntity
import com.zx.ad.platform.ADType
import com.zx.ad.utils.AdEventUtils
import com.zx.ad.utils.LogUtils


/**
 * @author (wangXuewei)
 * @datetime 2022-06-14 15:22 GMT+8
 * @detail :广点通广告平台
 */
object GdtAdManager {


    fun adInit(context: Context, appId: String) {
        GDTAdSdk.init(context,appId)
//        GDTAdSdk.init(context, "1101152570")
    }

    private var splashAd: SplashAD? = null
    private var mSplashAdListener: SplashAdListener? = null

    fun loadSplashAd(context: Context, entity: NewsEntity, loadCallBack: AdLoadCallBack) {
        mSplashAdListener = null
        splashAd = SplashAD(context, entity.codeId, object : SplashADListener {
            override fun onADDismissed() {
                if (mSplashAdListener != null) {
                    mSplashAdListener?.onAdSkip()
                }
            }

            override fun onNoAD(p0: AdError?) {
                if (mSplashAdListener != null) {
                    mSplashAdListener?.onError("GDT SplashShow p0:${p0?.errorCode} msg:${p0?.errorMsg}")
                } else {
                    AdEventUtils.onEventPullFail(entity)
                    loadCallBack.OnAdLoadFail("GDT Splash p0:${p0?.errorCode} msg:${p0?.errorMsg}")
                }
            }

            override fun onADPresent() {

            }

            override fun onADClicked() {

            }

            override fun onADTick(p0: Long) {

            }

            override fun onADExposure() {
                if (mSplashAdListener != null) {
                    mSplashAdListener?.onAdShow()
                }
            }

            override fun onADLoaded(p0: Long) {
                AdEventUtils.onEventPullSuccess(entity)
                loadCallBack.OnAdLoadSuccess(ADType.PLATFORM_AD_GDT, entity)
            }
        })
        splashAd?.fetchAdOnly()
    }

    fun showSplashAd(
        context: Activity,
        entity: NewsEntity,
        mSplashContainer: FrameLayout?,
        listener: SplashAdListener
    ) {
        if (splashAd == null) {
            listener.onError("GDT splashAd is Null")
            return
        }
        mSplashAdListener = listener
        if (mSplashContainer != null && !context.isFinishing) {
            mSplashContainer.removeAllViews()
            splashAd?.showAd(mSplashContainer)
        } else {
            listener.onError("GDT view Exception")
            return
        }
    }


    private var rewardVideoAd: RewardVideoAD? = null
    private var mRewardAdListener: RewardVideoAdListener? = null

    /**
     *  加载激励视频广告
     */
    fun loadRewardVideoAd(context: Context, entity: NewsEntity, loadCallBack: AdLoadCallBack) {
        mRewardAdListener = null
        rewardVideoAd = RewardVideoAD(context, entity.codeId, object : RewardVideoADListener {
            override fun onADLoad() {
                AdEventUtils.onEventPullSuccess(entity)
                loadCallBack.OnAdLoadSuccess(ADType.PLATFORM_AD_KS, entity)
            }

            override fun onVideoCached() {

            }

            override fun onADShow() {
                if (mRewardAdListener != null) {
                    AdEventUtils.onEventShowSuccess(entity)
                    LogUtils.d("onVideoPlayStart")
                    mRewardAdListener?.onRewardVideoAdShow()
                }
            }

            override fun onADExpose() {

            }

            override fun onReward(p0: MutableMap<String, Any>?) {
                if (mRewardAdListener != null) {
                    mRewardAdListener?.onRewardVideoAdReward()
                }
            }

            override fun onADClick() {

            }

            override fun onVideoComplete() {
                AdEventUtils.onEventComplete(entity)
            }

            override fun onADClose() {
                if (mRewardAdListener != null) {
                    AdEventUtils.onEventComplete(entity)
                    LogUtils.d("onADClose")
                    mRewardAdListener?.onRewardVideoAdClose()
                }
            }

            override fun onError(p0: AdError?) {
                if (mRewardAdListener != null) {
                    AdEventUtils.onEventShowFail(entity)
                    mRewardAdListener?.onRewardVideoAdError("GDT RewardVideoAd PlayError p0:${p0?.errorCode} msg:${p0?.errorMsg}")
                } else {
                    AdEventUtils.onEventPullFail(entity)
                    loadCallBack.OnAdLoadFail("GDT RewardVideoAd p0:${p0?.errorCode} msg:${p0?.errorMsg}")
                }
            }
        })
        rewardVideoAd?.loadAD()
    }

    /**
     *  展示激励视频广告
     */
    fun showRewardVideoAd(
        context: Activity, entity: NewsEntity,
        listener: RewardVideoAdListener
    ) {
        if (rewardVideoAd == null) {
            listener.onRewardVideoAdError("KS rewardVideoAd Is Null")
            return
        }
        mRewardAdListener = listener
        if (!context.isFinishing) {
            rewardVideoAd?.showAD(context)
        } else {
            listener.onRewardVideoAdError("context is finish")
        }
    }

}