package com.offline.music.player.max;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxAdListener;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.ads.MaxInterstitialAd;
import com.applovin.mediation.nativeAds.MaxNativeAdListener;
import com.applovin.mediation.nativeAds.MaxNativeAdLoader;
import com.applovin.mediation.nativeAds.MaxNativeAdView;
import com.offline.music.player.MusicApplication;


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

public class MaxManager {



    public static final String AD_TAG = "------MAX---------";


    /**
     * 原生广告
     */
    //small
    public static final String native_Ad1 = "318a8ed92bb2596d";
    //small
    public static final String native_Ad2 = "63e3d50c36c627d2";
    /**
     * 插页广告
     */
    private static final String one_AD = "6510769676690c4d";
    private static final String two_Ad = "7c6b55091a9aaf39";
    private static final String three_ad = "39c6f571b5939dde";


    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()) {
                Log.d(AD_TAG, "isReady");
                return ad;
            }
        }
        Log.d(AD_TAG, "no  Ready");
        return null;
    }

    public static void onLoadNativeAd(ViewGroup nativeAdContainer, String adPlace, NativeMaxListener listener) {
        MaxNativeAdLoader nativeAdLoader;

        nativeAdLoader = new MaxNativeAdLoader(adPlace, MusicApplication.myApplication);
        nativeAdLoader.setNativeAdListener(new MaxNativeAdListener() {
            MaxAd nativeAd;
            @Override
            public void onNativeAdLoaded(@Nullable MaxNativeAdView maxNativeAdView, @NonNull MaxAd maxAd) {
                // Clean up any pre-existing native ad to prevent memory leaks.
                if (nativeAd != null) {
                    nativeAdLoader.destroy(nativeAd);
                }

                // Save ad for cleanup.
                nativeAd = maxAd;

                // Add ad view to view.
                nativeAdContainer.removeAllViews();
                nativeAdContainer.addView(maxNativeAdView);
                nativeAdContainer.setVisibility(View.VISIBLE);
                listener.onLoaded(maxAd,nativeAdLoader);
                Log.d(AD_TAG, "-------onNativeAdLoaded-----maxAd=" + maxAd.getAdUnitId());
            }

            @Override
            public void onNativeAdLoadFailed(@NonNull String s, @NonNull MaxError maxError) {
                super.onNativeAdLoadFailed(s, maxError);
                Log.d(AD_TAG, "-------onNativeAdLoadFailed-----maxAd=" + maxError.getMessage());
            }
        });
        nativeAdLoader.loadAd();
    }

    public static void clearNativeAd(MaxAd nativeAd, MaxNativeAdLoader nativeAdLoader) {
        if (nativeAd != null) {
            nativeAdLoader.destroy(nativeAd);
        }
        nativeAdLoader.destroy();
    }



    public static List<MaxInterstitialAd> onLoadAd() {
        if (adList.isEmpty()) {
            MaxInterstitialAd AdT = new MaxInterstitialAd(two_Ad, MusicApplication.myApplication);
            MaxInterstitialAd AdOne = new MaxInterstitialAd(one_AD, MusicApplication.myApplication);
            MaxInterstitialAd AdThree = new MaxInterstitialAd(three_ad, MusicApplication.myApplication);
            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();
                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();
                }
            }
        });
    }

}
