package com.icecondor.nest;

import java.util.ArrayList;
import java.util.ListIterator;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;

public class FlockOverlay extends ItemizedOverlay<BirdItem> {

	private ArrayList<BirdItem> birds = new ArrayList<BirdItem>();
	private String appTag = "FlockOverlay";
	private BirdItem last_bird;
	private Context app;
	
	public FlockOverlay(Drawable defaultMarker, Context app) {
		super(boundCenterBottom(defaultMarker));
		this.app = app;
	}
	
	public void add(BirdItem bird) {
		birds.add(bird);
		populate();
	}
	
	@Override
	protected BirdItem createItem(int i) {
		return birds.get(i);
	}

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

	public boolean contains(String guid) {
		ListIterator<BirdItem> list = birds.listIterator();
		while(list.hasNext()) {
			BirdItem bird = list.next();
			if(bird.getTitle().equals(guid))
				return true;
		}
		return false;
	}
	
	public boolean onTap(GeoPoint p, MapView mapView) {
		Log.i(appTag, "got TAP for map location "+p);
		if(last_bird != null) {
			setFocus(null);
		}
		return true;
	}
	
	public boolean onTap(int index) {
		Log.i(appTag, "got TAP on item#"+index);
		last_bird = birds.get(index);
		setFocus(last_bird);
		Toast.makeText(app, last_bird.getSnippet(), Toast.LENGTH_SHORT).show();
		return true;
	}
	
	public void draw(Canvas canvas, MapView view, boolean shadow ) {
		super.draw(canvas, view, shadow);
		//draw other stuffs...

	} 
}

@@ -214,16 +214,49 @@
 il.Log;%0A
+import android.view.MotionEvent;%0A
 import a
@@ -1582,32 +1582,245 @@
 turn true;%0A%09%7D%0A%09%0A
+%09public boolean onTouchEvent(MotionEvent event, MapView mapView) %7B%0A%09%09// Android Bug - app crashes if this method does not exist%0A%09%09// Tries to load an element from array when array is empty%0A%09%09return false;%0A%09%09%0A%09%7D%0A%09%0A
 %09public void dra

work around ItemizedOverlay bug