package com.zx.ad.platform.ks

import android.app.Activity
import android.content.Context
import android.widget.FrameLayout
import com.kwad.sdk.api.*
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 KsAdManager {
    private var splashAd: KsSplashScreenAd? = null
    fun adInit(context: Context, appId: String) {
        KsAdSDK.init(
            context, SdkConfig.Builder()
                .appId(appId)
//                .appId("90009")
                .showNotification(true) // 是否展示下载通知栏
                .debug(true) // 是否开启sdk 调试⽇志 可选
                .build()
        );
    }

    fun loadSplashAd(context: Context, entity: NewsEntity, loadCallBack: AdLoadCallBack) {
        var scene = KsScene.Builder(entity.codeId.toLong()).needShowMiniWindow(false).build()
//        var scene = KsScene.Builder(4000000042L).needShowMiniWindow(false).build()
        if (KsAdSDK.getLoadManager() == null) {
            loadCallBack.OnAdLoadFail("Ks KsAdSDK.getLoadManager Is NUll")
            return
        }
        KsAdSDK.getLoadManager()?.loadSplashScreenAd(scene, object :
            KsLoadManager.SplashScreenAdListener {
            override fun onError(p0: Int, p1: String?) {
                AdEventUtils.onEventPullFail(entity)
                loadCallBack.OnAdLoadFail("KS p0:${p0} p1:${p1}")
            }

            override fun onRequestResult(p0: Int) {
            }

            override fun onSplashScreenAdLoad(p0: KsSplashScreenAd?) {
                AdEventUtils.onEventPullSuccess(entity)
                splashAd = p0;
                loadCallBack.OnAdLoadSuccess(ADType.PLATFORM_AD_KS, entity)
            }

        })
    }

    fun showSplashAd(
        context: Activity, entity: NewsEntity,
        mSplashContainer: FrameLayout?,
        listener: SplashAdListener
    ) {
        if (splashAd == null) {
            listener.onError("CSJ splashAd is Null")
            return
        }
        //获取SplashView
        var view =
            splashAd?.getView(context, object : KsSplashScreenAd.SplashScreenAdInteractionListener {
                override fun onAdClicked() {
                    LogUtils.d("ks onAdClicked")
                }

                override fun onAdShowError(p0: Int, p1: String?) {
                    AdEventUtils.onEventShowFail(entity)
                    LogUtils.d("Ks SplashShow p0${p0} p1${p1}")
                    listener.onError("Ks SplashShow p0${p0} p1${p1}")
                }

                override fun onAdShowEnd() {
                    AdEventUtils.onEventComplete(entity)
                    LogUtils.d("onAdShowEnd")
                    listener.onAdSkip()
                }

                override fun onAdShowStart() {
                    AdEventUtils.onEventShowSuccess(entity)
                }

                override fun onSkippedAd() {
                    listener.onAdSkip()
                }

                override fun onDownloadTipsDialogShow() {
                }

                override fun onDownloadTipsDialogDismiss() {
                }

                override fun onDownloadTipsDialogCancel() {
                }
            })
        if (view != null && mSplashContainer != null && !context.isFinishing) {
            mSplashContainer.removeAllViews()
            //把SplashView 添加到ViewGroup中,注意开屏广告view：width =屏幕宽；height >=75%屏幕高
            mSplashContainer.addView(view)
            //设置不开启开屏广告倒计时功能以及不显示跳过按钮,如果这么设置，您需要自定义倒计时逻辑
            //ad.setNotAllowSdkCountdown();
        } else {
            listener.onError("KS view Exception")
            return
        }

    }


    private var rewardVideoAd: KsRewardVideoAd? = null

    /**
     *  加载激励视频广告
     */
    fun loadRewardVideoAd(context: Context, entity: NewsEntity, loadCallBack: AdLoadCallBack) {
        var scene = KsScene.Builder(entity.codeId.toLong()).build()
//        var scene = KsScene.Builder(90009001).build()
        if (KsAdSDK.getLoadManager() == null) {
            loadCallBack.OnAdLoadFail("Ks KsAdSDK.getLoadManager Is NUll")
            return
        }
        KsAdSDK.getLoadManager()
            ?.loadRewardVideoAd(scene, object : KsLoadManager.RewardVideoAdListener {
                override fun onError(p0: Int, p1: String?) {
                    AdEventUtils.onEventPullFail(entity)
                    loadCallBack.OnAdLoadFail("KS RewardVideoAd p0:${p0} p1:${p1}")
                }

                override fun onRewardVideoResult(p0: MutableList<KsRewardVideoAd>?) {

                }

                override fun onRewardVideoAdLoad(adList: MutableList<KsRewardVideoAd>?) {
                    if (adList != null && adList.size > 0) {
                        AdEventUtils.onEventPullSuccess(entity)
                        rewardVideoAd = adList[0];
                        loadCallBack.OnAdLoadSuccess(ADType.PLATFORM_AD_KS, entity)
                    } else {
                        AdEventUtils.onEventPullFail(entity)
                        loadCallBack.OnAdLoadFail("KS RewardVideoAd adSize 0")
                    }
                }
            })

    }

    private var isSkipRewardVideoAd = false

    /**
     *  展示激励视频广告
     */
    fun showRewardVideoAd(
        context: Activity, entity: NewsEntity,
        listener: RewardVideoAdListener
    ) {
        if (rewardVideoAd == null) {
            listener.onRewardVideoAdError("KS rewardVideoAd Is Null")
            return
        }
        if (!context.isFinishing) {
            isSkipRewardVideoAd = false
            rewardVideoAd?.setRewardAdInteractionListener(object :
                KsRewardVideoAd.RewardAdInteractionListener {
                override fun onAdClicked() {
                    LogUtils.d("onAdClicked")
                }

                override fun onPageDismiss() {
                    AdEventUtils.onEventComplete(entity)
                    LogUtils.d("onPageDismiss")
                    listener.onRewardVideoAdClose()
                }

                override fun onVideoPlayError(p0: Int, p1: Int) {
                    AdEventUtils.onEventShowFail(entity)
                    LogUtils.d("onVideoPlayError p0${p0} p1${p1}")
                    listener.onRewardVideoAdError("Ks RewardVideoAd PlayError p0:${p0} p1:${p1}")
                }

                override fun onVideoPlayEnd() {
                    LogUtils.d("onVideoPlayEnd ")
                }

                override fun onVideoSkipToEnd(p0: Long) {
                    LogUtils.d("onVideoSkipToEnd ${p0}")
                }

                override fun onVideoPlayStart() {
                    AdEventUtils.onEventShowSuccess(entity)
                    LogUtils.d("onVideoPlayStart")
                    listener.onRewardVideoAdShow()
                }

                override fun onRewardVerify() {
                    LogUtils.d("onRewardVerify")
                    listener.onRewardVideoAdReward()
                }

                override fun onRewardStepVerify(p0: Int, p1: Int) {
                    LogUtils.d("onRewardStepVerify p0${p0} p1${p1}")
                }

                override fun onExtraRewardVerify(p0: Int) {
                    LogUtils.d("onExtraRewardVerify ${p0}")
                }
            })

            //2.设置展示配置
            val videoPlayConfig = KsVideoPlayConfig.Builder()
                .showLandscape(false) // 横屏播放, 建议和当前屏幕方向保持一致
                .build()
            rewardVideoAd?.showRewardVideoAd(context, videoPlayConfig)
        }else{
            listener.onRewardVideoAdError("context is finish")
        }
    }

}