package com.colorful.keyboard.theme.ad

import android.app.Activity
import android.util.Log
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.AdError
import com.anythink.interstitial.api.ATInterstitial
import com.anythink.interstitial.api.ATInterstitialListener

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() {

        val atInterstitial = ATInterstitial(activity, mPlace)
        atInterstitial.setAdListener(object : ATInterstitialListener {
            override fun onInterstitialAdLoaded() {
                val info = atInterstitial.checkAdStatus().atTopAdInfo

                InstAdCacheManager.instance.setAdCache(mPlace, atInterstitial)
                adLoadListener?.loaded(info)
            }

            override fun onInterstitialAdLoadFail(adError: AdError?) {
                adLoadListener?.loadFailed(adError)

                Log.d("ocean","onInterstitialAdLoadFail->${adError} ")

                Log.d("ocean","AdError.getFullErrorInfo()->${adError?.fullErrorInfo}")
            }

            override fun onInterstitialAdClicked(p0: ATAdInfo?) {

            }

            override fun onInterstitialAdShow(p0: ATAdInfo?) {

            }

            override fun onInterstitialAdClose(p0: ATAdInfo?) {

            }

            override fun onInterstitialAdVideoStart(p0: ATAdInfo?) {

            }

            override fun onInterstitialAdVideoEnd(p0: ATAdInfo?) {

            }

            override fun onInterstitialAdVideoError(p0: AdError?) {

            }

        })
        atInterstitial.load()
    }
}