package ir.almahdi.sandogh_bsa_aza.vam_other_ozv;

import android.widget.Filter;

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

import ir.almahdi.sandogh_bsa_aza.vam_other_ozv.model.md_vam_other;

/**
 * Created by Hp on 3/17/2016.
 */
public class CustomFilter extends Filter{

    vam_Other_adapter adapter;
   List<md_vam_other> filterList;


    public CustomFilter(List<md_vam_other> filterList, vam_Other_adapter adapter)
    {
        this.adapter=adapter;
        this.filterList=filterList;

    }

    //FILTERING OCURS
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results=new FilterResults();

        //CHECK CONSTRAINT VALIDITY
        if(constraint != null && constraint.length() > 0)
        {
            //CHANGE TO UPPER
            constraint=constraint.toString().toUpperCase();
            //STORE OUR FILTERED karbars
            ArrayList<md_vam_other> filteredkarbars=new ArrayList<>();

            for (int i=0;i<filterList.size();i++)
            {
                //CHECK
                if(filterList.get(i).getLastname_ozv().toUpperCase().contains(constraint))
                {
                    //ADD karbar TO FILTERED karbars
                    filteredkarbars.add(filterList.get(i));
                }
            }

            results.count=filteredkarbars.size();
            results.values=filteredkarbars;
        }else
        {
            results.count=filterList.size();
            results.values=filterList;

        }


        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {

       adapter.myVamList_other= (List<md_vam_other>) results.values;

        //REFRESH
        adapter.notifyDataSetChanged();
    }
}
