package com.braintreepayments.api.internal;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.net.ConnectivityManager;

import com.braintreepayments.api.Venmo;
import com.paypal.android.sdk.onetouch.core.PayPalOneTouchCore;

import org.json.JSONException;
import org.json.JSONObject;

public class AnalyticsEvent {

    int id;
    String event;
    long timestamp;
    JSONObject metadata;

    public AnalyticsEvent(Context context, String sessionId, String integration, String event) {
        this.event = "android." + integration + "." + event;
        this.timestamp = System.currentTimeMillis() / 1000;
        metadata = new JSONObject();
        try {
            metadata.put(AnalyticsIntentService.SESSION_ID_KEY, sessionId)
                    .put(AnalyticsIntentService.DEVICE_NETWORK_TYPE_KEY, getNetworkType(context))
                    .put(AnalyticsIntentService.USER_INTERFACE_ORIENTATION_KEY, getUserOrientation(context))
                    .put(AnalyticsIntentService.MERCHANT_APP_VERSION_KEY, getAppVersion(context))
                    .put(AnalyticsIntentService.PAYPAL_INSTALLED_KEY, PayPalOneTouchCore.isWalletAppInstalled(context))
                    .put(AnalyticsIntentService.VENMO_INSTALLED_KEY, Venmo.isVenmoInstalled(context));
        } catch (JSONException ignored) {}
    }

    public AnalyticsEvent() {
        metadata = new JSONObject();
    }

    public String getIntegrationType() {
        String[] eventSegments = this.event.split("\\.");
        if (eventSegments.length > 1) {
            return eventSegments[1];
        } else {
            return "";
        }
    }

    private String getNetworkType(Context context) {
        String networkType = null;
        ConnectivityManager connectivityManager =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager.getActiveNetworkInfo() != null) {
            networkType = connectivityManager.getActiveNetworkInfo().getTypeName();
        }
        if (networkType == null) {
            networkType = "none";
        }
        return networkType;
    }

    private String getUserOrientation(Context context) {
        int orientation = context.getResources().getConfiguration().orientation;
        switch (orientation) {
            case Configuration.ORIENTATION_PORTRAIT:
                return "Portrait";
            case Configuration.ORIENTATION_LANDSCAPE:
                return "Landscape";
            default:
                return "Unknown";
        }
    }

    private String getAppVersion(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (NameNotFoundException e) {
            return "VersionUnknown";
        }
    }
}

@@ -215,16 +215,48 @@
 Manager;
+%0Aimport android.net.NetworkInfo;
 %0A%0Aimport
@@ -1990,20 +1990,42 @@
         
-if (
+NetworkInfo networkInfo = 
 connecti
@@ -2058,16 +2058,41 @@
 rkInfo()
+;%0A        if (networkInfo
  != null
@@ -2125,38 +2125,9 @@
 e = 
-connectivityManager.getActiveN
+n
 etwo
@@ -2132,18 +2132,16 @@
 workInfo
-()
 .getType

Fix NPE with getting connectivityManager.getActiveNetworkInfo() in AnalyticsEvent