package fr.masciulli.drinks.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

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

import fr.masciulli.drinks.Holder;
import fr.masciulli.drinks.R;
import fr.masciulli.drinks.model.Drink;

import com.squareup.picasso.Picasso;

public class DrinksListAdapter extends BaseAdapter implements Filterable {
    private List<Drink> mDrinks = Collections.emptyList();
    private List<Drink> mSavedDrinks = Collections.emptyList();
    private Context mContext;
    private Filter mFilter = new Filter() {

        private List<Drink> mFilteredDrinks = new ArrayList<Drink>();

        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            mFilteredDrinks.clear();
            FilterResults results = new FilterResults();

            Log.d("adapter","performing filtering with " + charSequence);

            final String query = charSequence.toString().toLowerCase();

            for (Drink drink : mSavedDrinks) {
                if(drink.name.toLowerCase().contains(query)) {
                    Log.d("adapter","found matching drink : " + drink.name);
                    mFilteredDrinks.add(drink);
                } else {
                    Log.d("adapter", drink.name.toLowerCase() + "does not match");
                }
            }

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

            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            update((List<Drink>)filterResults.values, true);
        }
    };


    public DrinksListAdapter(Context context) {
        mContext = context;
    }

    @Override
    public int getCount() {
        return mDrinks.size();
    }

    @Override
    public Drink getItem(int i) {
        return mDrinks.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View root, ViewGroup parent) {

        if (root == null) {
            root = LayoutInflater.from(mContext).inflate(R.layout.item_drink, parent, false);
        }

        final ImageView imageView = Holder.get(root, R.id.image);
        final TextView nameView = Holder.get(root, R.id.name);

        final Drink drink = getItem(i);

        nameView.setText(drink.name);
        Picasso.with(mContext).load(drink.imageUrl).into(imageView);

        return root;
    }

    public void update(List<Drink> drinks, boolean dueToFilterOperation) {
        mDrinks = drinks;
        notifyDataSetChanged();
        if(!dueToFilterOperation) {
            mSavedDrinks = new ArrayList<Drink>(drinks);
        }
    }

    public void update(List<Drink> drinks) {
        update(drinks, false);
    }

    @Override
    public Filter getFilter() {
        return mFilter;
    }
}

@@ -1251,24 +1251,103 @@
 werCase();%0A%0A
+            final List%3CDrink%3E ingredientMatchDrinks = new ArrayList%3CDrink%3E();%0A%0A
             
@@ -1468,252 +1468,510 @@
     
-Log.d(%22adapter%22,%22found matching drink : %22 + drink.name);%0A                    mFilteredDrinks.add(drink);%0A                %7D else %7B%0A                    Log.d(%22adapter%22, drink.name.toLowerCase() + %22does not match%22);%0A                %7D%0A            %7D
+mFilteredDrinks.add(drink);%0A                %7D else %7B%0A                    // drink name does not match, we check the ingredients%0A                    for (String ingredient : drink.ingredients) %7B%0A                        if (ingredient.toLowerCase().contains(query)) %7B%0A                            ingredientMatchDrinks.add(drink);%0A                            break;%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A%0A            mFilteredDrinks.addAll(ingredientMatchDrinks);
 %0A%0A  

Allow search by ingredients