package com.prank.funny.pranks.max;

import android.app.Activity;
import android.util.Log;

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.prank.funny.pranks.FunnyApp;

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

public class MaxManager {

    private static final String one_AD = "889e0fa22ec24651";
    private static final String two_Ad = "65a283ca58749b4d";
    private static final String three_ad = "b1b772652371cee1";


    public static final int type_no_cache = 0;
    public static final int type_has_cache = 1;
    public static final int type_show_success = 2;
    public static final int type_show_close = 3;
    public static final int type_show_fail = 4;

    private static List<MaxInterstitialAd> adList = new ArrayList<>();

    public static MaxInterstitialAd getAd(List<MaxInterstitialAd> list) {
        Collections.shuffle(list);
        for (MaxInterstitialAd ad : list) {
            if (ad.isReady()) {
                return ad;
            }
        }
        return null;
    }

    public static List<MaxInterstitialAd> onLoadAd() {
        if (adList.isEmpty()) {
            MaxInterstitialAd AdT = new MaxInterstitialAd(two_Ad, FunnyApp.app);
            MaxInterstitialAd AdOne = new MaxInterstitialAd(one_AD, FunnyApp.app);
            MaxInterstitialAd AdThree = new MaxInterstitialAd(three_ad, FunnyApp.app);
            adList.add(AdOne);
            adList.add(AdT);
            adList.add(AdThree);
        }

        for (MaxInterstitialAd ad : adList) {
            if (!ad.isReady()) {
                setMyListener(ad, new MaxListener() {
                    @Override
                    public void onFail(MaxAd ad) {

                    }

                    @Override
                    public void onShowSuccess(MaxAd ad) {

                    }

                    @Override
                    public void onHidden() {

                    }
                });
                ad.loadAd();
            }

        }

        return adList;
    }

    public static void setMyListener(MaxInterstitialAd ad, MaxListener maxListener) {
        ad.setListener(new MaxAdListener() {
            @Override
            public void onAdLoaded(@NonNull MaxAd maxAd) {
                Log.d(FunnyApp.TAG, "-------onAdLoaded-----maxAd=" + maxAd.getAdUnitId());
            }

            @Override
            public void onAdDisplayed(@NonNull MaxAd maxAd) {
                Log.d(FunnyApp.TAG, "-------onAdDisplayed-----maxAd=" + maxAd.getAdUnitId());
                maxListener.onShowSuccess(maxAd);
            }

            @Override
            public void onAdHidden(@NonNull MaxAd maxAd) {
                maxListener.onHidden();
                setMyListener(ad, new MaxListener() {
                    @Override
                    public void onFail(MaxAd ad) {

                    }

                    @Override
                    public void onShowSuccess(MaxAd ad) {

                    }

                    @Override
                    public void onHidden() {

                    }
                });
                ad.loadAd();
            }

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

            }

            @Override
            public void onAdLoadFailed(@NonNull String s, @NonNull MaxError maxError) {
                Log.d(FunnyApp.TAG, "-------onAdLoadFailed-----s=" + s+"----maxError="+maxError.getMessage());
            }

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

    }

    protected static void ShowAd(Activity activity, onAdStatusListener listener) {
        MaxInterstitialAd ad = MaxManager.getAd(adList);
        if (ad == null) {
            listener.onAdStatus(type_no_cache);
        } else {
            listener.onAdStatus(type_has_cache);
            MaxManager.setMyListener(ad, new MaxListener() {
                @Override
                public void onFail(MaxAd ad) {
                    listener.onAdStatus(type_show_fail);
                }

                @Override
                public void onShowSuccess(MaxAd ad) {
                    listener.onAdStatus(type_show_success);
                }

                @Override
                public void onHidden() {
                    listener.onAdStatus(type_show_close);
                }
            });
            ad.showAd(activity);
        }
    }


    public static void startShowMaxAd(Activity activity, onAdAfterAction listener) {
        MaxManager.ShowAd(activity, new onAdStatusListener() {
            @Override
            public void onAdStatus(int type) {
                if (type == MaxManager.type_show_close || type == MaxManager.type_show_fail || type == MaxManager.type_no_cache) {
                    if (listener != null)
                        listener.onAction();
                }
            }
        });
    }

}
