package io.happie.raygun;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import main.java.com.mindscapehq.android.raygun4android.RaygunClient;
import main.java.com.mindscapehq.android.raygun4android.messages.RaygunUserInfo;
import android.content.Context;
import android.util.Log;

public class RaygunNativePlugin extends CordovaPlugin {
    private final String pluginName = "RaygunNativePlugin";

    @Override
    public boolean execute(final String action, final JSONArray data, final CallbackContext callbackContext) {
        Log.d(pluginName, pluginName + " called with options: " + data);
        if (action.equals("startNativeRaygun")) startNativeRaygun(data, callbackContext);
        else if(action.equals("testCrash")) testCrash();
        return true;
    }

    private void startNativeRaygun(final JSONArray data, final CallbackContext callbackContext) {
        final Context context = this.cordova.getActivity().getApplicationContext();
        this.cordova.getThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                RaygunClient.Init(context);
                RaygunClient.AttachExceptionHandler();
                try {
                    JSONObject obj = data.getJSONObject(0);
                    String message;
                    if (obj.has("user")) {
                        RaygunUserInfo user = new RaygunUserInfo();
                        user.FullName = obj.getString("user");
                        RaygunClient.SetUser(user);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void testCrash() {
        Integer integers[] = new Integer[Integer.MAX_VALUE];
        integers[2147483647] = 2147483647+1;
        testCrash();
        throw new RuntimeException("This is a crash");
    }
}
@@ -1216,17 +1216,17 @@
 nClient.
-I
+i
 nit(cont
@@ -1260,17 +1260,17 @@
 nClient.
-A
+a
 ttachExc
@@ -1372,44 +1372,8 @@
 0);%0A
-                    String message;%0A
     
@@ -1512,16 +1512,19 @@
 ser.
+set
 FullName
  = o
@@ -1519,19 +1519,17 @@
 FullName
- = 
+(
 obj.getS
@@ -1541,16 +1541,17 @@
 (%22user%22)
+)
 ;%0A      
@@ -1581,17 +1581,17 @@
 nClient.
-S
+s
 etUser(u

update android raygun consumption