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.LevelPlayInterstitialAd
import com.unity3d.mediation.interstitial.LevelPlayInterstitialAdListener

class AdInstLoad {
    private var mPlace: String
    private var adLoadListener: LoadListener? = null
    private var activity: Activity? = null

    constructor(activity: Activity, place: String, listener: LoadListener?) {
        this.mPlace = place
        this.adLoadListener = listener
        this.activity = activity
        init()
    }

    constructor(place: String, listener: LoadListener?) {
        this.mPlace = place
        this.adLoadListener = listener
        init()
    }

    private fun init() {
        // Create the interstitial ad object
        val mInterstitialAd = LevelPlayInterstitialAd(mPlace)
        mInterstitialAd.setListener(object : LevelPlayInterstitialAdListener {
            override fun onAdLoaded(levelPlayAdInfo: LevelPlayAdInfo) {
                // Ad was loaded successfully
                InstAdCacheManager.instance.setAdCache(mPlace, mInterstitialAd)
                adLoadListener?.loaded(levelPlayAdInfo)
            }
            override fun onAdLoadFailed(levelPlayAdError: LevelPlayAdError) {
                // Ad load failed
                adLoadListener?.loadFailed("code->${levelPlayAdError.errorCode} message->${levelPlayAdError.errorMessage}")
                Log.d("ocean", "load ad onError-> code->${levelPlayAdError.errorCode} message->${levelPlayAdError.errorMessage}")
            }
            override fun onAdDisplayed(levelPlayAdInfo: LevelPlayAdInfo) {
                // Ad was displayed and visible on screen
            }
            override fun onAdDisplayFailed(levelPlayAdError: LevelPlayAdError, levelPlayAdInfo: LevelPlayAdInfo) {
                // Ad fails to be displayed
                // Optional
            }
            override fun onAdClicked(levelPlayAdInfo: LevelPlayAdInfo) {
                // Ad was clicked
                // Optional
            }
            override fun onAdClosed(levelPlayAdInfo: LevelPlayAdInfo) {
                // Ad was closed
                // Optional
            }
            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
            }
        })
        mInterstitialAd.loadAd()
    }
}