package com.grarak.kerneladiutor.utils.tools;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;

import com.grarak.kerneladiutor.utils.database.PerAppDB;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by joe on 2/29/16.
 */
public class Per_App {
    public static Map<String, String> getInstalledApps (Context context) {
        // Get a list of installed apps. Currently this is only the package name
        final PackageManager pm = context.getPackageManager();

        final Map<String, String> applist = new HashMap<>();

        final List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

        for (ApplicationInfo packageInfo : packages) {
            applist.put(String.valueOf(packageInfo.loadLabel(pm)), packageInfo.packageName);
        }

        return applist;
    }

    public static String[] getPackageNames (Map<String, String> apps) {
        String[] array = apps.values().toArray(new String[apps.size()]);
        Arrays.sort(array, String.CASE_INSENSITIVE_ORDER);
        return  array;
    }

    public static String[] getAppNames (Map<String, String> apps) {
        String[] array = apps.keySet().toArray(new String[apps.size()]);
        Arrays.sort(array, String.CASE_INSENSITIVE_ORDER);
        return  array;
    }

    public static void save_app (String app, String id, Context context) {
        PerAppDB perappDB = new PerAppDB(context);
        List<PerAppDB.PerAppItem> PerAppItem = perappDB.getAllApps() ;
        for (int i = 0; i < PerAppItem.size(); i++) {
            String p = PerAppItem.get(i).getApp();
            if (p != null && p.equals(app)) {
                perappDB.delApp(i);
            }
        }

        perappDB.putApp(app, id);
        perappDB.commit();
    }

    public static void remove_app (String app, String id, Context context) {
        PerAppDB perappDB = new PerAppDB(context);

        List<PerAppDB.PerAppItem> PerAppItem = perappDB.getAllApps() ;
        for (int i = 0; i < PerAppItem.size(); i++) {
            String p = PerAppItem.get(i).getApp();
            if (p != null && p.equals(app)) {
                perappDB.delApp(i);
            }
        }

        perappDB.commit();
    }

    public static boolean app_profile_exists (String app, Context context) {
        PerAppDB perappDB = new PerAppDB(context);
        boolean exists = perappDB.containsApp(app);

        return exists;
    }

    public static ArrayList<String> app_profile_info (String app, Context context) {
        PerAppDB perappDB = new PerAppDB(context);
        if (perappDB.containsApp(app)) {
            return perappDB.get_info(app);
        }

        return null;
    }

    public static boolean[] getExistingSelections (String[] apps, String profile, Context context) {
        PerAppDB perappDB = new PerAppDB(context);
        boolean exists[] = new boolean[apps.length];

        List<PerAppDB.PerAppItem> PerAppItem = perappDB.getAllApps() ;
        for (int i = 0; i < PerAppItem.size(); i++) {
            String p = PerAppItem.get(i).getApp();
            String id = PerAppItem.get(i).getID();
            if (p != null && Arrays.asList(apps).contains(p)) {
                if (id != null && id.equals(profile)) {
                    exists[Arrays.asList(apps).indexOf(p)] = true;
                }
            }
        }
        return exists;
    }

    public static boolean isAccessibilityEnabled(Context context, String id) {

        AccessibilityManager am = (AccessibilityManager) context
                .getSystemService(Context.ACCESSIBILITY_SERVICE);

        List<AccessibilityServiceInfo> runningServices = am
                .getEnabledAccessibilityServiceList(AccessibilityEvent.TYPES_ALL_MASK);
        for (AccessibilityServiceInfo service : runningServices) {
            if (id != null && id.equals(service.getId())) {
                return true;
            }
        }

        return false;
    }

}

@@ -1349,32 +1349,77 @@
 NSITIVE_ORDER);%0A
+        array = insertDefaultListing(array);%0A
         return  
@@ -1417,32 +1417,32 @@
  return  array;%0A
-
     %7D%0A%0A    publi
@@ -1636,33 +1636,324 @@
 R);%0A        
-return  a
+array = insertDefaultListing(array);%0A        return  array;%0A    %7D%0A%0A    private static String%5B%5D insertDefaultListing(String%5B%5D array)%7B%0A        String%5B%5D newArray = new String%5Barray.length+1%5D;%0A        newArray%5B0%5D = %22Default%22;%0A        System.arraycopy(array,0,newArray,1,array.length);%0A        return newA
 rray;%0A    %7D%0A

add Default back to per-app listings