package info.richardjones.mnemonicrevisionhelper.app;

import info.richardjones.mnemonicrevisionhelper.app.MnemonicFilter;
import info.richardjones.mnemonicrevisionhelper.app.R;
import info.richardjones.mnemonicrevisionhelper.app.loader.MatchingMnemonic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;


import java.util.List;

public class MnemonicResultsArrayAdapter extends ArrayAdapter<MatchingMnemonic> implements Filterable {

    private final Context context;
    private final LayoutInflater inflater;
    private final MnemonicFilter filter;
//    private List<MatchingMnemonic> originalData;
//    private List<MatchingMnemonic> filteredData;

    public MnemonicResultsArrayAdapter(Context context, List<MatchingMnemonic> values) {
        super(context, -1, values);
        this.context = context;
//        this.originalData = values;
//        this.filteredData = values;
        inflater = LayoutInflater.from(context);
        filter = new MnemonicFilter(this, values);

    }

    public int getCount() {
        return filter.getFilteredData().size();
    }

    public MatchingMnemonic getItem(int position) {
        return filter.getFilteredData().get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.row_layout, parent, false);
        TextView answerTV = (TextView) rowView.findViewById(R.id.firstLine);
        TextView originTV = (TextView) rowView.findViewById(R.id.secondLine);
        TextView typeTV = (TextView) rowView.findViewById(R.id.thirdLine);

        MatchingMnemonic matchingMnemonic = filter.getFilteredData().get(position);
        if (matchingMnemonic != null) {
            answerTV.setText(matchingMnemonic.getDetail().getExpandedMnemonic());
            originTV.setText("Origin: " + matchingMnemonic.getDetail().getOrigin());
            typeTV.setText("Type: " + matchingMnemonic.getDetail().getCategory());
        }
        return rowView;
    }

    @Override
    public Filter getFilter() {
        return filter;
//        Filter filter = new Filter() {
//
//            @SuppressWarnings("unchecked")
//            @Override
//            protected void publishResults(CharSequence constraint, FilterResults results) {
//                if (results.count == 0) {
//                    notifyDataSetInvalidated();
//                } else {
//                    filteredData = (List<MatchingMnemonic>) results.values;
//                    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;
//                }
//
//                List<MatchingMnemonic> filteredArrayNames = new ArrayList<MatchingMnemonic>();
//
//                for (int i = 0; i < originalData.size(); i++) {
//                    MatchingMnemonic dataNames = originalData.get(i);
//
//                    char[] charArray = constraint.toString().toUpperCase().toCharArray();
//                    Arrays.sort(charArray);
//
//                    if (dataNames.getOrderedMnemonic().toUpperCase().equals(new String(charArray).toUpperCase()))  {
//                        filteredArrayNames.add(dataNames);
//                    }
//                }
//
//                results.count = filteredArrayNames.size();
//                results.values = filteredArrayNames;
//
//                return results;
//            }
//        };
//
//        return filter;
    }
}
@@ -694,174 +694,29 @@
 nal 
-LayoutInflater inflater;%0A    private final MnemonicFilter filter;%0A//    private List%3CMatchingMnemonic%3E originalData;%0A//    private List%3CMatchingMnemonic%3E filteredData
+MnemonicFilter filter
 ;%0A%0A 
@@ -875,133 +875,8 @@
 xt;%0A
-//        this.originalData = values;%0A//        this.filteredData = values;%0A        inflater = LayoutInflater.from(context);%0A
     
@@ -2197,1746 +2197,8 @@
 er;%0A
-//        Filter filter = new Filter() %7B%0A//%0A//            @SuppressWarnings(%22unchecked%22)%0A//            @Override%0A//            protected void publishResults(CharSequence constraint, FilterResults results) %7B%0A//                if (results.count == 0) %7B%0A//                    notifyDataSetInvalidated();%0A//                %7D else %7B%0A//                    filteredData = (List%3CMatchingMnemonic%3E) results.values;%0A//                    notifyDataSetChanged();%0A//                %7D%0A//            %7D%0A//%0A//            @Override%0A//            protected FilterResults performFiltering(CharSequence constraint) %7B%0A//                FilterResults results = new FilterResults();%0A//                if (constraint.toString().equals(%22%22) %7C%7C constraint.length() == 1) %7B%0A//                    results.values = originalData;%0A//                    results.count = originalData.size();%0A//                    return results;%0A//                %7D%0A//%0A//                List%3CMatchingMnemonic%3E filteredArrayNames = new ArrayList%3CMatchingMnemonic%3E();%0A//%0A//                for (int i = 0; i %3C originalData.size(); i++) %7B%0A//                    MatchingMnemonic dataNames = originalData.get(i);%0A//%0A//                    char%5B%5D charArray = constraint.toString().toUpperCase().toCharArray();%0A//                    Arrays.sort(charArray);%0A//%0A//                    if (dataNames.getOrderedMnemonic().toUpperCase().equals(new String(charArray).toUpperCase()))  %7B%0A//                        filteredArrayNames.add(dataNames);%0A//                    %7D%0A//                %7D%0A//%0A//                results.count = filteredArrayNames.size();%0A//                results.values = filteredArrayNames;%0A//%0A//                return results;%0A//            %7D%0A//        %7D;%0A//%0A//        return filter;%0A
     

remove unused code