package com.manubet.android.touristAndroid;
  

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;



import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
//import android.util.Log;
import android.widget.Toast;


public class GeoUpdateHandler implements LocationListener {

	private MapController mapController;
	private GeoPoint position=new GeoPoint(0,0);
	private MapView mapView;
	private List<Overlay> mapOverlays;
	private OverlayItem userItem,placeItem;
	private UserOverlay userPicOverlay;
	private PlacesOverlay markerOverlay;
	private InfoProvider nearby;
	static String direccion=null;
	private Context mContext;
	
	

	public GeoUpdateHandler(Context context, MapController mapController, MapView mapView, List<Overlay> mapOverlays){
		this.mapController = mapController;
		this.mapView=mapView;
		this.mapOverlays=mapOverlays;
		mContext= context;
		nearby=new InfoProvider(mContext);
		


	}
	


	@Override
	public void onLocationChanged(Location location) {
			double lat = location.getLatitude();
			double lng =  location.getLongitude();//usar double para buscar informacion
			position = new GeoPoint((int)(lat*1E6),(int)(lng*1E6));

			mapOverlays=mapView.getOverlays();
			mapOverlays.clear();
			
			drawUser(lat,lng);
			nearby.updatePlaces(lat,lng);
			//nearby.places contiene una List de Place, ahora tengo que pintarlos
			drawPlaces(nearby.getPlaces());
			mapController.animateTo(position);
			mapView.setBuiltInZoomControls(true);
	} 
 
	private void drawUser(double lat, double lng)  {
		direccion=GeoCoding.toDirection(lat, lng);
		//Log.d("POSITION",position.toString());
		Drawable userPic = mapView.getResources().getDrawable(R.drawable.iconopersona);
        userPicOverlay = new UserOverlay(userPic, mapView.getContext());
		userItem = new OverlayItem(position, "",direccion); //creo ventana con posicion y direccion
		userPicOverlay.updateOverlay(userItem);//asignar icono
		mapOverlays=mapView.getOverlays();
		mapOverlays.add(userPicOverlay);//pinto nueva posicion
	}
	
	private void drawPlaces(LinkedList<Place> places){
		Iterator<Place> it = places.listIterator();  
		Place aux;
		
		Drawable markPic = mapView.getResources().getDrawable(R.drawable.marker);
		markerOverlay = new PlacesOverlay(markPic, mapView.getContext());
		mapOverlays=mapView.getOverlays();
		
		while (it.hasNext()){
			aux=it.next();
			placeItem  = new OverlayItem(new GeoPoint((int)(aux.getLat()*1E6),(int)(aux.getLng()*1E6)),
										 "",aux.getNombre());
			markerOverlay.addOverlay(placeItem);
			mapOverlays.add(markerOverlay);//pinto nueva posicion
			}
	}

	public void onProviderDisabled(String provider)
	{
	Toast.makeText( mContext,R.string.gpsDesactivado,Toast.LENGTH_SHORT ).show();
	}
	public void onProviderEnabled(String provider)
	{
	Toast.makeText( mContext,R.string.gpsActivo,Toast.LENGTH_SHORT ).show();
	
	}
	public void onStatusChanged(String provider, int status, Bundle extras){
		
		
	}

}
