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_maps02 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);
		
		map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
		map.setMyLocationEnabled(true);
		map.getUiSettings().setZoomControlsEnabled(true);
		map.getUiSettings().setCompassEnabled(true);
		map.getUiSettings().setMyLocationButtonEnabled(true);
		map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
	     
		int n = Display_datas_run.objets.size();
			
		for(int i=0;i<n;i++)
		{
				Double latitude = Display_datas_run.objets.get(i).getLatitude();
				Double longitude = Display_datas_run.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);
		}
			
			
	}
	
	
	
	
	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_maps02.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);
		 
	}
	

	
}