package info.richardjones.mnemonicrevisionhelper.app;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import info.richardjones.mnemonicrevisionhelper.app.loader.MatchingMnemonic;
import info.richardjones.mnemonicrevisionhelper.app.loader.MnemonicLoader;


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

public class MnemonicFilter extends Filter {

    private final ArrayAdapter adapter;
    private List<MatchingMnemonic> originalData;
    private List<MatchingMnemonic> filteredData;

    public MnemonicFilter(ArrayAdapter adapter, List<MatchingMnemonic> values){
        this.adapter = adapter;
        this.originalData = values;
        this.filteredData = values;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        filteredData = (List<MatchingMnemonic>) results.values;
        adapter.notifyDataSetChanged();
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        if (constraint.toString().equals("") || constraint.length() == 1) {
            results.values = originalData;
            results.count = originalData.size();
            return results;
        }

        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(adapter.getContext());
        Boolean incSlogans = sharedPref.getBoolean(SettingsActivity.USE_SLOGAN_PREFERENCES, true);
        Boolean incSongTitles = sharedPref.getBoolean(SettingsActivity.USE_SONG_TITLE_PREFERENCES, true);

        List<MatchingMnemonic> filteredArrayNames = new ArrayList<MatchingMnemonic>();

        char[] charArray = constraint.toString().toUpperCase().toCharArray();
        Arrays.sort(charArray);
        String searchText = new String(charArray);

        for (MatchingMnemonic dataNames: originalData) {
            if (dataNames.getDetail().getOrigin().equals(MnemonicLoader.SLOGAN_ORIGIN_NAME) && !incSlogans) {
                    continue;
            }
            if (dataNames.getDetail().getOrigin().equals(MnemonicLoader.SONG_TITLE_ORIGIN_NAME) && !incSongTitles) {
                continue;
            }
            if (dataNames.getOrderedMnemonic().equals(searchText))  {
                filteredArrayNames.add(dataNames);
            }
        }

        results.count = filteredArrayNames.size();
        results.values = filteredArrayNames;

        return results;
    }

    public List<MatchingMnemonic> getFilteredData() {
        return filteredData;
    }
}

@@ -131,24 +131,49 @@
 nceManager;%0A
+import android.util.Log;%0A
 import andro
@@ -1733,24 +1733,116 @@
 ES, true);%0A%0A
+        Log.d(%22ABC: slog %22, incSlogans +%22%22);%0A        Log.d(%22ABC: song%22, incSongTitles+%22%22);%0A%0A
         List
@@ -2169,38 +2169,40 @@
 .getDetail().get
-Origin
+Category
 ().equals(Mnemon
@@ -2244,28 +2244,24 @@
 cSlogans) %7B%0A
-    
             
@@ -2333,14 +2333,16 @@
 .get
-Origin
+Category
 ().e

fix problem with filter from reading from preferences