package com.vackosar.searchbasedlauncher;

import android.content.ComponentName;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AppsView implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
    final MainActivity mainActivity;
    final ListView listView;
    private final DialogFactory dialogFactory;
    List<App> filtered = new ArrayList<>();
    List<App> recent = new ArrayList<>();
    public static final int FIRST_INDEX = 0;
    public static final App MENU_APP = new App(MainActivity.APP_PACKAGE_NAME + ".Menu", "#Menu", MainActivity.APP_PACKAGE_NAME + ".Menu");

    public AppsView(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
        listView = (ListView) mainActivity.findViewById(R.id.appListView);
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);
        dialogFactory = new DialogFactory(mainActivity);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        executeActivity(position);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        return showOptionsForApp(position);
    }

    public void refeshView() {
        String filterText = mainActivity.getSearchText().getFilterText();
        addFiltered(filterText);
        if (autostartFirstApp()) {
            executeActivity(FIRST_INDEX);
        } else {
            viewAppList(filtered);
        }
    }

    private void addFiltered(String filterText) {
        filtered.clear();
        addRecent(filterText);
        for (App app: mainActivity.getAppsManager().getPkg()) {
            addFiltredIfMatch(filterText, app);
        }
        addFiltredIfMatch(filterText, MENU_APP);
    }

    private void addFiltredIfMatch(String filterText, App app) {
        if (checkMatch(filterText, app) && !recent.contains(app)) {
            filtered.add(app);
        }
    }

    private void addRecent(String filterText) {
        recent.retainAll(mainActivity.getAppsManager().getPkg());
        for (App app: getReversedRecent()) {
            if (checkMatch(filterText, app)) {
                filtered.add(app.getAsRecent());
            }
        }
    }

    private boolean checkMatch(String filterText, App app) {
        return app.getNick().toLowerCase().matches(filterText);
    }

    private boolean autostartFirstApp() {
        return filtered.size() == 1 && mainActivity.getAppsManager().getPkg().size() > 1 && mainActivity.getAutostartButton().isOn();
    }

    private List<App> getReversedRecent() {
        final ArrayList<App> reverseRecent = new ArrayList<App>(recent);
        Collections.reverse(reverseRecent);
        return reverseRecent;
    }

    private void viewAppList(List<App> appList) {
        final List<String> list = new ArrayList<String>();
        for (App app: appList) {
            list.add(app.getNick());
        }
        listView.setAdapter(new ArrayAdapter<String>(mainActivity, android.R.layout.simple_list_item_1, android.R.id.text1, list));
    }

    public void executeActivity(int index) {
        final App app = getApp(index);
        mainActivity.getSearchText().setActivatedColor();
        addRecent(app);
        if (app.isMenu()) {
            mainActivity.getMenu().toggle();
        } else {
            executeActivity(app);
        }
    }

    private void executeActivity(App app) {
        try {
            final Intent intent = new Intent(Intent.ACTION_MAIN, null);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setComponent(new ComponentName(app.getName(), app.getActivity()));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            mainActivity.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            mainActivity.getSearchText().setNormalColor();
        }
    }

    private App getApp(int index) {
        final List<App> pkg = mainActivity.getAppsManager().getPkg();
        return pkg.get(pkg.indexOf(filtered.get(index)));
    }

    public void addRecent(App app) {
        recent.remove(app);
        recent.add(app);
        if (recent.size() > 10) {
            recent.remove(FIRST_INDEX);
        }
    }

    public boolean showOptionsForApp(final int appIndex) {
        final App app = filtered.get(appIndex);
        if (app.equals(MENU_APP)) {
            return false;
        }
        switch (mainActivity.getMenu().getAppTypeSelector().getSelected()) {
            case normal:
                dialogFactory.showNormalOptions(app);
                break;
            case activity:
                dialogFactory.showAddExtraAppOptions(app);
                break;
            case extra:
                dialogFactory.showRemoveExtraAppOptions(app);
                break;
            case hidden:
                dialogFactory.showUnhideAppOptions(app);
                break;
        }
        return false;
    }
}

@@ -421,16 +421,66 @@
 tener %7B%0A
+    public static final String RECENT = %22recent%22;%0A
     fina
@@ -504,24 +504,24 @@
 inActivity;%0A
-
     final Li
@@ -584,16 +584,73 @@
 actory;%0A
+    private final PreferencesAdapter preferencesAdapter;%0A
     List
@@ -681,24 +681,24 @@
 ayList%3C%3E();%0A
+
     List%3CApp
@@ -705,36 +705,16 @@
 %3E recent
- = new ArrayList%3C%3E()
 ;%0A    pu
@@ -1213,16 +1213,153 @@
 ivity);%0A
+        preferencesAdapter = new PreferencesAdapter(mainActivity);%0A        recent = new ArrayList%3C%3E(preferencesAdapter.loadSet(RECENT));%0A
     %7D%0A%0A 
@@ -4747,32 +4747,84 @@
 DEX);%0A        %7D%0A
+        preferencesAdapter.saveSet(recent, RECENT);%0A
     %7D%0A%0A    publi

Save recent activities to preferences.