package com.gallery.free.wallpaper.environment.ad

import android.app.Activity
import android.util.Log
import com.unity3d.mediation.LevelPlayAdError
import com.unity3d.mediation.LevelPlayAdInfo
import com.unity3d.mediation.interstitial.LevelPlayInterstitialAdListener
import com.gallery.free.wallpaper.environment.ad.async.Async

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.instance.getAdCache(mPlace)
        interstitialAd?.setListener(object : LevelPlayInterstitialAdListener{
            override fun onAdLoaded(levelPlayAdInfo: LevelPlayAdInfo) {
                // Ad was loaded successfully
            }
            override fun onAdLoadFailed(levelPlayAdError: LevelPlayAdError) {
                // Ad load failed
            }
            override fun onAdDisplayed(levelPlayAdInfo: LevelPlayAdInfo) {
                // Ad was displayed and visible on screen
                showListener?.onAdShown(levelPlayAdInfo)
                Log.d("ocean", "onAdDisplayed 广告展示回调")
                autoClose()
            }
            override fun onAdDisplayFailed(levelPlayAdError: LevelPlayAdError, levelPlayAdInfo: LevelPlayAdInfo) {
                // Ad fails to be displayed
                Log.d("ocean", "AdInstShower 视频广告播放失败回调->${levelPlayAdError.errorCode} ${levelPlayAdError.errorMessage}")
                showListener?.onAdShowFailed(AdShowFailed("${levelPlayAdError.errorCode} ${levelPlayAdError.errorMessage}"))
            }
            override fun onAdClicked(levelPlayAdInfo: LevelPlayAdInfo) {
                // Ad was clicked
                showListener?.onAdClicked()
                Log.d("ocean", "onAdClicked 广告点击回调")
            }
            override fun onAdClosed(levelPlayAdInfo: LevelPlayAdInfo) {
                // Ad was closed
                showListener?.onAdClosed()
                Log.d("ocean", "onAdClosed 广告关闭回调")
            }
            override fun onAdInfoChanged(levelPlayAdInfo: LevelPlayAdInfo) {
                // Called after the ad info is updated. Available when another interstitial ad has loaded, and includes a higher CPM/Rate
                // Optional
            }
        })
        interstitialAd?.showAd(activity!!)
    }

    private fun autoClose() {
        val autoCloseTime = 3000L
        Log.d("ocean-brush","show后开始等待自动关闭广告 $autoCloseTime")
        Async.scheduleTaskOnUiThread(autoCloseTime, Runnable {
            AdActivityManager.instance.doClose()
            Log.d("ocean-brush","show后执行关闭广告 $autoCloseTime")
        })
    }
}