package com.ar.sketch.paint.drawing.ad;

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.ar.sketch.paint.drawing.tool.MyArApp;


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

public class MaxManager {

    private static final String one_AD = "7c4b34325d69456c";
    private static final String two_Ad = "f1ada735b400f486";
    private static final String three_ad = "12f02849b311236f";


    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 final String Ad_TAG = MyArApp.TAG;
    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, MyArApp.app);
            MaxInterstitialAd AdOne = new MaxInterstitialAd(one_AD, MyArApp.app);
            MaxInterstitialAd AdThree = new MaxInterstitialAd(three_ad, MyArApp.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(Ad_TAG, "-------onAdLoaded-----maxAd=" + maxAd.getAdUnitId());
            }

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

            @Override
            public void onAdHidden(@NonNull MaxAd maxAd) {
                Log.d(Ad_TAG, "-------onAdHidden-----maxAd=" + maxAd.getAdUnitId());
                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(Ad_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();
                }
            }
        });
    }

}
