package com.example.GPS_DB01;


import java.io.IOException;
import java.math.RoundingMode;
import java.text.DecimalFormat;
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.BitmapDescriptorFactory;
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 GPS_tracker_maps extends Activity implements OnMapClickListener // FragmentActivity
{
	private GoogleMap map;
	LatLngBounds.Builder builder;
	CameraUpdate cu;

	ArrayList<LatLng> arrayPoints = null;
	PolylineOptions polylineOptions;
	
	
	private TextView mTapTextView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gps_tracker_maps_layout);
		
		
		
		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
		
	 
	    
	    
	
		
		//  test();
/*		  
		  LatLng p1 = new LatLng(-34.92873, 138.59995);
		  LatLng p2 = new LatLng(-12.4258647, 130.7932231);
		  LatLng p3 = new LatLng(-37.81319, 144.96298);
		  LatLng p4 = new LatLng(-31.95285, 115.85734);
		  
		  MarkerOptions markerOptions = new MarkerOptions();
	      markerOptions.position(p1);
	      map.addMarker(markerOptions);
	      
	      markerOptions.position(p2);
	      map.addMarker(markerOptions);

	      markerOptions.position(p3);
	      map.addMarker(markerOptions);

	      markerOptions.position(p4);
	      map.addMarker(markerOptions);
*/
	      /*
	     TextReport id = new TextReport("patoche.com", "report", -34.92873 , 138.59995 , 1000, 500);
	     Control_Service.objets.add(id);
	     id = new TextReport("patoche.com", "report", -12.4258647, 130.7932231 , 1000, 500);
	     Control_Service.objets.add(id);
	     id = new TextReport("patoche.com", "report", -37.81319, 144.96298 , 1000, 500);
	     Control_Service.objets.add(id);
	     id = new TextReport("patoche.com", "report", -31.95285, 115.85734 , 1000, 500);
	     Control_Service.objets.add(id);
*/
	     
	     int n = Control_Service.objets.size();
			
			for(int i=0;i<n;i++)
			{
				Double latitude = Control_Service.objets.get(i).getLatitude();
				Double longitude = Control_Service.objets.get(i).getLongitude();
				MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)); //.title("Hello Maps");
				marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.rec));
				map.addMarker(marker);
			}
	    
			
			double distance = 0.0;
			
			for(int i=0;i<n-1;i++)
			{
				Double lat1 = Control_Service.objets.get(i).getLatitude();
				Double lng1 = Control_Service.objets.get(i).getLongitude();
				Double lat2 = Control_Service.objets.get(i+1).getLatitude();
				Double lng2 = Control_Service.objets.get(i+1).getLongitude();
				double d = Haversine.Haversine(lat1, lng1, lat2, lng2, "K"); 
				
				distance = distance + d; 
				double res2 = Haversine.CalculationByDistance(lat1, lng1, lat2, lng2);
				double res3 = Haversine.CalculationByDistance02(lat1, lng1, lat2, lng2);
			}	
			// 0    1       2       3     4 
			

			DecimalFormat df = new DecimalFormat("#.###");
			df.setRoundingMode(RoundingMode.HALF_UP);
			String arround =  df.format(distance); //Le nombre sera arrondi à 0,91239.
			//df.format(0.912384); //Le nombre sera arrondi à 0,91238.
			
	}
	
	
	public void test()
	{
		 
		int n = Control_Service.objets.size();
		
		for(int i=0;i<n;i++)
		{
			Double latitude = Control_Service.objets.get(i).getLatitude();
			Double longitude = Control_Service.objets.get(i).getLongitude();
			MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)); //.title("Hello Maps");
			marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.rec));
			map.addMarker(marker);
		}
		
//		double latitude = 2.385044;
//		double longitude = 42.486671;
//		MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");
//		marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.rec));
//		map.addMarker(marker);
		
	}
	
	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(GPS_tracker_maps.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);
		 
	}
	
/*	
	Location.distanceBetween(
		    startLatitude,
		    startLongitude,
		    endLatitude,
		    endLongitude,
		    results);
*/	
	
}