package com.artisanic.helper.ad

import android.app.Activity
import android.util.Log
import com.tradplus.ads.base.bean.TPAdError
import com.tradplus.ads.base.bean.TPAdInfo
import com.tradplus.ads.open.interstitial.InterstitialAdListener

class AdInstShower {
    private var mPlace: String
    private var showListener: ShowListener? = null
    private var activity: Activity? = null

    constructor(activity: Activity, place: String, showListener: ShowListener?) {
        this.mPlace = place
        this.showListener = showListener
        this.activity = activity
        init()
    }

    constructor(place: String, showListener: ShowListener?) {
        this.mPlace = place
        this.showListener = showListener
        init()
    }

    private fun init() {
        val interstitialAd = InstAdCacheManager.Companion.instance.getAdCache(mPlace)
        interstitialAd?.setAdListener(object : InterstitialAdListener {
            //广告加载完成 首个广告源加载成功时回调 一次加载流程只会回调一次
            override fun onAdLoaded(tpAdInfo: TPAdInfo?) {}

            // 广告被点击
            override fun onAdClicked(tpAdInfo: TPAdInfo?) {
                showListener?.onAdClicked()
                Log.d("ocean", "AdInstShower 广告点击回调")
            }

            // 广告成功展示在页面上
            override fun onAdImpression(tpAdInfo: TPAdInfo?) {
                showListener?.onAdShown(tpAdInfo)
                Log.d("ocean", "AdInstShower 广告展示回调")
            }

            // 广告加载失败
            override fun onAdFailed(error: TPAdError?) {}

            // 广告被关闭
            override fun onAdClosed(tpAdInfo: TPAdInfo?) {
                showListener?.onAdClosed()
                Log.d("ocean", "AdInstShower 广告关闭回调")
            }

            // 视频播放开始（部分广告源支持）
            override fun onAdVideoStart(tpAdInfo: TPAdInfo?) {}

            //视频播放结束（部分广告源支持）
            override fun onAdVideoEnd(tpAdInfo: TPAdInfo?) {}

            //视频播放失败（部分广告源支持）
            override fun onAdVideoError(tpAdInfo: TPAdInfo?, error: TPAdError?) {
                Log.d("ocean", "AdInstShower 视频广告播放失败回调->${error}")
                showListener?.onAdShowFailed(AdShowFailed(error?.errorMsg.toString()))
            }
        })
        interstitialAd?.showAd(activity!!, mPlace)
    }

}