package com.example.mapspatoche;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.identity.intents.Address;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.gms.vision.barcode.Barcode.GeoPoint;

// https://www.infinitestudio.fr/2014/04/28/intro-api-google-maps-android-v2/

// https://developers.google.com/android/reference/com/google/android/gms/maps/UiSettings

public class MainActivity extends Activity implements OnMapClickListener // FragmentActivity
{
	private GoogleMap map;
	LatLngBounds.Builder builder;
	CameraUpdate cu;

	ArrayList<LatLng> arrayPoints = null;
	PolylineOptions polylineOptions;
	
	private static final LatLng ADELAIDE = new LatLng(-34.92873, 138.59995);
    private static final LatLng DARWIN = new LatLng(-12.4258647, 130.7932231);
    private static final LatLng MELBOURNE = new LatLng(-37.81319, 144.96298);
    private static final LatLng PERTH = new LatLng(-31.95285, 115.85734);
    
    private static final LatLng LOWER_MANHATTAN = new LatLng(40.722543,	-73.998585);
	private static final LatLng TIMES_SQUARE = new LatLng(40.7577, -73.9857);
	private static final LatLng BROOKLYN_BRIDGE = new LatLng(40.7057, -73.9964);

	private TextView mTapTextView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		
		arrayPoints = new ArrayList<LatLng>();
		
		map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
			
		map.setMyLocationEnabled(true);
		
		
		map.getUiSettings().setZoomControlsEnabled(true);
		map.getUiSettings().setCompassEnabled(true);
		map.getUiSettings().setMyLocationButtonEnabled(true);
		
		//map.setZoomControlsEnabled(true); 
	//	map.setMapType(map.MAP_TYPE_SATELLITE);
		map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		
		
		//map.addPolyline((new PolylineOptions()).add(TIMES_SQUARE, BROOKLYN_BRIDGE, LOWER_MANHATTAN,	TIMES_SQUARE).width(5).color(Color.BLUE).geodesic(true));
		
		// move camera to zoom on map
		//map.moveCamera(CameraUpdateFactory.newLatLngZoom(LOWER_MANHATTAN,13));
		
		
		
		/**call the map set up method*/
		//mSetUpMap();
		
		test01();
		
		//https://www.google.fr/maps/dir/43.771493,2.1850038/43.7765885,2.18792/@43.7740345,2.1840538,17z/am=t/data=!3m1!4b1!4m9!4m8!1m5!3m4!1m2!1d2.187663!2d43.7755792!3s0x12ade1d391608785:0x79769d1f7f513c42!1m0!3e2
		
		//Button buttonGetInfo = (Button)findViewById(R.id.btn_infos);
	 
		  mTapTextView = (TextView) findViewById(R.id.tv_infos);
	    
		//http://android-er.blogspot.fr/2011/07/get-the-coordinates-latitude-and.html
		
	 
	    
	    
	/*    
	    Button.OnClickListener buttonGetInfoOnClickListener  = new Button.OnClickListener()
	    {

	     @Override
	     public void onClick(View v) {
	    
	      
	      GeoPoint mapCenter = myMap.getMapCenter();
	      
	      float centerLatitude = (float)(mapCenter.getLatitudeE6())/1000000;
	      float centerLongitude = (float)(mapCenter.getLongitudeE6())/1000000;
	      
	      float latitudeSpan = (float)(myMap.getLatitudeSpan())/1000000;
	      float longitudeSpan = (float)(myMap.getLongitudeSpan()/1000000);
	      
	      GeoPoint mapTopLeft = myMap.getProjection().fromPixels(0, 0);
	      float topLatitude = (float)(mapTopLeft.getLatitudeE6())/1000000;
	      float leftLongitude = (float)(mapTopLeft.getLongitudeE6())/1000000;
	      
	      GeoPoint mapBottomRight
	      = myMap.getProjection().fromPixels(myMap.getWidth(), myMap.getHeight());
	      float bottomLatitude = (float)(mapBottomRight.getLatitudeE6())/1000000;
	      float rightLongitude = (float)(mapBottomRight.getLongitudeE6())/1000000;

	      int zoomLevel = myMap.getZoomLevel();
	      
	      String info =
	        "Center: " + centerLatitude + "/" + centerLongitude + "\n"
	        + "Top Left: " + topLatitude + "/" + leftLongitude + "\n"
	        + "Bottom Right: " + bottomLatitude + "/" + rightLongitude + "\n"
	        + "latitudeSpan: " + latitudeSpan + "\n"
	        + "longitudeSpan: " + longitudeSpan + "\n"
	        + "zoomLevel: " + zoomLevel;
	      
	      Toast.makeText(AndroidMapActivity.this,
	        info,
	        Toast.LENGTH_LONG).show();
	     }};
		
		*/
		
	}
	
	
	public void test01()
	{
		
		ArrayList<LatLng> coordList = new ArrayList<LatLng>();

		
		coordList.add(new LatLng(0, 0));
		coordList.add(new LatLng(4, 1));
		coordList.add(new LatLng(2.5, 2));

		PolylineOptions polylineOptions = new PolylineOptions();

		// Create polyline options with existing LatLng ArrayList
		polylineOptions.addAll(coordList);
		polylineOptions
		 .width(5)
		 .color(Color.RED);

		// Adding multiple points in map using polyline and arraylist
		map.addPolyline(polylineOptions);
		
		map.setOnMapClickListener(this);
		
		
	    /************* Zoom sur la France *************************/
		
		Geocoder gc = new Geocoder(MainActivity.this);
		List<android.location.Address> list = null;
		try 
		{
			//list = gc.getFromLocationName("1600 Amphitheatre Parkway, Mountain View, CA", 1);
			list = gc.getFromLocationName("FRANCE",1);
		} 
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		  android.location.Address address = list.get(0);

		  double lat = address.getLatitude();
		  double lng = address.getLongitude();
		
		  //LatLng Paris = new LatLng(48.862725,	2.287592);
		  LatLng Paris = new LatLng(lat,	lng);
		  
		  moveToSelectedArea(Paris, 5);
			//Paris, France
			//Latitude : 48.862725 | Longitude : 2.287592
			
		
	}
	
	
	private void moveToSelectedArea(LatLng position, int ZOOM_LEVEL)
	{
	    if (map != null) 
	    {
	        //int ZOOM_LEVEL = 10;
	        // In your case, if you want to apply different zoom for different type, make a switch case based on type and set the zoom value
	        map.animateCamera(CameraUpdateFactory.newLatLngZoom(position, ZOOM_LEVEL));
	    }
	}
	
	/**create method to set map view*/
	public void mSetUpMap() {
		/**clear the map before redraw to them*/
		map.clear();
		/**Create dummy Markers List*/
		List<Marker> markersList = new ArrayList<Marker>();
		Marker Delhi = map.addMarker(new MarkerOptions().position(new LatLng(
				28.61, 77.2099)).title("Delhi"));
		Marker Chaandigarh = map.addMarker(new MarkerOptions().position(new LatLng(
				30.75, 76.78)).title("Chandigarh"));
		Marker SriLanka = map.addMarker(new MarkerOptions().position(new LatLng(
				7.000, 81.0000)).title("Sri Lanka"));
		Marker America = map.addMarker(new MarkerOptions().position(new LatLng(
				38.8833, 77.0167)).title("America"));
		Marker Arab = map.addMarker(new MarkerOptions().position(new LatLng(
				24.000, 45.000)).title("Arab"));
		
		Marker Realmont = map.addMarker(new MarkerOptions().position(new LatLng(
				43.776, 2.189)).title("Realmont"));
		
		
		/**Put all the markers into arraylist*/
		markersList.add(Delhi);
		markersList.add(SriLanka);
		markersList.add(America);
		markersList.add(Arab);
		markersList.add(Chaandigarh);
		
		markersList.add(Realmont);
		
		/**create for loop for get the latLngbuilder from the marker list*/
		builder = new LatLngBounds.Builder();
		for (Marker m : markersList)
		{
			builder.include(m.getPosition());
		}
		
		/**initialize the padding for map boundary*/
		int padding = 50;
		
		/**create the bounds from latlngBuilder to set into map camera*/
		LatLngBounds bounds = builder.build();
		
		/**create the camera with bounds and padding to set into map*/
		cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
		
		/**call the map call back to know map is loaded or not*/
		map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() 
		{
				@Override
				public void onMapLoaded() 
				{
					/**set animated zoom camera into map*/
					map.animateCamera(cu);
					
				}
		});
	}


	@Override
	public void onMapClick(LatLng point) 
	{
		// TODO Auto-generated method stub
		 mTapTextView.setText("long pressed, point=" + point);
		 
		 moveToSelectedArea(point, 14);
		 
	}
	
	
}