package com.g12.capacitor.admob.ads;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.OrientationEventListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.AppCompatButton;

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.OnUserEarnedRewardListener;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
import com.google.android.gms.ads.nativead.NativeAd;
import com.google.android.gms.ads.nativead.NativeAdOptions;
import com.google.android.gms.ads.nativead.NativeAdView;
import com.google.android.gms.ads.rewarded.RewardItem;
import com.google.android.gms.ads.rewarded.RewardedAd;
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback;
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd;
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.json.JSONArray;

@CapacitorPlugin(name = "AdmobAds")
public class AdmobAdsPlugin extends Plugin {
    LinearLayout bLinearLayout;
    AdLoader adLoader;
    LinearLayout linearLayout;
    RewardedAd mRewardedAd;
    InterstitialAd mInterstitialAd;
    RewardedInterstitialAd mRewardedInterstitialAd;
    AdView adView;
    int prevOrientation = 0;
    String bannerAdPosition = "bottom";
    Boolean bannerAdHidden = false;

    private AdmobAds implementation = new AdmobAds();

    @SuppressLint("ResourceType")
    @Override
    public void load() {
        linearLayout = new LinearLayout(getContext());
        linearLayout.setId(254398);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        bLinearLayout = new LinearLayout(getContext());
        LinearLayout.LayoutParams bParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
        );
        bLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

        getActivity()
                .runOnUiThread(
                        new Runnable() {
                            @Override
                            public void run() {
                                MobileAds.initialize(getContext(), new OnInitializationCompleteListener() {
                                    @Override
                                    public void onInitializationComplete(InitializationStatus initializationStatus) {

                                    }
                                });
                                getActivity().addContentView(linearLayout, params);
                                getActivity().addContentView(bLinearLayout, bParams);
                            }
                        }
                );

//    checkOrientationChange();
    }

    @SuppressLint("ResourceType")
    @PluginMethod
    public void loadNativeAd(PluginCall call) {
        String adId = null;
        Boolean isTestAd = call.getBoolean("isTesting");
        int adsCount = call.getInt("adsCount");

        if (isTestAd) {
            adId = "ca-app-pub-3940256099942544/2247696110";
        } else {
                adId = call.getString("adId");            
        }

        if (adsCount == 0) {
            adsCount = 1;
        } else if (adsCount > 5) {
            adsCount = 5;
        }

        JSObject res = new JSObject();
        ArrayList<Map> adsArray = new ArrayList<>();

        adLoader =
                new AdLoader.Builder(getContext(), adId)
                        .forNativeAd(
                                new NativeAd.OnNativeAdLoadedListener() {
                                    @SuppressLint("ResourceType")
                                    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
                                    @Override
                                    public void onNativeAdLoaded(NativeAd nativeAd) {
                                        //                           Toast.makeText(getContext(), "HEADLINE " + nativeAd.getAdvertiser(), Toast.LENGTH_SHORT).show();
                                        Map<String, String> map = new HashMap<String, String>();

                                        if (nativeAd.getHeadline() != null) {
                                            map.put("headline", nativeAd.getHeadline());
                                        }

                                        if (nativeAd.getBody() != null) {
                                            map.put("body", nativeAd.getBody());
                                        }

                                        if (nativeAd.getIcon() != null) {
                                            map.put("icon", nativeAd.getIcon().getUri().toString());
                                        }

                                        if (nativeAd.getAdvertiser() != null) {
                                            map.put("advertiser", nativeAd.getAdvertiser());
                                        }

                                        if (nativeAd.getImages() != null) {
                                            if (nativeAd.getImages().size() > 0) {
                                                map.put("cover", nativeAd.getImages().get(0).getUri().toString());
                                            } else {
                                                map.put("cover", "");
                                            }
                                        }

                                        if (nativeAd.getCallToAction() != null) {
                                            map.put("cta", nativeAd.getCallToAction());
                                        }

                                        LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
                                                LinearLayout.LayoutParams.MATCH_PARENT,
                                                LinearLayout.LayoutParams.WRAP_CONTENT
                                        );

                                        NativeAdView templateView = new NativeAdView(getContext());
                                        int id = gen();
                                        templateView.setId(id);
                                        templateView.setLayoutParams(lparams);

                                        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                        inflater.inflate(R.layout.small_template, templateView, true);

                                        AppCompatButton CTABtn = templateView.findViewById(R.id.cta);

                                        CTABtn.setText(nativeAd.getCallToAction());

                                        templateView.setCallToActionView(CTABtn);
                                        templateView.setNativeAd(nativeAd);

                                        templateView.setLayoutParams(new LinearLayout.LayoutParams(1, 1));

                                        new Handler(Looper.getMainLooper())
                                                .postDelayed(
                                                        new Runnable() {
                                                            public void run() {
                                                                templateView.setLayoutParams(
                                                                        new LinearLayout.LayoutParams(
                                                                                LinearLayout.LayoutParams.MATCH_PARENT,
                                                                                LinearLayout.LayoutParams.WRAP_CONTENT
                                                                        )
                                                                );
                                                                templateView.setVisibility(View.INVISIBLE);
                                                            }
                                                        },
                                                        2000
                                                );

                                        linearLayout.addView(templateView);

                                        map.put("id", String.valueOf(id));
                                        map.put("adChoicesUrl", "https://adssettings.google.com/whythisad");

                                        adsArray.add(map);

                                        if (adLoader.isLoading()) {
                                        } else {
                                            JSONArray jsonArray = new JSONArray(adsArray);
                                            JSObject res = new JSObject();
                                            res.put("ads", jsonArray);
                                            call.resolve(res);
                                        }
                                    }
                                }
                        )
                        .withAdListener(
                                new AdListener() {
                                    @Override
                                    public void onAdFailedToLoad(LoadAdError adError) {
                                        // Toast.makeText(getContext(), "ERR " + adError.getMessage(), Toast.LENGTH_SHORT).show();
                                        // Handle the failure by logging, altering the UI, and so on.
                                        call.reject("Ad failed to load " + adError.getMessage());
                                    }
                                }
                        )
                        .withNativeAdOptions(
                                new NativeAdOptions.Builder()
                                        // Methods in the NativeAdOptions.Builder class can be
                                        // used here to specify individual options settings.
                                        .build()
                        )
                        .build();

        adLoader.loadAds(new AdRequest.Builder().build(), adsCount);
    }

    @PluginMethod
    public void triggerNativeAd(PluginCall call) {
        int id = Integer.valueOf(call.getString("id"));
        getActivity()
                .runOnUiThread(
                        new Runnable() {
                            @Override
                            public void run() {
                                int lId = getActivity().getResources().getIdentifier("254398", "id", getContext().getPackageName());
                                LinearLayout linearLayout = getActivity().findViewById(lId);
                                NativeAdView nativeAdView = linearLayout.findViewById(id);

                                AppCompatButton CTABtn = nativeAdView.findViewById(R.id.cta);

                                CTABtn.performClick();
                            }
                        }
                );
    }

    public int gen() {
        Random r = new Random(System.currentTimeMillis());
        return ((1 + r.nextInt(2)) * 10000 + r.nextInt(10000));
    }

    @PluginMethod
    public void loadRewardedVideoAd(PluginCall call) {
        String adId = null;
        Boolean isTestAd = call.getBoolean("isTesting");

        JSObject res = new JSObject();

        if (isTestAd) {
            adId = "ca-app-pub-3940256099942544/5224354917";
        } else {
                adId = call.getString("adId");            
        }

        AdRequest adRequest = new AdRequest.Builder().build();

        String finalAdId = adId;
        getActivity()
                .runOnUiThread(
                        new Runnable() {
                            @Override
                            public void run() {
                                RewardedAd.load(
                                        getContext(),
                                        finalAdId,
                                        adRequest,
                                        new RewardedAdLoadCallback() {
                                            @Override
                                            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                                                call.reject(loadAdError.getMessage());

                                                mRewardedAd = null;
                                            }

                                            @Override
                                            public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
                                                mRewardedAd = rewardedAd;

                                                call.resolve();

                                            }
                                        }
                                );
                            }
                        }
                );
    }

    @PluginMethod
    public void showRewardedVideoAd(PluginCall call) {
        JSObject res = new JSObject();

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mRewardedAd != null) {
                    Activity activityContext = getActivity();
                    mRewardedAd.show(
                            activityContext,
                            new OnUserEarnedRewardListener() {
                                @Override
                                public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                                    notifyListeners("rewardedVideoAdOnRewarded", res);
                                }
                            }
                    );

                    call.resolve();

                    mRewardedAd.setFullScreenContentCallback(
                            new FullScreenContentCallback() {
                                @Override
                                public void onAdClicked() {
                                    // Called when a click is recorded for an ad.
                                    notifyListeners("rewardedVideoAdClicked", res);
                                }

                                @Override
                                public void onAdShowedFullScreenContent() {
                                    // Called when ad is shown.
                                    notifyListeners("rewardedVideoAdShowed", res);
                                }

                                @Override
                                public void onAdFailedToShowFullScreenContent(AdError adError) {
                                    // Called when ad fails to show.
                                    res.put("message", adError.getMessage());
                                    notifyListeners("rewardedVideoAdFailedToShow", res);
                                    mRewardedAd = null;
                                }

                                @Override
                                public void onAdImpression() {
                                    // Called when an impression is recorded for an ad.
                                    notifyListeners("rewardedVideoAdImpression", res);
                                }

                                @Override
                                public void onAdDismissedFullScreenContent() {
                                    // Called when ad is dismissed.
                                    // Set the ad reference to null so you don't show the ad a second time.
                                    notifyListeners("rewardedVideoAdDismissed", res);
                                    mRewardedAd = null;
                                }
                            }
                    );
                } else {
                    call.reject("Ad not loaded. Call loadRewardedVideoAd() to load the ad");
                }
            }
        });
    }

    @PluginMethod
    public void loadRewardedInterstitialAd(PluginCall call) {
        String adId = null;
        Boolean isTestAd = call.getBoolean("isTesting");

        JSObject res = new JSObject();

        if (isTestAd) {
            adId = "ca-app-pub-3940256099942544/5354046379";
        } else {
                adId = call.getString("adId");            
        }

        AdRequest adRequest = new AdRequest.Builder().build();

        String finalAdId = adId;
        getActivity()
                .runOnUiThread(
                        new Runnable() {
                            @Override
                            public void run() {
                                RewardedInterstitialAd.load(
                                        getContext(),
                                        finalAdId,
                                        adRequest,
                                        new RewardedInterstitialAdLoadCallback() {
                                            @Override
                                            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                                                call.reject(loadAdError.getMessage());

                                                mRewardedInterstitialAd = null;
                                            }

                                            @Override
                                            public void onAdLoaded(@NonNull RewardedInterstitialAd rewardedAd) {
                                                mRewardedInterstitialAd = rewardedAd;

                                                call.resolve();
                                            }
                                        }
                                );
                            }
                        }
                );
    }

    @PluginMethod
    public void showRewardedInterstitialAd(PluginCall call) {
        JSObject res = new JSObject();

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mRewardedInterstitialAd != null) {
                    Activity activityContext = getActivity();
                    mRewardedInterstitialAd.show(
                            activityContext,
                            new OnUserEarnedRewardListener() {
                                @Override
                                public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                                    notifyListeners("rewardedInterstitialAdOnRewarded", res);
                                }
                            }
                    );

                    call.resolve();

                    mRewardedInterstitialAd.setFullScreenContentCallback(
                            new FullScreenContentCallback() {
                                @Override
                                public void onAdClicked() {
                                    // Called when a click is recorded for an ad.
                                    notifyListeners("rewardedInterstitialAdClicked", res);
                                }

                                @Override
                                public void onAdShowedFullScreenContent() {
                                    // Called when ad is shown.
                                    notifyListeners("rewardedInterstitialAdShowed", res);
                                }

                                @Override
                                public void onAdFailedToShowFullScreenContent(AdError adError) {
                                    // Called when ad fails to show.
                                    res.put("message", adError.getMessage());
                                    notifyListeners("rewardedInterstitialAdFailedToShow", res);
                                    mRewardedInterstitialAd = null;
                                }

                                @Override
                                public void onAdImpression() {
                                    // Called when an impression is recorded for an ad.
                                    notifyListeners("rewardedInterstitialAdImpression", res);
                                }

                                @Override
                                public void onAdDismissedFullScreenContent() {
                                    // Called when ad is dismissed.
                                    // Set the ad reference to null so you don't show the ad a second time.
                                    notifyListeners("rewardedInterstitialAdDismissed", res);
                                    mRewardedInterstitialAd = null;
                                }
                            }
                    );
                } else {
                    call.reject("Ad not loaded. Call loadRewardedInterstitialAd() to load the ad");
                }
            }
        });
    }

    @PluginMethod
    public void loadInterstitialAd(PluginCall call) {
        String adId = null;
        Boolean isTestAd = call.getBoolean("isTesting");

        JSObject res = new JSObject();

        if (isTestAd) {
            adId = "ca-app-pub-3940256099942544/1033173712";
        } else {
                adId = call.getString("adId");            
        }

        AdRequest adRequest = new AdRequest.Builder().build();

        String finalAdId = adId;

        getActivity()
                .runOnUiThread(
                        new Runnable() {
                            @Override
                            public void run() {
                                InterstitialAd.load(
                                        getContext(),
                                        finalAdId,
                                        adRequest,
                                        new InterstitialAdLoadCallback() {
                                            @Override
                                            public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                                                // The mInterstitialAd reference will be null until
                                                // an ad is loaded.
                                                mInterstitialAd = interstitialAd;
                                                call.resolve();
                                            }

                                            @Override
                                            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                                                // Handle the error
                                                call.reject(loadAdError.getMessage());
                                                mInterstitialAd = null;
                                            }
                                        }
                                );
                            }
                        }
                );
    }

    @PluginMethod
    public void showInterstitialAd(PluginCall call) {
        JSObject res = new JSObject();

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mInterstitialAd != null) {
                    call.resolve();

                    mInterstitialAd.show(getActivity());

                    mInterstitialAd.setFullScreenContentCallback(
                            new FullScreenContentCallback() {
                                @Override
                                public void onAdClicked() {
                                    // Called when a click is recorded for an ad.
                                    notifyListeners("interstitialAdClicked", res);
                                }

                                @Override
                                public void onAdDismissedFullScreenContent() {
                                    // Called when ad is dismissed.
                                    // Set the ad reference to null so you don't show the ad a second time.
                                    notifyListeners("interstitialAdDismissed", res);
                                    mInterstitialAd = null;
                                }

                                @Override
                                public void onAdFailedToShowFullScreenContent(AdError adError) {
                                    // Called when ad fails to show.
                                    res.put("message", adError.getMessage());
                                    notifyListeners("interstitialAdFailedToShow", res);
                                    mInterstitialAd = null;
                                }

                                @Override
                                public void onAdImpression() {
                                    // Called when an impression is recorded for an ad.
                                    notifyListeners("interstitialAdImpression", res);
                                }

                                @Override
                                public void onAdShowedFullScreenContent() {
                                    // Called when ad is shown.
                                    notifyListeners("interstitialAdShown", res);
                                }
                            }
                    );
                } else {
                    call.reject("Ad not loaded. Call loadInterstitialAd() to load the ad");
                }
            }
        });
    }

    @PluginMethod
    public void showBannerAd(PluginCall call) {
        if (adView == null) {
            String adId = null;
            Boolean isTestAd = call.getBoolean("isTesting");
            String adSize = call.getString("adSize");
            bannerAdPosition = call.getString("adPosition");

            JSObject res = new JSObject();

            if (isTestAd) {
                adId = "ca-app-pub-3940256099942544/6300978111";
            } else {
                    adId = call.getString("adId");                
            }

            String finalAdId = adId;

            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    bannerAdHidden = false;
                    bLinearLayout.setVisibility(View.VISIBLE);
                    AdRequest adRequest = new AdRequest.Builder().build();

                    adView = new AdView(getContext());
                    adView.setAdSize(getAdSize(adSize));
                    adView.setAdUnitId(finalAdId);

                    adView.loadAd(adRequest);

                    adView.setAdListener(new AdListener() {
                        @Override
                        public void onAdClicked() {
                            notifyListeners("bannerAdClicked", res);
                        }

                        @Override
                        public void onAdClosed() {
                            notifyListeners("bannerAdClosed", res);
                            adView = null;
                        }

                        @Override
                        public void onAdFailedToLoad(LoadAdError adError) {
                            call.reject(adError.getMessage());
                            adView = null;
                        }

                        @Override
                        public void onAdImpression() {
                            notifyListeners("bannerAdImpression", res);
                        }

                        @Override
                        public void onAdLoaded() {
                            createBannerAdView(bannerAdPosition);
                            call.resolve();
                        }

                        @Override
                        public void onAdOpened() {
                            notifyListeners("bannerAdOpened", res);
                        }
                    });

                }
            });
        } else {
            call.reject("Banner ad already showing");
        }
    }

    @PluginMethod
    public void hideBannerAd(PluginCall call) {
        if (adView != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    bLinearLayout.setVisibility(View.GONE);
                    adView.pause();
                    bannerAdHidden = true;
//          adjustWebView(bannerAdPosition);
                    call.resolve();
                }
            });
        } else {
            call.reject("No banner ad to hide. Call showBannerAd() to show a banner ad first");
        }
    }

    @PluginMethod
    public void resumeBannerAd(PluginCall call) {
        if (adView != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    bLinearLayout.setVisibility(View.VISIBLE);
                    adView.resume();
                    bannerAdHidden = false;
//          adjustWebView(bannerAdPosition);
                    call.resolve();
                }
            });
        } else {
            call.reject("No banner ad to resume. Call showBannerAd() to show a banner ad first");
        }
    }

    @PluginMethod
    public void removeBannerAd(PluginCall call) {
        if (adView != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    bLinearLayout.removeView(adView);
                    adView.destroy();
                    adView = null;
                    bannerAdHidden = true;
//          adjustWebView(bannerAdPosition);
                    call.resolve();
                }
            });
        } else {
            call.reject("No banner ad to remove. Call showBannerAd() to show a banner ad first");
        }
    }

    private AdSize getAdSize(String adSize) {
        if (adSize.equals("BANNER")) {
            return AdSize.BANNER;
        } else if (adSize.equals("LARGE_BANNER")) {
            return AdSize.LARGE_BANNER;
        } else if (adSize.equals("MEDIUM_RECTANGLE")) {
            return AdSize.MEDIUM_RECTANGLE;
        } else if (adSize.equals("FULL_BANNER")) {
            return AdSize.FULL_BANNER;
        } else {
            return AdSize.LEADERBOARD;
        }
    }

    public void createBannerAdView(String adPosition) {
//    adjustWebView(adPosition);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );

        if (adPosition.equals("bottom")) {
            params.gravity = Gravity.BOTTOM;
        } else {
            params.gravity = Gravity.TOP;
        }

        adView.setLayoutParams(params);

        if (adView.getParent() == null) {
            bLinearLayout.addView(adView);
        }
    }

//  public void adjustWebView(String adPosition) {
//    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
//    float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
//    float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
//
//    Resources r = getContext().getResources();
//    float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpWidth, r.getDisplayMetrics());
//
//    int adHeight = 0;
//
//    if (bannerAdHidden) {
//      adHeight = 0;
//    } else {
//      if (adView != null) {
//        adHeight = adView.getAdSize().getHeight();
//      }
//    }
//
//    float height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) ((dpHeight - adHeight) - 24), r.getDisplayMetrics());
//    float diffHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) (dpHeight - ((dpHeight - adHeight))), r.getDisplayMetrics());
//
//    ViewGroup.LayoutParams lp = getBridge().getWebView().getLayoutParams();
//    lp.width = (int) width;
//    lp.height = (int) height;
//
//    if (adPosition.equals("top")) {
//      ViewGroup.MarginLayoutParams mLp = (ViewGroup.MarginLayoutParams) getBridge().getWebView().getLayoutParams();
//      mLp.topMargin = (int) diffHeight;
//    }
//
//    getBridge().getWebView().setLayoutParams(lp);
//  }

//  public void checkOrientationChange() {
//    OrientationEventListener orientationEventListener = new OrientationEventListener(getContext()) {
//      @Override
//      public void onOrientationChanged(int i) {
//        if (i != prevOrientation) {
//          new android.os.Handler().postDelayed(
//            new Runnable() {
//              public void run() {
//                adjustWebView(bannerAdPosition);
//              }
//            }, 500);
//        }
//        prevOrientation = i;
//      }
//    };
//
//    if (orientationEventListener.canDetectOrientation()) {
//      orientationEventListener.enable();
//    }
//  }
}
