package com.lc.myapplication.security.locks.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.lc.myapplication.security.locks.Securitylocks;
import com.lc.myapplication.security.locks.value.MeValues;

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

public class MaxManager {
    /**
     d642099557241756
     599cd93d978a2f31
     40f5a50c609f9280
     */
    private static final String one_AD = "d642099557241756";
    private static final String two_Ad = "599cd93d978a2f31";
    private static final String three_ad = "40f5a50c609f9280";


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

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

            @Override
            public void onAdHidden(@NonNull MaxAd maxAd) {
                Log.d(Securitylocks.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(Securitylocks.TAG, "-------onAdLoadFailed-----s="+s+"-----maxError="+maxError.getCode()+"----="+maxError.getMessage());
            }

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

    }

    public 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);
        }
    }


}
