package com.ym.admodule.bd

import android.app.Activity
import android.os.Handler
import android.os.Message
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.baidu.mobads.*
import com.baidu.mobads.rewardvideo.RewardVideoAd
import com.ym.admodule.config.*
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.AdEventUtils.AD_MODEL_REWARD
import com.ym.modulecommon.utils.AdEventUtils.AD_MODEL_SPLAH

object BDADUtils {
    private const val TAG = "AdManager"
    //开屏
    fun loadSplashAd(newsEntity: NewsEntity, activity: Activity, container: ViewGroup, zxadListener: ZXADSplashListener?, skipView: TextView?,zxadCallback: ZXADCallback) {
        BaiduManager.init(activity)
        container.visibility = View.VISIBLE
        val listener: SplashLpCloseListener = object : SplashLpCloseListener {
            override fun onLpClosed() {
                zxadListener?.onAdSkip()
            }

            override fun onAdDismissed() {
                zxadListener?.onAdSkip()
            }

            override fun onAdFailed(arg0: String) {
                AdEventUtils.onEventPullFail(newsEntity,AdEventUtils.AD_MODEL_SPLAH)
                AdEventUtils.onEvent(newsEntity,"",arg0)
                zxadListener?.onError(arg0+"")
                zxadCallback.onAdFail()
            }

            override fun onAdPresent() {
                container.setVisibility(View.GONE)
            }

            override fun onAdClick() {
                AdEventUtils.onEventClick(newsEntity, AD_MODEL_SPLAH)
            }
        }
        val adPlaceId = newsEntity.codeId // 重要：请填上您的广告位ID，代码位错误会导致无法请求到广告
        SplashAd(activity, container, listener, adPlaceId, true)
        AdEventUtils.onEventShow(newsEntity)
    }


    //插屏广告
    fun loadInteractionAd(
        activity: Activity,
        newsEntity: NewsEntity,
        callback: ZXADCallback
    ) {
        var mInterAd = InterstitialAd(activity, newsEntity.codeId)
        mInterAd.setListener(object :InterstitialAdListener{
            override fun onAdFailed(p0: String?) {
                callback.onAdFail()
            }

            override fun onAdDismissed() {
            }

            override fun onAdPresent() {
            }

            override fun onAdClick(p0: InterstitialAd?) {
            }

            override fun onAdReady() {
                mInterAd.showAd(activity)
            }

        })
        //加载广告
        mInterAd.loadAd()
    }

    //激励视频
    fun loadRewardVideoAd(
        activity: Activity,
        newsEntity: NewsEntity,
        listener: ZXADVideoListener,
        callback: ZXADCallback
    ) {
        var mRewardVideoAd = RewardVideoAd(
            activity,
            newsEntity.codeId, object :RewardVideoAd.RewardVideoAdListener{
                override fun onAdFailed(p0: String?) {
                    AdEventUtils.onEventPullFail(newsEntity,AdEventUtils.AD_MODEL_REWARD)
                    AdEventUtils.onEvent(newsEntity,"",p0?:"")
                    callback.onAdFail()
                }

                override fun playCompletion() {
                }

                override fun onAdShow() {
                    AdEventUtils.onEventShow(newsEntity)
                    callback.onAdSuccess()
                    listener.onAdShow()
                }

                override fun onAdClick() {
                    AdEventUtils.onEventClick(newsEntity,AD_MODEL_REWARD)
                }

                override fun onAdClose(p0: Float) {
                    listener.onAdClose(AdInfo(newsEntity.codeId))
                }

                override fun onVideoDownloadSuccess() {
                }
                override fun onVideoDownloadFailed() {
                    listener.onError("百度 VideoDownloadFailed")
                }

            }, true)
        mRewardVideoAd.load()
        var startTime = System.currentTimeMillis()
        val handler = object : Handler() {
            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                if(System.currentTimeMillis()-startTime<3000){
                    if (mRewardVideoAd != null) {
                        mRewardVideoAd.show()
                        removeCallbacksAndMessages(null)
                    } else{
                        sendEmptyMessageDelayed(1,20)
                    }
                }else{
                    callback.onAdFail()
                    AdEventUtils.onEventPullFail(newsEntity,AdEventUtils.AD_MODEL_REWARD)
                    removeCallbacksAndMessages(null)
                }
            }

        }
        handler.sendEmptyMessageDelayed(1,20)
    }
}