package com.manubet.android.touristAndroid;


import java.util.List; 


import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
//import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;  

public class MapTab extends MapActivity {
    private MapController mapController;
	private static MapView mapView;
	private List<Overlay> mapOverlays;
	private LocationManager locationManager;
	private SharedPreferences preferences;
	private String mode="MAP";
	private GeoUpdateHandler myGPS;
	
	 


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mapView = (MapView) findViewById(R.id.mapview);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        mapView.displayZoomControls(true);
        mapView.computeScroll();
        mapOverlays = mapView.getOverlays();
        mapController = mapView.getController();
        mapController.setZoom(17);
    } 
    
    
    
 
	private void updatePosition() {
		myGPS = new GeoUpdateHandler(this,mapController,mapView,mapOverlays); //crear el listener
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    	if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
				locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, myGPS);
	            Toast.makeText(getBaseContext(), 
	                    getResources().getString(R.string.gps_signal), 
	                    Toast.LENGTH_LONG).show();		 
    	}
    	else {
				Toast.makeText(getBaseContext(), 
                    getResources().getString(R.string.provider_not_found), 
                    Toast.LENGTH_LONG).show();
			}		
   }


 	
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.layout.menumap, menu);
	    return(super.onCreateOptionsMenu(menu));
	    
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.configure:
	    	startActivity(new Intent(this, MyPreferences.class));
			return(true);
	    case R.id.about:{
			InfoDialog aboutDialog = new InfoDialog(mapView.getContext());
			aboutDialog.show();
	    }
	        return true; 
	    case R.id.help:{
	    	try {
			InfoWeb.currentUrl=getResources().getString(R.string.homepage)+getResources().getString(R.string.manual);
			InfoWeb.load();
			}
			catch   (NullPointerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace(); 
			} 
			MainActivity.swichtTab(1);
			}
	    	return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
	public void onResume() {  
		super.onResume();
		updatePosition();
		String newmodo=preferences.getString("vistas", "MAP");

		//probar vistas
		if (!newmodo.equals(mode)) {
			mode=newmodo;

			if (newmodo.equals("TR")){
				mapView.setTraffic(true);
				mapView.setSatellite(false);
				}
			else if (newmodo.equals("MAP")){
				mapView.setTraffic(false);
				mapView.setSatellite(false);
				}
			else if (newmodo.equals("SAT")){
				
				mapView.setTraffic(false);
				mapView.setSatellite(true);
				}

			}
	}
	
	@Override
	public void onPause(){
		super.onPause(); 
		locationManager.removeUpdates(myGPS);
		myGPS=null;
	}
	

}

