package com.ar.sketch.drawing.ad;

import android.app.Activity;
import android.os.CountDownTimer;
import android.util.Log;

import androidx.annotation.NonNull;

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.ar.sketch.drawing.MyApp;


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

public class AdManager {

    private static final int type_no_cache = 0;
    private static final int type_has_cache = 1;
    private static final int type_show_success = 2;
    private static final int type_show_close = 3;
    private static final int type_show_fail = 4;


    private static final String place1Id = "n68070d4639067";
    private static final String place2Id = "n68070d1cde47a";
    private static final String place3Id = "n68070d0e966c3";


    private static boolean place1LoadFail = false;
    private static boolean place2LoadFail = false;
    private static boolean place3LoadFail = false;

    private static boolean alreadyShow = false;

    private static List<ATInterstitial> list = new ArrayList<>();


    private static String Ad_TAG = MyApp.TAG;
    public static void loadAllAd() {
        place1LoadFail = false;
        place2LoadFail = false;
        place3LoadFail = false;
        if (list.size() == 0) {
            ATInterstitial mInterstitialAd1 = new ATInterstitial(MyApp.app, place1Id);
            ATInterstitial mInterstitialAd2 = new ATInterstitial(MyApp.app, place2Id);
            ATInterstitial mInterstitialAd3 = new ATInterstitial(MyApp.app, place3Id);
            list.add(mInterstitialAd1);
            list.add(mInterstitialAd2);
            list.add(mInterstitialAd3);
        }
        for (ATInterstitial ad : list) {
            if (!ad.isAdReady()) {
                setCallBack(ad, new AdListener() {
                    @Override
                    public void loadFail(@NonNull String placeId) {
                        if (placeId.equals(place1Id)) {
                            place1LoadFail = true;
                        }
                        if (placeId.equals(place2Id)) {
                            place2LoadFail = true;
                        }
                        if (placeId.equals(place3Id)) {
                            place3LoadFail = true;
                        }
                    }

                    @Override
                    public void showSuccess() {

                    }

                    @Override
                    public void showFail() {

                    }

                    @Override
                    public void showClose() {

                    }
                });
                ad.load();
            }
        }


    }

    private static ATInterstitial getReadyAd() {
        Collections.shuffle(list);
        for (ATInterstitial ad : list) {
            if (ad.isAdReady()) {
                Log.d(Ad_TAG, "-has Cache------------");
                return ad;
            }
        }
        Log.d(Ad_TAG, "-No Cache------------");
        return null;
    }

    private static void setCallBack(ATInterstitial ad, AdListener listener) {
        ad.setAdListener(new ATInterstitialListener() {


            @Override
            public void onInterstitialAdLoaded() {
                Log.d(Ad_TAG, "LoadLoaded   " + ad.mPlacementId);
            }

            @Override
            public void onInterstitialAdLoadFail(AdError adError) {
                listener.loadFail(ad.mPlacementId);
                Log.d(Ad_TAG, "LoadFail:--" + ad.mPlacementId + "--" + adError.getCode() + "---" + adError.getDesc());
            }

            @Override
            public void onInterstitialAdClicked(ATAdInfo atAdInfo) {

            }

            @Override
            public void onInterstitialAdShow(ATAdInfo atAdInfo) {
                Log.d(Ad_TAG, "AdShow " + atAdInfo.getShowId());
                listener.showSuccess();
                ad.load();
            }

            @Override
            public void onInterstitialAdClose(ATAdInfo atAdInfo) {
                listener.showClose();
            }

            @Override
            public void onInterstitialAdVideoStart(ATAdInfo atAdInfo) {

            }

            @Override
            public void onInterstitialAdVideoEnd(ATAdInfo atAdInfo) {

            }

            @Override
            public void onInterstitialAdVideoError(AdError adError) {

            }
        });
    }




    public static CountDownTimer showWelcomeAd(
            Activity activity,
            Long totalTim,
            CountAction countAction,
            GoMainAction goMain
    ) {
        Log.d(Ad_TAG,"----------alreadyShow="+alreadyShow);
        alreadyShow = false;
        CountDownTimer timer = new CountDownTimer(totalTim, 100) {
            @Override
            public void onTick(long millisUntilFinished) {
                countAction.onCount(millisUntilFinished);
                if (!alreadyShow) {
                    showAD(activity, new AdStatusAction() {
                        @Override
                        public void onStatus(int adType) {
                            if(place1LoadFail&&place2LoadFail&&place3LoadFail){
                                Log.d(Ad_TAG,"------------all load fail");
                                alreadyShow = true;
                                goMain.onGo();
                            }
                            if (adType == type_has_cache) {
                                alreadyShow = true;
                            }
                            if (adType == type_show_close || adType == type_show_fail) {
                                goMain.onGo();
                            }

                        }
                    });
                }
            }

            @Override
            public void onFinish() {
                if (!alreadyShow) {
                    showTopOn(activity, new onActionListener() {
                        @Override
                        public void onAction() {
                            goMain.onGo();
                        }
                    });
                }
            }
        };


        return timer;
    }



    private static void showAD(Activity activity, AdStatusAction action) {
        ATInterstitial readyAd = getReadyAd();
        if (readyAd != null) {
            action.onStatus(type_has_cache);
            setCallBack(readyAd, new AdListener() {
                @Override
                public void loadFail(@NonNull String placeId) {

                }

                @Override
                public void showSuccess() {
                    action.onStatus(type_show_success);
                }

                @Override
                public void showFail() {
                    action.onStatus(type_show_fail);
                }

                @Override
                public void showClose() {
                    action.onStatus(type_show_close);
                }
            });
            readyAd.show(activity);
        } else {
            action.onStatus(type_no_cache);

        }
    }


    public static void showTopOn(Activity activity, onActionListener listener) {
        showAD(activity, new AdStatusAction() {
            @Override
            public void onStatus(int adType) {
                if (adType == type_no_cache || adType == type_show_close || adType == type_show_fail) {
                    listener.onAction();
                }
            }
        });

    }
}
