package com.example.applock.value;

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

import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.anythink.interstitial.api.ATInterstitialListener;
import com.example.applock.application.SoftWareLockApplication;

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

public class Mytool {
    private static final String one_AD = "n66a248aa03c0e";
    private static final String two_Ad = "n66a24886778dc";
    private static final String three_ad = "n66a2485a4f7d4";
    private static ArrayList<ATInterstitial> adArrayList;


    public static void setCallback(ATInterstitial ad, Adcallback adcallback) {
        ad.setAdListener(new ATInterstitialListener() {
            @Override
            public void onInterstitialAdLoaded() {
                Log.e("zzj", "---------onInterstitialAdLoaded--------" + ad.mPlacementId);
            }

            @Override
            public void onInterstitialAdLoadFail(AdError adError) {
                Log.e("zzj", "---------onInterstitialAdLoadFail--------" + adError.getFullErrorInfo());
                adcallback.onShowFail(adError);
            }

            @Override
            public void onInterstitialAdClicked(ATAdInfo atAdInfo) {

            }

            @Override
            public void onInterstitialAdShow(ATAdInfo atAdInfo) {
                ad.load();
            }

            @Override
            public void onInterstitialAdClose(ATAdInfo atAdInfo) {
                adcallback.onAdHidden();
            }

            @Override
            public void onInterstitialAdVideoStart(ATAdInfo atAdInfo) {

            }

            @Override
            public void onInterstitialAdVideoEnd(ATAdInfo atAdInfo) {

            }

            @Override
            public void onInterstitialAdVideoError(AdError adError) {

            }
        });
    }

    public static ATInterstitial onCache(List<ATInterstitial> list) {
        /*Collections.shuffle() 是 Java 中的一个方法，用于随机打乱指定列表中的元素1*/
        Collections.shuffle(list);
        for (ATInterstitial ad : list) {
            if (ad.isAdReady()) {
                return ad;
            }
        }
        return null;
    }

    public static List<ATInterstitial> getAllAd() {
        if (adArrayList == null) {
            adArrayList = new ArrayList<>();
            adArrayList.add(createAd(one_AD, SoftWareLockApplication.app));
            adArrayList.add(createAd(two_Ad, SoftWareLockApplication.app));
            adArrayList.add(createAd(three_ad, SoftWareLockApplication.app));
        }
        for (ATInterstitial ad : adArrayList) {
            if (!ad.isAdReady()) {
                setCallback(ad, new Adcallback() {
                    @Override
                    public void onShowFail(AdError ad) {

                    }

                    @Override
                    public void onAdHidden() {

                    }
                });
                ad.load();
            }
        }
        return adArrayList;
    }

    private static ATInterstitial createAd(String adUnitId, Context context) {
        ATInterstitial ad = new ATInterstitial(context, adUnitId);
        return ad;
    }


    public static void showAd(Activity activity,onActionCallback callback) {
        ATInterstitial mInterstitialAd = Mytool.onCache(adArrayList);
        if (mInterstitialAd == null) {
            callback.onAction();
        } else {
            Mytool.setCallback(mInterstitialAd, new Adcallback() {
                @Override
                public void onShowFail(AdError ad) {
                    callback.onAction();
                }

                @Override
                public void onAdHidden() {
                    callback.onAction();
                }
            });
            mInterstitialAd.show(activity);
        }

    }
}
