package com.colorful.keyboard.theme.ad

import android.app.Activity
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.AdError
import com.anythink.interstitial.api.ATInterstitialListener
import com.colorful.keyboard.theme.App

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 atInterstitial = InstAdCacheManager.instance.getAdCache(mPlace)
        atInterstitial?.setAdListener(object : ATInterstitialListener {
            override fun onInterstitialAdLoaded() {

            }

            override fun onInterstitialAdLoadFail(p0: AdError?) {

            }

            override fun onInterstitialAdClicked(p0: ATAdInfo?) {
                showListener?.onAdClicked()
            }

            override fun onInterstitialAdShow(p0: ATAdInfo?) {
                showListener?.onAdShown(p0)
                App.app.lastAdDisplayTime.set(System.currentTimeMillis())
            }

            override fun onInterstitialAdClose(p0: ATAdInfo?) {
                showListener?.onAdClosed()
            }

            override fun onInterstitialAdVideoStart(p0: ATAdInfo?) {

            }

            override fun onInterstitialAdVideoEnd(p0: ATAdInfo?) {

            }

            override fun onInterstitialAdVideoError(adError: AdError?) {
                showListener?.onAdShowFailed(AdShowFailed(adError.toString()))
            }

        })
        if (atInterstitial?.isAdReady == true) {
            atInterstitial.show(activity)
        }
    }
}