package com.input.board.appskey.keyboard.customer;

import android.app.Activity;

import androidx.annotation.NonNull;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxAdListener;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.ads.MaxInterstitialAd;
import com.input.board.appskey.keyboard.interf.onAdShowCallback;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class IntersitialAd {

    private static String unitAd1 = "06bd48bfaba9b6ec";
    private static String unitAd2 = "66a8f5e488c3adac";
    private static String unitAd3 = "16413a54ebedc530";



    private static MaxInterstitialAd createAd(String unitAd, Activity act) {
        MaxInterstitialAd maxInterAd = new MaxInterstitialAd(unitAd, act);
        if (!maxInterAd.isReady()) {
            maxInterAd.loadAd();
        }else {

        }
        return maxInterAd;
    }

    public static List<MaxInterstitialAd> loadInterAd(Activity act) {
        MaxInterstitialAd ad1 = createAd(unitAd1, act);
        MaxInterstitialAd ad2 = createAd(unitAd2, act);
        MaxInterstitialAd ad3 = createAd(unitAd3, act);
        List<MaxInterstitialAd> list = new ArrayList<>();
        list.add(ad1);
        list.add(ad2);
        list.add(ad3);
        Collections.shuffle(list);
        return list;
    }

    public static MaxInterstitialAd showInterAd(List<MaxInterstitialAd> ads) {
        Collections.shuffle(ads);
        for (int index = 0; index < ads.size(); index++) {
            MaxInterstitialAd interstitialAd = ads.get(index);
            if (interstitialAd.isReady()) {
                return interstitialAd;
            }
        }
        return null;
    }

    public static void setAdCallback(MaxInterstitialAd ad, onAdShowCallback showCallback) {
        ad.setListener(new MaxAdListener() {
            @Override
            public void onAdLoaded(@NonNull MaxAd maxAd) {

            }

            @Override
            public void onAdDisplayed(@NonNull MaxAd maxAd) {
                showCallback.displayed();
            }

            @Override
            public void onAdHidden(@NonNull MaxAd maxAd) {
                showCallback.hidden();
            }

            @Override
            public void onAdClicked(@NonNull MaxAd maxAd) {

            }

            @Override
            public void onAdLoadFailed(@NonNull String s, @NonNull MaxError maxError) {

            }

            @Override
            public void onAdDisplayFailed(@NonNull MaxAd maxAd, @NonNull MaxError maxError) {
                showCallback.displayFail();
            }
        });

    }
}
