package org.jevon.cordova.parse.pushplugin;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import com.parse.Parse;
import com.parse.ParseInstallation;

public class ParsePushPlugin extends CordovaPlugin {
	
	public static final String ACTION_INITIALIZE = "initialize";
	
	@Override
	public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
		if (action.equals(ACTION_INITIALIZE)) {
			this.initialize(callbackContext, args);
			return true;
		}
		// TODO more actions...
		return false;	// false == NoSuchMethod
	}
	
	private void initialize(final CallbackContext context, final JSONArray args) throws JSONException {
		if (args.length() >= 1) {
			String appId = args.getString(0);
			String clientKey = args.getString(1);
			Parse.initialize(cordova.getActivity(), appId, clientKey);
			
			// "Also, save the current installation to Parse"
			ParseInstallation.getCurrentInstallation().saveInBackground();
			
			context.success();
		} else {
			context.error("Expected two arguments");
		}
	}

}

@@ -788,24 +788,30 @@
  %3E= 1) %7B%0A%09%09%09
+final 
 String appId
@@ -835,16 +835,22 @@
 (0);%0A%09%09%09
+final 
 String c
@@ -878,24 +878,225 @@
 ring(1);%0A%09%09%09
+%0A%09%09%09// %22exec() to initialize blocked the main thread; Plugin should use CordovaInterface.getThreadPool()%22%0A%09%09%09cordova.getThreadPool().execute(new Runnable() %7B%0A%09%09%09%09@Override%0A%09%09%09%09public void run() %7B%0A%09%09%09%09%09
 Parse.initia
@@ -1141,28 +1141,32 @@
 entKey);%0A%09%09%09
+%09%09
 %0A%09%09%09
+%09%09
 // %22Also, sa
@@ -1203,16 +1203,18 @@
  Parse%22%0A
+%09%09
 %09%09%09Parse
@@ -1274,20 +1274,24 @@
 d();%0A%09%09%09
-%0A
+%09%09%0A%09
 %09%09%09
+%09
 context.
@@ -1301,16 +1301,29 @@
 cess();%0A
+%09%09%09%09%7D%0A%09%09%09%7D);%0A
 %09%09%7D else

Use Cordova ThreadPool for Parse.initialize