package org.azavea.otm.ui;

import java.util.LinkedHashMap;
import java.util.Map;

import org.azavea.otm.App;
import org.azavea.otm.R;
import org.azavea.otm.FilterManager;
import org.azavea.otm.adapters.SpeciesAdapter;
import org.azavea.otm.data.Species;
import org.json.JSONException;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class SpeciesListDisplay extends ListActivity {
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		FilterManager search = App.getFilterManager();
		
		if (search.getSpecies().size() > 0) {
			renderSpeciesList();
		} else {
			search.loadSpeciesList(new Callback() {

				@Override
				public boolean handleMessage(Message msg) {
					if (msg.getData().getBoolean("success")) {
						renderSpeciesList();
					} else {
						Toast.makeText(App.getInstance(), "Could not get species list", 
								Toast.LENGTH_SHORT).show();
					}
					return true;
				}
				
			});
		}
	}
	
	private void renderSpeciesList() {
		LinkedHashMap<Integer, Species> list = App.getFilterManager().getSpecies();
		Species[] species = new Species[list.size()];
		int i = 0;
		for (Map.Entry<Integer,Species> entry : list.entrySet()) {
			species[i] = entry.getValue();
			i++;
		}
		SpeciesAdapter adapter = new SpeciesAdapter(this, R.layout.species_list_row, 
				species);
		Log.d(App.LOG_TAG, list.size() + " species loaded");
		setListAdapter(adapter);
		
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Species selection = (Species)l.getItemAtPosition(position);
		Intent result = new Intent();
		// The underlying JSONObject of Species is not serializable, so just 
		// return the information to use for querying
		try {
			result.putExtra("species_id", selection.getId());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		setResult(RESULT_OK, result);
		finish();
	}	    
  }

@@ -433,16 +433,79 @@
 essage;%0A
+import android.text.Editable;%0Aimport android.text.TextWatcher;%0A
 import a
@@ -543,24 +543,56 @@
 .view.View;%0A
+import android.widget.EditText;%0A
 import andro
@@ -1702,16 +1702,710 @@
 ter);%0A%09%09
+setContentView(R.layout.species_list_selector);%0A%09%09setupFiltering(adapter);%0A%09%7D%0A%09%0A%09private void setupFiltering(final SpeciesAdapter adapter) %7B%0A%09%09EditText filterEditText = (EditText) findViewById(R.id.species_filter_text);%0A%09%09filterEditText.addTextChangedListener(new TextWatcher() %7B%0A%09%09   @Override%0A%09%09    public void onTextChanged(CharSequence s, int start, int before,%0A%09%09    %09%09int count) %7B%0A%09%09%09   //adapter.getFilter().filter(s.toString());%0A%09%09    %7D%0A%0A%09%09%09@Override%0A%09%09%09public void afterTextChanged(Editable s) %7B%0A%09%09%09%09adapter.getFilter().filter(s);%0A%09%09%09%09adapter.notifyDataSetChanged();%0A%09%09%09%7D%0A%0A%09%09%09@Override%0A%09%09%09public void beforeTextChanged(CharSequence s, int start, int count,%0A%09%09%09%09%09int after) %7B%7D%0A%09%09%09%7D); %09%09
 %0A%09%7D%0A%09%0A%09@

Add a textwatcher to filter edittext