package com.prankster.fx.topon;

import android.app.Activity;
import android.os.CountDownTimer;
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.prankster.fx.FXApp;

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 = "63cb5af8a1d7975e";
    private static final String place2Id = "c666cb8a3c03bd9c";
    private static final String place3Id = "8b2dfd2a5f6df1e7";


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

    private static boolean alreadyShow = false;

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


    public static void loadAllAd() {
        if (list.size() == 0) {
            MaxInterstitialAd mInterstitialAd1 = new MaxInterstitialAd(place1Id, FXApp.app);
            MaxInterstitialAd mInterstitialAd2 = new MaxInterstitialAd(place2Id, FXApp.app);
            MaxInterstitialAd mInterstitialAd3 = new MaxInterstitialAd(place2Id, FXApp.app);
            list.add(mInterstitialAd1);
            list.add(mInterstitialAd2);
            list.add(mInterstitialAd3);
        }
        for (MaxInterstitialAd ad : list) {
            if (!ad.isReady()) {
                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.loadAd();
            }
        }


    }

    private static MaxInterstitialAd getReadyAd() {
        Collections.shuffle(list);
        for (MaxInterstitialAd ad : list) {
            if (ad.isReady()) {
                Log.d(FXApp.TAG, "-has Cache------------");
                return ad;
            }
        }
        Log.d(FXApp.TAG, "-No Cache------------");
        return null;
    }

    private static void setCallBack(MaxInterstitialAd ad, AdListener listener) {
        ad.setListener(new MaxAdListener() {
            @Override
            public void onAdLoaded(@NonNull MaxAd maxAd) {
                Log.d(FXApp.TAG, "LoadLoaded   " + ad.getAdUnitId());
            }

            @Override
            public void onAdDisplayed(@NonNull MaxAd maxAd) {
                Log.d(FXApp.TAG, "AdShow " + maxAd.getAdUnitId());
                listener.showSuccess();
                ad.loadAd();
            }

            @Override
            public void onAdHidden(@NonNull MaxAd maxAd) {
                listener.showClose();
            }

            @Override
            public void onAdClicked(@NonNull MaxAd maxAd) {

            }

            @Override
            public void onAdLoadFailed(@NonNull String s, @NonNull MaxError maxError) {
                listener.loadFail(ad.getAdUnitId());
                Log.d(FXApp.TAG, "LoadFail:--" + ad.getAdUnitId() + "--" + maxError.getCode() + "---" + maxError.getMessage());
            }

            @Override
            public void onAdDisplayFailed(@NonNull MaxAd maxAd, @NonNull MaxError maxError) {

            }
        });
    }


    public static CountDownTimer showWelcomeAd(
            Activity activity,
            Long totalTim,
            CountAction countAction,
            GoMainAction goMain
    ) {
        Log.d(FXApp.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 (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) {
        MaxInterstitialAd 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.showAd(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();
                }
            }
        });

    }
}
