package com.keyboard.craft.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.keyboard.craft.CraftApp;


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

public class MaxManager {

    private static final String one_AD = "6217952675c49896";
    private static final String two_Ad = "0e4283e22b286755";
    private static final String three_ad = "acb1fb42b3034b0a";


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

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

            @Override
            public void onAdHidden(@NonNull MaxAd maxAd) {
                Log.d(CraftApp.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(CraftApp.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, type -> {
            Log.d("CraftApp","type="+type);
            if (type == MaxManager.type_show_close || type == MaxManager.type_show_fail || type == MaxManager.type_no_cache) {
                if (listener != null)
                    listener.onAction();
            }
        });
    }

}
