package com.keyboard.journey.ad

import android.app.Activity
import com.applovin.mediation.MaxAd
import com.applovin.mediation.MaxAdListener
import com.applovin.mediation.MaxError

class MaxAdInstShower {
    private var mPlace: String
    private var showListener: AdShowListener? = null
    private var activity: Activity? = null

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

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

    private fun init() {
        val interstitialAd = InstAdCacheManager.instance.getAdCache(mPlace)
        interstitialAd?.setListener(object : MaxAdListener {
            override fun onAdLoaded(p0: MaxAd) {
                // Handle ad loaded event
            }

            override fun onAdDisplayed(p0: MaxAd) {
                // Handle ad displayed event
                showListener?.onAdShown()
            }

            override fun onAdHidden(p0: MaxAd) {
                // Handle ad hidden event
                showListener?.onAdClosed()
            }

            override fun onAdClicked(p0: MaxAd) {
                // Handle ad clicked event
            }

            override fun onAdLoadFailed(p0: String, p1: MaxError) {
                // Handle ad load failure event
                showListener?.onAdShowFailed(AdShowFailed(p1.message))
            }

            override fun onAdDisplayFailed(p0: MaxAd, p1: MaxError) {
                // Handle ad display failure event
            }
        })
        interstitialAd?.showAd()
    }
}