package com.input.board.appskey.keyboard.unityad;

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

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.input.board.appskey.keyboard.KeyBoardApp;
import com.unity3d.ads.IUnityAdsLoadListener;
import com.unity3d.ads.IUnityAdsShowListener;
import com.unity3d.ads.UnityAds;
import com.unity3d.ads.UnityAdsShowOptions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;

public class UnityAdManager {


    public static String unitAd1 = "Nova_keyboard_ad_unity_inst_open";
    public static String unitAd2 = "Nova_keyboard_ad_unity_inst_set";
    public static String unitAd3 = "Nova_keyboard_ad_unity_inst_theme";


    public static String TAG = "--------";



    public static Boolean AD1Ready = false;
    public static Boolean AD2Ready = false;
    public static Boolean AD3Ready = false;








    public static void loadAllAdNew() {
        if (!AD1Ready)
            loadAd(unitAd1);
        if (!AD2Ready)
            loadAd(unitAd2);
        if (!AD3Ready)
            loadAd(unitAd3);
    }

    public static String getReadyUnitAd() {
        ArrayList<String> unitList = new ArrayList<>();
        unitList.add(unitAd1);
        unitList.add(unitAd2);
        unitList.add(unitAd3);
        Collections.shuffle(unitList);

        for (String unit : unitList) {
            if (Objects.equals(unit, unitAd1)) {
                if (AD1Ready) {
                    return unit;
                }
            } else if (Objects.equals(unit, unitAd2)) {
                if (AD2Ready) {
                    return unit;
                }
            } else if (Objects.equals(unit, unitAd3)) {
                if (AD3Ready) {
                    return unit;
                }
            }
        }
        return null;
    }

    public static void loadAd(String adUnitId) {
        UnityAds.load(adUnitId, new IUnityAdsLoadListener() {
            @Override
            public void onUnityAdsAdLoaded(String placementId) {
                Log.d(TAG, "------loaded-unit" + placementId);
                if (Objects.equals(placementId, unitAd1)) {
                    AD1Ready = true;
                } else if (Objects.equals(placementId, unitAd2)) {
                    AD2Ready = true;
                } else if (Objects.equals(placementId, unitAd3)) {
                    AD3Ready = true;
                }

            }

            @Override
            public void onUnityAdsFailedToLoad(String placementId, UnityAds.UnityAdsLoadError error, String message) {
                Log.d(TAG, "------load fail-unit" + placementId + "-----" + message);
            }
        });

    }



    public static void showUnityAd(Activity activity, String adUnitId, onShowCallBack onShowCallBack) {
        UnityAds.show(activity, adUnitId, new UnityAdsShowOptions(), new IUnityAdsShowListener() {
            @Override
            public void onUnityAdsShowFailure(String placementId, UnityAds.UnityAdsShowError error, String message) {
                onShowCallBack.onShowFail();
                Log.d(TAG, "--------onUnityAdsShowFailure---message=" + message);
            }

            @Override
            public void onUnityAdsShowStart(String placementId) {
                Log.d(TAG, "--------onUnityAdsShowStart---=" + placementId);
                if (Objects.equals(placementId, unitAd1)) {
                    AD1Ready = false;
                } else if (Objects.equals(placementId, unitAd2)) {
                    AD2Ready = false;
                } else if (Objects.equals(placementId, unitAd3)) {
                    AD3Ready = false;
                }
                onShowCallBack.onShowSuccess();

            }

            @Override
            public void onUnityAdsShowClick(String placementId) {

            }

            @Override
            public void onUnityAdsShowComplete(String placementId, UnityAds.UnityAdsShowCompletionState state) {
                onShowCallBack.onShowClose();
                Log.d(TAG, "--------onUnityAdsShowComplete---");
                loadAd(placementId);
            }
        });
    }
}
