package org.opendatakit.dependencies;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;

import org.opendatakit.androidcommon.R;
import org.opendatakit.application.CommonApplication;

/**
 * This class checks for app dependencies, informing the user if any are missing
 *
 * @author marshallbradley93@gmail.com
 */
public class DependencyChecker {

    public static final String surveyAppPkgName = "org.opendatakit.survey";
    private static final String oiFileMgrPkgName = "org.openintents.filemanager";
    private static final String servicesAppPkgName = "org.opendatakit.services";

    private static final String tables = "tables";
    private static final String scan = "scan";

    private final Activity activity;
    private final Context context;

    public DependencyChecker(Activity activity) {
        this.activity = activity;
        this.context = activity.getApplicationContext();
    }

    public boolean checkDependencies() {

        boolean oiInstalled;
        boolean servicesInstalled;

        if (tables.equals(((CommonApplication)context).getToolName()) ||
            scan.equals(((CommonApplication)context).getToolName())) { // need to check
        // for OI and Services
            oiInstalled = isPackageInstalled(context, oiFileMgrPkgName);
        } else { // only need to check for Services
            oiInstalled = true;
        }

        servicesInstalled = isPackageInstalled(context, servicesAppPkgName);

        if (oiInstalled && servicesInstalled) { // correct dependencies installed
            return true;
        } else {
            alertMissing(oiInstalled, servicesInstalled); // missing dependencies, warn user
            return false;
        }
    }

    private void alertMissing(boolean oiInstalled, boolean servicesInstalled) {

        String message;
        String title = context.getString(R.string.dependency_missing);

        if (oiInstalled && !servicesInstalled) {
            message = context.getString(R.string.services_missing);
        } else if (!oiInstalled && servicesInstalled) {
            message = context.getString(R.string.oi_missing);
        } else {
            message = context.getString(R.string.oi_and_services_missing);
            title = context.getString(R.string.dependencies_missing);
        }

        AlertDialog alert = buildAlert(title, message);
        alert.show();
    }

    private AlertDialog buildAlert(String title, String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setCancelable(false);
        builder.setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        activity.finish();
                        System.exit(0);
                    }
                });

        return builder.create();
    }

    public static boolean isPackageInstalled(Context context, String packageName) {
        PackageManager pm = context.getPackageManager();
        try {
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
}

@@ -388,16 +388,367 @@
 missing%0A
+ * It treats tables and scan as depending on both services and oi file manager. Everything else%0A * it treats as depending on just services.%0A * WARNING!%0A * If the required dependencies are not installed, it finish()'s the view and calls System.exit(0).%0A *%0A * Doesn't use a DialogFragment so it might break if the user rotates the screen. That's a TODO%0A
  *%0A * @a
@@ -1174,80 +1174,68 @@
 ate 
-final Activity activity;%0A    private final Context context;%0A%0A    public 
+DependencyChecker() %7B%0A    %7D%0A%0A    public static boolean check
 Depe
@@ -1239,24 +1239,19 @@
 ependenc
-yChecker
+ies
 (Activit
@@ -1264,24 +1264,25 @@
 vity) %7B%0A
+%0A
         
 this.act
@@ -1277,47 +1277,16 @@
     
-this.activity = activity;%0A        this.
+Context 
 cont
@@ -1328,56 +1328,8 @@
 t();
-%0A    %7D%0A%0A    public boolean checkDependencies() %7B
 %0A%0A  
@@ -2012,21 +2012,55 @@
 lled
-); // missing
+, context, activity); // missing%0A            //
  dep
@@ -2074,16 +2074,31 @@
 es, warn
+%0A            //
  user%0A  
@@ -2150,16 +2150,23 @@
 private 
+static 
 void ale
@@ -2217,24 +2217,68 @@
 cesInstalled
+, Context%0A        context, Activity activity
 ) %7B%0A%0A       
@@ -2823,24 +2823,34 @@
 tle, message
+, activity
 );%0A        a
@@ -2881,16 +2881,23 @@
 private 
+static 
 AlertDia
@@ -2939,16 +2939,41 @@
  message
+, final Activity activity
 ) %7B%0A    

Make DependencyChecker static, document it