package it.unipr.mobdev;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

public class MainActivity extends FragmentActivity {

	public static final String TAG = "MobDev-MapExample";
	
	private MarkerOptions myMarkerOptions = null;
	private Marker myMarker = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Get a handle to the Map Fragment
		GoogleMap map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

		//Check if the map has been correctly created
		if(map != null)
		{
			//Create a geographic point starting from latitude and longitude coordinate 
			LatLng parmaUniversity = new LatLng(44.76516282282244,10.311720371246338);
			
			//Enable or disable user location layer
			map.setMyLocationEnabled(true);
			
			//Move the map to a specific point
			map.moveCamera(CameraUpdateFactory.newLatLngZoom(parmaUniversity, 13));
			
			//Change map Type
			//map.setMapType(GoogleMap.MAP_TYPE_HYBRID);

			//Create new marker options
			myMarkerOptions = new MarkerOptions();
			myMarkerOptions.title("Unipr - 1");
			myMarkerOptions.snippet("Universita' degli Studi di Parma - Facolta' di Ingegneria Sede Didattica");
			myMarkerOptions.position(parmaUniversity);
			
			//Change the default icon using a drawable resource
			myMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location_place));
			
			//Example about how to use a default marker with a different color
			//myMarker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
			
			//Add the marker to the Map and return the created marker object
			myMarker = map.addMarker(myMarkerOptions);
			
			//The same kind of marker definition and add operation in one line
			//map.addMarker(new MarkerOptions().title("Unipr - A").snippet("Universita' degli Studi di Parma - Facolta' di Ingegneria Sede Didattica").position(parmaUniversity));
			
			//Create and Assign the listener for onClick event on the Map
			map.setOnMapClickListener(new OnMapClickListener() {
				
				//Listener method to receive the LatLng object associated to the clicked point on the map
				@Override
				public void onMapClick(LatLng point) {
					Log.d(MainActivity.TAG,"MainActivity ---> onMapClick: " + point.latitude + ";" + point.longitude);
				}
			});
			
			//Create and Assign the listener for onLongClick event on the Map
			map.setOnMapLongClickListener(new OnMapLongClickListener() {
				
				@Override
				public void onMapLongClick(LatLng point) {
					Log.d(MainActivity.TAG,"MainActivity ---> onMapLongClick: " + point.latitude + ";" + point.longitude);
				}
			});
			
			//Create and Assign the listener for available markers
			map.setOnMarkerClickListener(new OnMarkerClickListener() {
				
				@Override
				public boolean onMarkerClick(Marker marker) {
					
					if(myMarker != null && marker.equals(myMarker))
						Log.d(MainActivity.TAG,"MainActivity ---> onInfoWindowClick of MyMarker");
					
					//True if we want that the event has been consumed or false to propagate it
					return false;
				}
			});
			
			//Create and Assign the listener for InfoWindows of existing markers
			map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
				
				@Override
				public void onInfoWindowClick(Marker marker) {
					if(myMarker != null && marker.equals(myMarker))
						Log.d(MainActivity.TAG,"MainActivity ---> onInfoWindowClick of MyMarker");
				}
			});
		}

	}

}
