package com.quiescent.wallpaper.collection.test

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

object InterstitialManager {

    private const val unit1 = "e4c638b430ed484e"
    private const val unit2 = "136732916feb53dc"
    private const val unit3 = "9f5d8ac0162a29bc"
//private const val unit1 = "4c0e7c1b292301ab"
//    private const val unit2 = "4c0e7c1b292301a2"
//    private const val unit3 = "4c0e7c1b292301a1"



    fun startLoadAd(context: Activity): List<MaxInterstitialAd> {
        return listOf(
            MaxInterstitialAd(unit1, context).apply {
                loadAd()
            },
            MaxInterstitialAd(unit2, context).apply {
                loadAd()
            },
            MaxInterstitialAd(unit3, context).apply {
                loadAd()
            }
        )

    }

    fun getReadyAd(adList: List<MaxInterstitialAd>): MaxInterstitialAd? {
        val shuffled = adList.shuffled()
        for (ad in shuffled) {
            if (ad.isReady) {
                return ad
            }
        }
        return null

    }

    fun MaxInterstitialAd.setMyCallBack(myCallBack: MyCallBack) {
        setListener(object :MaxAdListener{
            override fun onAdLoaded(p0: MaxAd) {

            }

            override fun onAdDisplayed(p0: MaxAd) {

            }

            override fun onAdHidden(p0: MaxAd) {
                myCallBack.adShowHidden()
            }

            override fun onAdClicked(p0: MaxAd) {

            }

            override fun onAdLoadFailed(p0: String, p1: MaxError) {

            }

            override fun onAdDisplayFailed(p0: MaxAd, p1: MaxError) {
                myCallBack.adShowFail()
            }

        })

    }

    interface  MyCallBack{
        fun adShowFail()
        fun adShowHidden()
    }

}