package io.happie.naturalforms;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.ActivityNotFoundException;
import android.content.pm.ResolveInfo;
import android.os.Environment;
import android.net.Uri;
import android.util.Log;

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;
import java.util.List;

public class NaturalForms extends CordovaPlugin {

    public static final String TAG = "NaturalForms";

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callback) throws JSONException {
        try {
            //PackageManager manager = cordova.getActivity().getApplicationContext().getPackageManager();
            //LaunchIntent = manager.getLaunchIntentForPackage(params.getString("net.expedata.naturalforms"));

            //LaunchIntent = new Intent("net.expedata.naturalforms.ui.CsvImportActivity");

            Intent LaunchIntent = new Intent(Intent.ACTION_VIEW);

            if (LaunchIntent == null) {
                callback.error("Could not find natural forms");
                return true;
            }

            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.");
            }

            LaunchIntent.setDataAndType(Uri.fromFile(csv), "text/csv");

            ResolveInfo best = getPackageInfo(LaunchIntent, "net.expedata.naturalforms");
            if (best == null) {
                callback.error("Please Install naturalFORMS");
            } else {
                LaunchIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
                cordova.getActivity().startActivity(LaunchIntent);
            }

            callback.success();
        } catch (ActivityNotFoundException e) {
            callback.error("Could not start naturalForms");
        }
        return true;
    }

    private ResolveInfo getPackageInfo(final Intent intent, final String packageName) {
        Log.e(TAG, "!!!!!!getPackageInfo");
        final PackageManager pm = cordova.getActivity().getPackageManager();
        final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);

        ResolveInfo best = null;
        for (final ResolveInfo info : matches) {
            Log.e(TAG, info.activityInfo.packageName);
            if (info.activityInfo.packageName.contains(packageName)) {
                best = info;
            }
        }
        return best;
    }
}
@@ -26,16 +26,54 @@
 forms;%0A%0A
+import android.content.ComponentName;%0A
 import a
@@ -490,22 +490,28 @@
 .io.File
-Writer
+OutputStream
 ;%0Aimport
@@ -808,18 +808,16 @@
         
-//
 PackageM
@@ -904,34 +904,39 @@
 ();%0A            
-//
+Intent 
 LaunchIntent = m
@@ -972,25 +972,8 @@
 age(
-params.getString(
 %22net
@@ -1000,78 +1000,11 @@
 ms%22)
-);%0A%0A            //LaunchIntent = new Intent(%22net.expedata.naturalforms
+;//
 .ui.
@@ -1020,20 +1020,16 @@
 Activity
-%22);%0A
 %0A       
@@ -1025,32 +1025,34 @@
 ity%0A            
+//
 Intent LaunchInt
@@ -1089,17 +1089,16 @@
 _VIEW);%0A
-%0A
         
@@ -1253,16 +1253,148 @@
     
-File csv
+String state = Environment.getExternalStorageState();%0A            if (Environment.MEDIA_MOUNTED.equals(state)) %7B%0A%0A                File JNDir
  = n
@@ -1431,16 +1431,22 @@
 lStorage
+Public
 Director
@@ -1451,32 +1451,43 @@
 ory(
-).getAbsolutePath() +
+Environment.DIRECTORY_DOCUMENTS),
  %22
-/
 JobN
@@ -1495,21 +1495,124 @@
 mbus
-/%22, %22
+%22);%0A                if (!JNDir.mkdirs()) %7B%0A                    //callback.error(%22Could not create naturalForms 
 data.
-csv
 %22);%0A
@@ -1627,43 +1627,83 @@
     
-try %7B%0A                File
+    %7D%0A%0A                try %7B%0A                    FileOutputStream over
 Write
-r fw
  = n
@@ -1713,18 +1713,61 @@
 File
-Writer(csv
+OutputStream(JNDir.toString() + %22/nf-data.csv%22, false
 );%0A 
@@ -1781,18 +1781,29 @@
         
-fw
+    overWrite
 .write(a
@@ -1818,16 +1818,27 @@
 tring(0)
+.getBytes()
 );%0A     
@@ -1852,141 +1852,79 @@
     
-fw.close();%0A            %7D catch (IOException ioe) %7B%0A                callback.error(%22Could not save naturalForms data.%22);%0A    
+    overWrite.flush();%0A                    overWrite.close();%0A%0A
         
 %7D%0A%0A 
@@ -1911,35 +1911,32 @@
 ose();%0A%0A        
-%7D%0A%0A
             Laun
@@ -1967,20 +1967,47 @@
 Uri.
-fromFile(
+parse(JNDir.toString() + %22/nf-data.
 csv
+%22
 ), %22
@@ -2015,24 +2015,32 @@
 ext/csv%22);%0A%0A
+        
             
@@ -2121,124 +2121,13 @@
 %22);%0A
+%0A
     
-        if (best == null) %7B%0A                callback.error(%22Please Install naturalFORMS%22);%0A            %7D else %7B%0A
     
@@ -2199,33 +2199,61 @@
 me, 
-best.activityInfo.name
+%22net.expedata.naturalforms.ui.CsvImportActivity%22
 );%0A
+%0A 
     
@@ -2264,16 +2264,19 @@
         
+   
 cordova.
@@ -2318,16 +2318,21 @@
 ntent);%0A
+%0A    
         
@@ -2328,32 +2328,219 @@
                %7D
+ catch (IOException ioe) %7B%0A                    callback.error(%22Could not save naturalForms data.%22);%0A                %7D%0A            %7D else callback.error(%22cannot access external storage.%22);
 %0A%0A            ca
@@ -2795,52 +2795,8 @@
 ) %7B%0A
-        Log.e(TAG, %22!!!!!!getPackageInfo%22);%0A
     
@@ -3034,63 +3034,8 @@
 ) %7B%0A
-            Log.e(TAG, info.activityInfo.packageName);%0A
     

improve natural forms android, in progress