package org.tndata.android.compass.util;

import android.widget.Filter;

import org.tndata.android.compass.adapter.ChooseGoalsAdapter;
import org.tndata.android.compass.model.Goal;

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


/**
 * Filters goals by title.
 *
 * @author Ismael Alonso
 * @version 1.0.0
 */
public class GoalFilter extends Filter{
    private ChooseGoalsAdapter mAdapter;
    private List<Goal> mList;


    /**
     * Constructor.
     *
     * @param adapter the adapter to be filtered.
     * @param list the original list of the adapter.
     */
    public GoalFilter(ChooseGoalsAdapter adapter, List<Goal> list){
        mAdapter = adapter;
        mList = list;
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint){
        String lowerCaseConstraint = constraint.toString().toLowerCase();
        List<Goal> output = new ArrayList<>();
        for (Goal goal:mList){
            if (goal.getTitle().toLowerCase().contains(lowerCaseConstraint)){
                output.add(goal);
            }
        }

        return new GoalFilterResults(output);
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results){
        if (results instanceof GoalFilterResults){
            mAdapter.addGoals(((GoalFilterResults)results).mOutput);
        }
    }


    /**
     * An extension of the FilterResults class that contains a list of goals.
     *
     * @author Ismael Alonso
     * @version 1.0.0
     */
    private class GoalFilterResults extends FilterResults{
        private final List<Goal> mOutput;

        /**
         * Constructor.
         *
         * @param output the result of the filtering.
         */
        GoalFilterResults(List<Goal> output){
            mOutput = output;
        }
    }
}

@@ -790,25 +790,61 @@
     
-S
+//The cons
 tr
+a
 in
-g lowerCaseC
+t is split into words%0A        String c
 onst
@@ -848,16 +848,23 @@
 nstraint
+Array%5B%5D
  = const
@@ -893,16 +893,27 @@
 erCase()
+.split(%22 %22)
 ;%0A      
@@ -965,106 +965,691 @@
     
-for (Goal goal:mList)%7B%0A            if (goal.getTitle().toLowerCase().contains(lowerCaseConstraint)
+int matchCount;%0A        //For each goal%0A        for (Goal goal:mList)%7B%0A            //The match count is reset%0A            matchCount = 0;%0A            //For each item in the constraint array%0A            for (String constraintItem:constraintArray)%7B%0A                //If either title or description match, increase the match count%0A                if (goal.getTitle().toLowerCase().contains(constraintItem) %7C%7C%0A                        goal.getDescription().toLowerCase().contains(constraintItem))%7B%0A                    matchCount++;%0A                %7D%0A            %7D%0A            //If all the words were present, add the goal to the output%0A            if (matchCount == constraintArray.length
 )%7B%0A 

Add description to filtering, smarter word splitting search