package aashrai.android.gettowork.utils;

import aashrai.android.gettowork.view.activity.SettingsActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.graphics.drawable.VectorDrawableCompat;
import java.util.List;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func0;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.schedulers.Schedulers;

public class Utils {
  public static Intent getHomeScreenIntent() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    return intent;
  }

  public static Intent getSettingsActivityIntent(Context context) {
    return new Intent(context, SettingsActivity.class);
  }

  public static boolean isAppLockDeactivationExpired(SharedPreferences sharedPreferences) {
    long millis = sharedPreferences.getLong(Constants.OVERLAY_DEACTIVATED_MILLIS, -1L);
    long timestamp = sharedPreferences.getLong(Constants.OVERLAY_DEACTIVATED_TIMESTAMP, -1L);
    return millis != -1L && timestamp + millis < System.currentTimeMillis();
  }

  public static boolean isAppLockActivated(SharedPreferences sharedPreferences) {
    return sharedPreferences.getBoolean(Constants.APP_LOCK_ACTIVATED, false)
        || isAppLockDeactivationExpired(sharedPreferences);
  }

  public static Func1<ApplicationInfo, Boolean> removeSelfPackage(final Context context) {
    return new Func1<ApplicationInfo, Boolean>() {
      @Override public Boolean call(ApplicationInfo applicationInfo) {
        return !applicationInfo.packageName.equals(context.getPackageName());
      }
    };
  }

  public static Func2<ApplicationInfo, ApplicationInfo, Integer> getApplicationSortFunction(
      final Context context) {
    return new Func2<ApplicationInfo, ApplicationInfo, Integer>() {
      @Override
      public Integer call(ApplicationInfo applicationInfo, ApplicationInfo applicationInfo2) {
        return applicationInfo.loadLabel(context.getPackageManager())
            .toString()
            .compareTo(applicationInfo2.loadLabel(context.getPackageManager()).toString());
      }
    };
  }

  public static Func1<? super ApplicationInfo, Boolean> removeLaunchers(final Context context) {
    return new Func1<ApplicationInfo, Boolean>() {
      @Override public Boolean call(ApplicationInfo applicationInfo) {
        //Hack for filtering launchers
        return !getApplicationName(applicationInfo, context).contains("launcher");
      }
    };
  }

  public static Func1<? super ApplicationInfo, Boolean> removeSystemApps() {
    return new Func1<ApplicationInfo, Boolean>() {
      @Override public Boolean call(ApplicationInfo applicationInfo) {
        return (applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0;
      }
    };
  }

  public static VectorDrawableCompat createVectorDrawable(final Context context, int resourceId) {
    return VectorDrawableCompat.create(context.getResources(), resourceId, context.getTheme());
  }

  public static String getApplicationName(ApplicationInfo applicationInfo, Context context) {
    return applicationInfo.loadLabel(context.getPackageManager()).toString().toLowerCase();
  }

  public static Observable<List<ApplicationInfo>> getAllApplications(final Context context) {
    return Observable.from(
        context.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA))
        .observeOn(Schedulers.computation())
        .filter(Utils.removeSelfPackage(context))
        .filter(Utils.removeLaunchers(context))
        .filter(Utils.removeSystemApps())
        .toSortedList(Utils.getApplicationSortFunction(context))
        .observeOn(AndroidSchedulers.mainThread());
  }

  public static <T> Observable<T> getDeferedObservable(final Observable<T> observable) {
    return Observable.defer(new Func0<Observable<T>>() {
      @Override public Observable<T> call() {
        return observable;
      }
    });
  }
}

@@ -252,50 +252,8 @@
 fo;%0A
-import android.content.pm.PackageManager;%0A
 impo
@@ -315,128 +315,8 @@
 at;%0A
-import java.util.List;%0Aimport rx.Observable;%0Aimport rx.android.schedulers.AndroidSchedulers;%0Aimport rx.functions.Func0;%0A
 impo
@@ -368,41 +368,8 @@
 nc2;
-%0Aimport rx.schedulers.Schedulers;
 %0A%0Apu
@@ -3133,768 +3133,7 @@
 %0A  %7D
-%0A%0A  public static Observable%3CList%3CApplicationInfo%3E%3E getAllApplications(final Context context) %7B%0A    return Observable.from(%0A        context.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA))%0A        .observeOn(Schedulers.computation())%0A        .filter(Utils.removeSelfPackage(context))%0A        .filter(Utils.removeLaunchers(context))%0A        .filter(Utils.removeSystemApps())%0A        .toSortedList(Utils.getApplicationSortFunction(context))%0A        .observeOn(AndroidSchedulers.mainThread());%0A  %7D%0A%0A  public static %3CT%3E Observable%3CT%3E getDeferedObservable(final Observable%3CT%3E observable) %7B%0A    return Observable.defer(new Func0%3CObservable%3CT%3E%3E() %7B%0A      @Override public Observable%3CT%3E call() %7B%0A        return observable;%0A      %7D%0A    %7D);%0A  %7D
 %0A%7D%0A

Remove uneeded static methods