package com.example.patrice.rerservation01;


import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
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 com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

public class CustomAdapter extends BaseAdapter implements Filterable
{

	Context context;
	ArrayList<Listview_games> countrylist;
	ArrayList<Listview_games> mStringFilterList;
	ValueFilter valueFilter;

	private DisplayImageOptions options;

	CustomAdapter(Context context, ArrayList<Listview_games> countrylist)
	{
		this.context = context;
		this.countrylist = countrylist;
		mStringFilterList = countrylist;

		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_error)
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.considerExifParams(true)
				//.displayer(new CircleBitmapDisplayer(Color.WHITE, 5))
				.displayer(new SimpleBitmapDisplayer())

				.build();

	}

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

	@Override
	public Object getItem(int position) {
		return countrylist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return countrylist.indexOf(getItem(position));
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{

		LayoutInflater mInflater = (LayoutInflater) context
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

		convertView = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.row_listview_item02, null);

			TextView id = (TextView) convertView.findViewById(R.id.id);
			TextView nom = (TextView) convertView.findViewById(R.id.nom);
			ImageView image = (ImageView) convertView.findViewById(R.id.image);
			TextView barcode = (TextView) convertView.findViewById(R.id.barcode);
			TextView description = (TextView) convertView.findViewById(R.id.description);
			TextView emprunteur = (TextView) convertView.findViewById(R.id.emprunteur);

			Listview_games country = countrylist.get(position);

			id.setText(country.getId());
			nom.setText(country.getNom());
			ImageLoader.getInstance().displayImage(country.getImage() , image, options);

			barcode.setText(country.getBarcode());
			description.setText(country.getStatut());

			if(country.getStatut().equals("disponible"))
			{
				convertView.setBackgroundColor(Color.parseColor("#FF25FF51"));
				emprunteur.setText("");
			}

			// le jeu a été retirer par un client = emprunteur
			else
			{
				convertView.setBackgroundColor(Color.parseColor("#FFCCCB4C"));
				emprunteur.setText(country.getEmprunteur());
			}



		}
		return convertView;
	}

	@Override
	public Filter getFilter()
	{
		if (valueFilter == null)
		{
			valueFilter = new ValueFilter();
		}
		return valueFilter;
	}

	private class ValueFilter extends Filter {
		@Override
		protected FilterResults performFiltering(CharSequence constraint)
		{
			FilterResults results = new FilterResults();

			if (constraint != null && constraint.length() > 0)
			{
				ArrayList<Listview_games> filterList = new ArrayList<Listview_games>();

				for (int i = 0; i < mStringFilterList.size(); i++)
				{
					if ((mStringFilterList.get(i).getNom().toUpperCase()).contains(constraint.toString().toUpperCase()))
					{

						Listview_games country = new Listview_games(
								mStringFilterList.get(i).getId(),
								mStringFilterList.get(i).getNom(),
								mStringFilterList.get(i).getImage(),
								mStringFilterList.get(i).getBarcode(),
								mStringFilterList.get(i).getStatut(),
								mStringFilterList.get(i).getEmprunteur()
						);

						filterList.add(country);


					}
				}

				results.count = filterList.size();
				results.values = filterList;
			}

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

			return results;

		}

		@Override
		protected void publishResults(CharSequence constraint,	FilterResults results)
		{
			countrylist = (ArrayList<Listview_games>) results.values;
			notifyDataSetChanged();
		}

	}

}
