package com.keyboard.journey.ad

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

class MaxAdInstLoad {
    private var mPlace: String
    private var adLoadListener: AdLoadListener? = null
    private var activity: Activity? = null

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

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

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

            override fun onAdDisplayed(p0: MaxAd) {
                // Handle ad displayed event
            }

            override fun onAdHidden(p0: MaxAd) {
                // Handle ad hidden event
            }

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

            override fun onAdLoadFailed(p0: String, p1: MaxError) {
                // Handle ad load failure event
                adLoadListener?.loadFailed(p1)
            }

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