package io.happie.naturalforms;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;

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

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class NaturalForms extends CordovaPlugin {

	public static final String TAG = "NaturalForms";

	@Override
	public boolean execute(String action, JSONArray args, CallbackContext callback) throws JSONException {

		final Intent nfIntent = cordova.getActivity().getPackageManager().getLaunchIntentForPackage("net.expedata.naturalforms");
		final CallbackContext finalCallBack = callback;

		if(nfIntent == null) {
			callback.error("Please install naturalForms");
		}

		File csv = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/JobNimbus/", "data.csv");
		try{
			FileWriter fw = new FileWriter(csv);
			fw.write(args.getString(0));
			fw.close();
		} catch(IOException ioe){
			callback.error("Could not save naturalForms data.");
		}

		nfIntent.setData(Uri.fromFile(csv));
		nfIntent.setClassName("net.expedata.naturalforms", "net.expedata.naturalforms.ui.CsvImportActivity"); // , csvImport.name);
		// startActivity(nfIntent);

		final NaturalForms plugin = this;
		cordova.getThreadPool().execute(new Runnable() {
			public void run() {
				cordova.startActivityForResult(plugin, nfIntent, 0);
				finalCallBack.success("success");
			}
		});
		return true;
	}
}
@@ -384,17 +384,20 @@
 ugin %7B%0A%0A
-%09
+    
 public s
@@ -438,17 +438,20 @@
 orms%22;%0A%0A
-%09
+    
 @Overrid
@@ -452,17 +452,20 @@
 verride%0A
-%09
+    
 public b
@@ -560,18 +560,24 @@
 tion %7B%0A%0A
-%09%09
+        
 final In
@@ -690,18 +690,24 @@
 orms%22);%0A
-%09%09
+        
 final Ca
@@ -751,12 +751,19 @@
 k;%0A%0A
-%09%09
+        
 if
+ 
 (nfI
@@ -779,19 +779,28 @@
 null) %7B%0A
-%09%09%09
+            
 callback
@@ -838,23 +838,35 @@
 orms%22);%0A
-%09%09%7D%0A%0A%09%09
+        %7D%0A%0A        
 File csv
@@ -971,18 +971,34 @@
 %22);%0A
-%09%09
+        
 try
+ 
 %7B%0A
-%09%09%09
+            
 File
@@ -1030,19 +1030,28 @@
 r(csv);%0A
-%09%09%09
+            
 fw.write
@@ -1071,19 +1071,28 @@
 ng(0));%0A
-%09%09%09
+            
 fw.close
@@ -1099,17 +1099,24 @@
 ();%0A
-%09%09
+        
 %7D catch
+ 
 (IOE
@@ -1128,21 +1128,31 @@
 ion ioe)
+ 
 %7B%0A
-%09%09%09
+            
 callback
@@ -1200,15 +1200,27 @@
 %22);%0A
-%09%09%7D%0A%0A%09%09
+        %7D%0A%0A        
 nfIn
@@ -1252,18 +1252,42 @@
 (csv));%0A
-%09%09
+        try %7B%0A            
 nfIntent
@@ -1406,41 +1406,21 @@
 e);%0A
-%09%09// startActivity(nfIntent);%0A%0A%09%09
+%0A            
 fina
@@ -1449,18 +1449,28 @@
 = this;%0A
-%09%09
+            
 cordova.
@@ -1510,19 +1510,32 @@
 ble() %7B%0A
-%09%09%09
+                
 public v
@@ -1546,20 +1546,36 @@
 run() %7B%0A
-%09%09%09%09
+                    
 cordova.
@@ -1623,12 +1623,28 @@
 0);%0A
-%09%09%09%09
+                    
 fina
@@ -1677,34 +1677,251 @@
 %22);%0A
-%09%09%09%7D%0A%09%09%7D);%0A%09%09return true;%0A%09
+                %7D%0A            %7D);%0A            return true;%0A        %7D catch (Exception err) %7B%0A            callback.error(%22naturalForms is not supported on this device%22);%0A            return true;%0A        %7D%0A        // startActivity(nfIntent);%0A    
 %7D%0A%7D

improve error handling for android