package com.example.geo_api12;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.location.Address;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

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.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;


import com.google.android.gms.maps.model.LatLng;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends FragmentActivity 
{

	AutoCompleteTextView atvPlaces;
	AutoCompleteTextView arrivalPlaces;
	
	autocomplete_PlacesTask autocomplete_PlacesTask;
	autocomplete_ParserTask autocomplete_ParserTask;
	    
	PlacesTask_departure placesTask_departure;
	ParserTask_departure parserTask_departure;
		
	PlacesTask_destination placesTask_destination;
	ParserTask_destination parserTask_destination;
	

	static GoogleMap map;
	
	AutoCompleteTextView depart;
	AutoCompleteTextView arrive;
	
	String departure = "";
	String arrival = "";

	static String global_departure = "";
	static String global_arrival = "";
	static String global_data = "";
	
	
	double latitude1 =0.0;
	double longitude1 = 0.0;
	double latitude2 = 0.0;
	double longitude2 = 0.0;
	
	ArrayList<Double> list_coord = new ArrayList<Double>();
	
	static ArrayList<LatLng> locations = new ArrayList();
	
	static ArrayList<LatLng> xxxxx = new ArrayList();
	
	int flag = 0;
	
	
	private ProgressDialog progressdialog;
	
	/*************************/
	
	//GoogleMap googleMap;
	

	ArrayList markerPoints;
	Dialog dialog;
	AlertDialog.Builder alertDialog;
	EditText et_departure, et_destination;
	Button btn_find, btn_cancel, btn_direction;
	String test_message_address_departure = "";
	String test_message_address_destination = "";
	boolean flag_address_departure = true;
	boolean flag_address_destination = true;
	int test_sequence = 0;
	TextView tvDistanceDuration;
	

	ArrayList<String> names;
    JSONObject json;
    String url;
  
    ArrayAdapter<String> adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		init();
		
	}
	
	/************************************ AUTOCOMPLETE ***********************************************************/
		
	public void init()
	{
			SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 
				
			map = mapFragment.getMap();
			
			map.setMyLocationEnabled(true);		
			map.setIndoorEnabled(true);
			map.getUiSettings().setZoomControlsEnabled(true);
			map.getUiSettings().setMyLocationButtonEnabled(true);
			map.getUiSettings().setCompassEnabled(true);
			map.getUiSettings().setAllGesturesEnabled(true);

			//ProgressDialog progressdialog = new ProgressDialog(MainActivity.this);

		
		    depart = (AutoCompleteTextView) findViewById(R.id.departure_places);
	        depart.setThreshold(5);
	        
	        arrive = (AutoCompleteTextView) findViewById(R.id.arrival_places);
	        arrive.setThreshold(5);
	        
	        depart.addTextChangedListener(new TextWatcher()
	        {
	        	 
		            @Override
		            public void onTextChanged(CharSequence s, int start, int before, int count)
		            {
		            	placesTask_departure = new PlacesTask_departure();
		            	placesTask_departure.execute(s.toString());
		            }
		 
		            @Override
		            public void beforeTextChanged(CharSequence s, int start, int count, int after)
		            {
		                
		            }
		 
		            @Override
		            public void afterTextChanged(Editable s) 
		            {
		              
		            }
	        });
	        
	        arrive.addTextChangedListener(new TextWatcher()
	        {
	        	 
		            @Override
		            public void onTextChanged(CharSequence s, int start, int before, int count)
		            {
		            	placesTask_destination = new PlacesTask_destination();
		            	placesTask_destination.execute(s.toString());
		            }
		 
		            @Override
		            public void beforeTextChanged(CharSequence s, int start, int count, int after)
		            {
		                
		            }
		 
		            @Override
		            public void afterTextChanged(Editable s) 
		            {
		              
		            }
	        });
	        
	       
	    	
	
	
			
		btn_direction = (Button) findViewById(R.id.btn_calculation);
    	btn_direction.setOnClickListener(new View.OnClickListener() 
        {
                @Override
                public void onClick(View v) 
                {
                	Toast.makeText(getApplicationContext(), "work 12 ...", Toast.LENGTH_LONG).show();
                	
                		if (depart.getText().toString().isEmpty() || arrive.getText().toString().isEmpty())
                        {
                            Toast.makeText(getApplicationContext(), "Text fields cannot be empty", Toast.LENGTH_SHORT).show();
                        } 
                        else 
                        {
                        	
                      
                        	departure = depart.getText().toString();
                        	arrival = arrive.getText().toString();
                        	
                        	ArrayList<String> list=new ArrayList<String>();  
                        	
                        	departure = departure.replaceAll(",", " ");
                        	arrival = arrival.replaceAll(",", " ");
                        	
                        	
                        	list.add(departure);  
                        	list.add(arrival);  
                        	
                        	map.clear();
                        	
                        	//GeocoderTask xxx = new aaaGeocoderTask();
                            //xxx.execute(list);

                        	
                        	//direction_ParserTask direction_parserTask = new direction_ParserTask();
             	            //direction_parserTask.execute("result");
                        	
                        }	
                	
                }
        });
			
		
		
	}
	
	
	private String downloadUrl_departure(String strUrl) throws IOException
	{
        String data = "";
        InputStream iStream = null;
        HttpURLConnection urlConnection = null;
        try{
                URL url = new URL(strUrl);                

                // Creating an http connection to communicate with url 
                urlConnection = (HttpURLConnection) url.openConnection();

                // Connecting to url 
                urlConnection.connect();

                // Reading data from url 
                iStream = urlConnection.getInputStream();

                BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

                StringBuffer sb  = new StringBuffer();

                String line = "";
                while( ( line = br.readLine())  != null){
                        sb.append(line);
                }
                
                data = sb.toString();

                br.close();

        }catch(Exception e){
                Log.d("Exception while downloading url", e.toString());
        }finally{
                iStream.close();
                urlConnection.disconnect();
        }
        return data;
     }	
	
		private class PlacesTask_departure extends AsyncTask<String, Void, String>{

			@Override
			protected String doInBackground(String... place) {
				// For storing data from web service
				String data = "";
				
				// Obtain browser key from https://code.google.com/apis/console
				String key = "AIzaSyBXw9vydHGGjBOYSeM9i-Xh8f9Mk0xzg1k";
				
				String input="";
				
				try {
					input = "input=" + URLEncoder.encode(place[0], "utf-8");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}		
				
				
				// place type to be searched
				String types = "types=geocode";
				
				// Sensor enabled
				String sensor = "sensor=false";			
				
				// Building the parameters to the web service
				String parameters = input+"&"+types+"&"+sensor+"&key="+key;
				parameters = input+"&"+types+"&"+sensor+"&key="+key+"&components=country:FR";

				//Change input+”&”+types+”&”+sensor+”&”+key; ==> input+”&”+types+”&”+sensor+”&key=”+key;
				
				// Output format
				String output = "json";
				
				// Building the url to the web service
				String url = "https://maps.googleapis.com/maps/api/place/autocomplete/"+output+"?"+parameters;
				
				//url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=fr&key=AIzaSyBXw9vydHGGjBOYSeM9i-Xh8f9Mk0xzg1k";
		
				try{
					// Fetching the data from web service in background
					data = downloadUrl_departure(url);
				}catch(Exception e){
	                Log.d("Background Task",e.toString());
				}
				return data;		
				
				//  "error_message" : "This API project is not authorized to use this API. Please ensure this API is activated in the Google Developers Console: https://console.developers.google.com/apis/api/places_backend?project=_",   "predictions" : [],   "status" : "REQUEST_DENIED"}
			}
			
			@Override
			protected void onPostExecute(String result) {			
				super.onPostExecute(result);
				
				// Creating ParserTask
				parserTask_departure = new ParserTask_departure();
				
				// Starting Parsing the JSON string returned by Web Service
				parserTask_departure.execute(result);
			}		
		}
	
	
		/** A class to parse the Google Places in JSON format */
	    private class ParserTask_departure extends AsyncTask<String, Integer, List<HashMap<String,String>>>
	    {

	    	JSONObject jObject;
	    	
			@Override
			protected List<HashMap<String, String>> doInBackground(String... jsonData) 
			{			
				
				List<HashMap<String, String>> places = null;
				
	            PlaceJSONParser placeJsonParser = new PlaceJSONParser();
	            
	            try{
	            	jObject = new JSONObject(jsonData[0]);
	            	
	            	// Getting the parsed data as a List construct
	            	places = placeJsonParser.parse(jObject);

	            }catch(Exception e){
	            	Log.d("Exception",e.toString());
	            }
	            return places;
			}
			
			@Override
			protected void onPostExecute(List<HashMap<String, String>> result) 
			{			
				
					String[] from = new String[] { "description"};
					int[] to = new int[] { android.R.id.text1 };
					
					// Creating a SimpleAdapter for the AutoCompleteTextView			
					SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), result, android.R.layout.simple_list_item_1, from, to);				
					
					// Setting the adapter
					depart.setAdapter(adapter);
			}			
	    }    
	
	
	    private String autocomplet_downloadUrl(String strUrl) throws IOException
	    {
	        String data = "";
	        InputStream iStream = null;
	        HttpURLConnection urlConnection = null;
	        try{
	                URL url = new URL(strUrl);                

	                // Creating an http connection to communicate with url 
	                urlConnection = (HttpURLConnection) url.openConnection();

	                // Connecting to url 
	                urlConnection.connect();

	                // Reading data from url 
	                iStream = urlConnection.getInputStream();

	                BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

	                StringBuffer sb  = new StringBuffer();

	                String line = "";
	                while( ( line = br.readLine())  != null){
	                        sb.append(line);
	                }
	                
	                data = sb.toString();

	                br.close();

	        }catch(Exception e){
	                Log.d("Exception while downloading url", e.toString());
	        }finally{
	                iStream.close();
	                urlConnection.disconnect();
	        }
	        return data;
	 }	
	
	
	    // Fetches all places from GooglePlaces AutoComplete Web Service
		private class autocomplete_PlacesTask extends AsyncTask<String, Void, String>
		{

			@Override
			protected String doInBackground(String... place) {
				// For storing data from web service
				String data = "";
				
				// Obtain browser key from https://code.google.com/apis/console
				String key = "AIzaSyBXw9vydHGGjBOYSeM9i-Xh8f9Mk0xzg1k";
				
				String input="";
				
				try {
					input = "input=" + URLEncoder.encode(place[0], "utf-8");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}		
				
				
				// place type to be searched
				String types = "types=geocode";
				
				// Sensor enabled
				String sensor = "sensor=false";			
				
				// Building the parameters to the web service
				String parameters = input+"&"+types+"&"+sensor+"&"+key;
				parameters = input+"&"+types+"&"+sensor+"&key="+key+"&components=country:FR";
				//String parameters = input+"&"+types+"&"+sensor+"&"+key+"&components=country:fr";
				
				
				//&components=country
				
				// Output format
				String output = "json";
				
				// Building the url to the web service
				String url = "https://maps.googleapis.com/maps/api/place/autocomplete/"+output+"?"+parameters;
		
				try{
					// Fetching the data from web service in background
					data = autocomplet_downloadUrl(url);
				}catch(Exception e){
	                Log.d("Background Task",e.toString());
				}
				return data;		
			}
			
			@Override
			protected void onPostExecute(String result) {			
				super.onPostExecute(result);
				
				// Creating ParserTask
				autocomplete_ParserTask = new autocomplete_ParserTask();
				
				// Starting Parsing the JSON string returned by Web Service
				autocomplete_ParserTask.execute(result);
			}		
		}
	
	
		 /** A method to download json data from url */
	    private String downloadUrl_destination(String strUrl) throws IOException{
	        String data = "";
	        InputStream iStream = null;
	        HttpURLConnection urlConnection = null;
	        try{
	                URL url = new URL(strUrl);                

	                // Creating an http connection to communicate with url 
	                urlConnection = (HttpURLConnection) url.openConnection();

	                // Connecting to url 
	                urlConnection.connect();

	                // Reading data from url 
	                iStream = urlConnection.getInputStream();

	                BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

	                StringBuffer sb  = new StringBuffer();

	                String line = "";
	                while( ( line = br.readLine())  != null){
	                        sb.append(line);
	                }
	                
	                data = sb.toString();

	                br.close();

	        }catch(Exception e){
	                Log.d("Exception while downloading url", e.toString());
	        }finally{
	                iStream.close();
	                urlConnection.disconnect();
	        }
	        return data;
	     }	
		
	
		/** A class to parse the Google Places in JSON format */
	    private class autocomplete_ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>
	    {

	    	JSONObject jObject;
	    	
			@Override
			protected List<HashMap<String, String>> doInBackground(String... jsonData)
			{			
				
				List<HashMap<String, String>> places = null;
				
	            PlaceJSONParser placeJsonParser = new PlaceJSONParser();
	            
	            try{
	            	jObject = new JSONObject(jsonData[0]);
	            	
	            	// Getting the parsed data as a List construct
	            	places = placeJsonParser.parse(jObject);

	            }catch(Exception e){
	            	Log.d("Exception",e.toString());
	            }
	            return places;
			}
			
			@Override
			protected void onPostExecute(List<HashMap<String, String>> result) 
			{			
				
					String[] from = new String[] { "description"};
					int[] to = new int[] { android.R.id.text1 };
					
					// Creating a SimpleAdapter for the AutoCompleteTextView			
					SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), result, android.R.layout.simple_list_item_1, from, to);				
					
					// Setting the adapter
					depart.setAdapter(adapter);
			}			
	    }    
	
	
	 // Fetches all places from GooglePlaces AutoComplete Web Service
		private class PlacesTask_destination extends AsyncTask<String, Void, String>{

			@Override
			protected String doInBackground(String... place) {
				// For storing data from web service
				String data = "";
				
				// Obtain browser key from https://code.google.com/apis/console
				String key = "AIzaSyBXw9vydHGGjBOYSeM9i-Xh8f9Mk0xzg1k";
				
				String input="";
				
				try {
					input = "input=" + URLEncoder.encode(place[0], "utf-8");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}		
				
				
				// place type to be searched
				String types = "types=geocode";
				
				// Sensor enabled
				String sensor = "sensor=false";			
				
				// Building the parameters to the web service
				String parameters = input+"&"+types+"&"+sensor+"&key="+key;
				parameters = input+"&"+types+"&"+sensor+"&key="+key+"&components=country:FR";

				//Change input+”&”+types+”&”+sensor+”&”+key; ==> input+”&”+types+”&”+sensor+”&key=”+key;
				
				// Output format
				String output = "json";
				
				// Building the url to the web service
				String url = "https://maps.googleapis.com/maps/api/place/autocomplete/"+output+"?"+parameters;
				
				//url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=fr&key=AIzaSyBXw9vydHGGjBOYSeM9i-Xh8f9Mk0xzg1k";
		
				try{
					// Fetching the data from web service in background
					data = downloadUrl_destination(url);
				}catch(Exception e){
	                Log.d("Background Task",e.toString());
				}
				return data;		
				
				//  "error_message" : "This API project is not authorized to use this API. Please ensure this API is activated in the Google Developers Console: https://console.developers.google.com/apis/api/places_backend?project=_",   "predictions" : [],   "status" : "REQUEST_DENIED"}
			}
			
			@Override
			protected void onPostExecute(String result) {			
				super.onPostExecute(result);
				
				// Creating ParserTask
				parserTask_destination = new ParserTask_destination();
				
				// Starting Parsing the JSON string returned by Web Service
				parserTask_destination.execute(result);
			}		
		}
		
	
		/** A class to parse the Google Places in JSON format */
	    private class ParserTask_destination extends AsyncTask<String, Integer, List<HashMap<String,String>>>{

	    	JSONObject jObject;
	    	
			@Override
			protected List<HashMap<String, String>> doInBackground(String... jsonData) {			
				
				List<HashMap<String, String>> places = null;
				
	            PlaceJSONParser placeJsonParser = new PlaceJSONParser();
	            
	            try{
	            	jObject = new JSONObject(jsonData[0]);
	            	
	            	// Getting the parsed data as a List construct
	            	places = placeJsonParser.parse(jObject);

	            }catch(Exception e){
	            	Log.d("Exception",e.toString());
	            }
	            return places;
			}
			
			@Override
			protected void onPostExecute(List<HashMap<String, String>> result) {			
				
					String[] from = new String[] { "description"};
					int[] to = new int[] { android.R.id.text1 };
					
					// Creating a SimpleAdapter for the AutoCompleteTextView			
					SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), result, android.R.layout.simple_list_item_1, from, to);				
					
					// Setting the adapter
					arrive.setAdapter(adapter);
			}			
	    }    
	
	
	
	
	/*****************************************************************************************************/
	
    
	    private String aaadownloadUrl(String strUrl) throws IOException{
	        String data = "";
	        InputStream iStream = null;
	        HttpURLConnection urlConnection = null;
	        try{
	                URL url = new URL(strUrl);


	                // Creating an http connection to communicate with url 
	                urlConnection = (HttpURLConnection) url.openConnection();

	                // Connecting to url 
	                urlConnection.connect();

	                // Reading data from url 
	                iStream = urlConnection.getInputStream();

	                BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

	                StringBuffer sb  = new StringBuffer();

	                String line = "";
	                while( ( line = br.readLine())  != null){
	                        sb.append(line);
	                }

	                data = sb.toString();

	                br.close();

	        }catch(Exception e){
	                Log.d("Exception while downloading url", e.toString());
	        }finally{
	                iStream.close();
	                urlConnection.disconnect();
	        }

	        return data;
	        
		}


	

	    private class aaaDownloadTask extends AsyncTask<String, Integer, String>{

	            String data = null;

	            // Invoked by execute() method of this object
	            @Override
	            protected String doInBackground(String... url) {
	                    try{                    		
	                            data = aaadownloadUrl(url[0]);
	                    }catch(Exception e){
	                             Log.d("Background Task",e.toString());
	                    }
	                    return data;
	            }

	            // Executed after the complete execution of doInBackground() method
	            @Override
	            protected void onPostExecute(String result){
	            		
	            		// Instantiating ParserTask which parses the json data from Geocoding webservice
	            		// in a non-ui thread
	            		aaaParserTask aaaparserTask = new aaaParserTask();

	                    // Start parsing the places in JSON format
	                    // Invokes the "doInBackground()" method of the class ParseTask
	                    aaaparserTask.execute(result);
	            }

	    }
	


	    class aaaParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>
		{

			JSONObject jObject;
			
			// Invoked by execute() method of this object
			@Override
			protected List<HashMap<String,String>> doInBackground(String... jsonData)
			{
			
				List<HashMap<String, String>> places = null;			
				GeocodeJSONParser parser = new GeocodeJSONParser();
	        
		        try{
		        	jObject = new JSONObject(jsonData[0]);
		        	
		          
		            places = parser.parse(jObject);
		            
		        }catch(Exception e){
		                Log.d("Exception",e.toString());
		        }
		        return places;
			}
			
		
			@Override
			protected void onPostExecute(List<HashMap<String,String>> list)
			{			
				
				// Clears all the existing markers			
				//mMap.clear();
				
				for(int i=0;i<list.size();i++)
				{
				
					// Creating a marker
		            MarkerOptions markerOptions = new MarkerOptions();
		            
		            // Getting a place from the places list
		            HashMap<String, String> hmPlace = list.get(i);
		
		            // Getting latitude of the place
		            double lat = Double.parseDouble(hmPlace.get("lat"));	            
		            
		            // Getting longitude of the place
		            double lng = Double.parseDouble(hmPlace.get("lng"));
		            
/*		            
		        	double latitude1;
		        	double longitude1;
		        	double latitude2;
		        	double longitude2;
*/		        	
		            
		            locations.add(new LatLng(lat, lng));

/*		            
		            if(flag == 1)
		            {
		            	String str_conv = String.valueOf(flag);
		            	Toast.makeText(getApplicationContext(), str_conv  , Toast.LENGTH_LONG).show();
		            	Log.v("**** ", str_conv);
		            }
		            if(flag == 2)
		            {
		            	String str_conv = String.valueOf(flag);
		            	Toast.makeText(getApplicationContext(), str_conv  , Toast.LENGTH_LONG).show();
		            	Log.v("**** ", str_conv);
		            }
*/		            
		            
		           
		            
		            // Getting name
		            String name = hmPlace.get("formatted_address");
		            
		            LatLng latLng = new LatLng(lat, lng);
		            
		            // Setting the position for the marker
		            markerOptions.position(latLng);
		            
		            // Setting the title for the marker
		            markerOptions.title(name);
		
		            // Placing a marker on the touched position
		            map.addMarker(markerOptions);    
		            
		            // Locate the first location
	                if(i==0)
	                	map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
	            }            
			}
		}
	
	    /***************************** Directions *************************************************/

	    private String getUrl(LatLng origin, LatLng dest) 
	    {

	        // Origin of route
	        String str_origin = "origin=" + origin.latitude + "," + origin.longitude;

	        // Destination of route
	        String str_dest = "destination=" + dest.latitude + "," + dest.longitude;


	        // Sensor enabled
	        String sensor = "sensor=false";

	        // Building the parameters to the web service
	        String parameters = str_origin + "&" + str_dest + "&" + sensor;

	        // Output format
	        String output = "json";

	        // Building the url to the web service
	        String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;


	        return url;
	    }

	    
	    private String destination_downloadUrl(String strUrl) throws IOException 
	    {
	        String data = "";
	        InputStream iStream = null;
	        HttpURLConnection urlConnection = null;
	        try {
	            URL url = new URL(strUrl);

	            // Creating an http connection to communicate with url
	            urlConnection = (HttpURLConnection) url.openConnection();

	            // Connecting to url
	            urlConnection.connect();

	            // Reading data from url
	            iStream = urlConnection.getInputStream();

	            BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

	            StringBuffer sb = new StringBuffer();

	            String line = "";
	            while ((line = br.readLine()) != null) {
	                sb.append(line);
	            }

	            data = sb.toString();
	            Log.d("downloadUrl", data.toString());
	            br.close();

	        } catch (Exception e) {
	            Log.d("Exception", e.toString());
	        } finally {
	            iStream.close();
	            urlConnection.disconnect();
	        }
	        return data;
	    }   
	    
	 // Fetches data from url passed
	    class FetchUrl extends AsyncTask<String, Void, String> 
	    {

	        @Override
	        protected String doInBackground(String... url) {

	            // For storing data from web service
	            String data = "";

	            try {
	                // Fetching the data from web service
	                data = destination_downloadUrl(url[0]);
	                Log.d("Background Task data", data.toString());
	            } catch (Exception e) {
	                Log.d("Background Task", e.toString());
	            }
	            return data;
	        }

	        @Override
	        protected void onPostExecute(String result)
	        {
	            super.onPostExecute(result);

	            direction_ParserTask direction_parserTask = new direction_ParserTask();

	            // Invokes the thread for parsing the JSON data
	            direction_parserTask.execute(result);

	        }
	    }
	
	    
	    private class direction_ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {

	        // Parsing the data in non-ui thread
	        @Override
	        protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {

	            JSONObject jObject;
	            List<List<HashMap<String, String>>> routes = null;

	            try {
	                jObject = new JSONObject(jsonData[0]);
	                Log.d("ParserTask",jsonData[0].toString());
	                DataParser parser = new DataParser();
	                Log.d("ParserTask", parser.toString());

	                // Starts parsing data
	                routes = parser.parse(jObject);
	                Log.d("ParserTask","Executing routes");
	                Log.d("ParserTask",routes.toString());

	            } catch (Exception e) {
	                Log.d("ParserTask",e.toString());
	                e.printStackTrace();
	            }
	            return routes;
	        }

	        // Executes in UI thread, after the parsing process
	        @Override
	        protected void onPostExecute(List<List<HashMap<String, String>>> result) 
	        {
	            ArrayList<LatLng> points;
	            PolylineOptions lineOptions = null;

	            // Traversing through all the routes
	            for (int i = 0; i < result.size(); i++) {
	                points = new ArrayList<>();
	                lineOptions = new PolylineOptions();

	                // Fetching i-th route
	                List<HashMap<String, String>> path = result.get(i);

	                // Fetching all the points in i-th route
	                for (int j = 0; j < path.size(); j++)
	                {
	                    HashMap<String, String> point = path.get(j);

	                    double lat = Double.parseDouble(point.get("lat"));
	                    double lng = Double.parseDouble(point.get("lng"));
	                    LatLng position = new LatLng(lat, lng);

	                    points.add(position);
	                }

	                // Adding all the points in the route to LineOptions
	                lineOptions.addAll(points);
	                lineOptions.width(10);
	                lineOptions.color(Color.RED);

	                Log.d("onPostExecute","onPostExecute lineoptions decoded");

	            }

	            // Drawing polyline in the Google Map for the i-th route
	            if(lineOptions != null) 
	            {
	                map.addPolyline(lineOptions);
	            }
	            else {
	                Log.d("onPostExecute","without Polylines drawn");
	            }
	        }
	    }
	
}



class DataParser {

    /** Receives a JSONObject and returns a list of lists containing latitude and longitude */
    public List<List<HashMap<String,String>>> parse(JSONObject jObject){

        List<List<HashMap<String, String>>> routes = new ArrayList<>() ;
        JSONArray jRoutes;
        JSONArray jLegs;
        JSONArray jSteps;

        try {

            jRoutes = jObject.getJSONArray("routes");

            /** Traversing all routes */
            for(int i=0;i<jRoutes.length();i++){
                jLegs = ( (JSONObject)jRoutes.get(i)).getJSONArray("legs");
                List path = new ArrayList<>();

                /** Traversing all legs */
                for(int j=0;j<jLegs.length();j++){
                    jSteps = ( (JSONObject)jLegs.get(j)).getJSONArray("steps");

                    /** Traversing all steps */
                    for(int k=0;k<jSteps.length();k++){
                        String polyline = "";
                        polyline = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("polyline")).get("points");
                        List<LatLng> list = decodePoly(polyline);

                        /** Traversing all points */
                        for(int l=0;l<list.size();l++){
                            HashMap<String, String> hm = new HashMap<>();
                            hm.put("lat", Double.toString((list.get(l)).latitude) );
                            hm.put("lng", Double.toString((list.get(l)).longitude) );
                            path.add(hm);
                        }
                    }
                    routes.add(path);
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }catch (Exception e){
        }


        return routes;
    }


  
    private List<LatLng> decodePoly(String encoded) 
    {

        List<LatLng> poly = new ArrayList<>();
        int index = 0, len = encoded.length();
        int lat = 0, lng = 0;

        while (index < len) {
            int b, shift = 0, result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;

            shift = 0;
            result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;

            LatLng p = new LatLng((((double) lat / 1E5)),
                    (((double) lng / 1E5)));
            poly.add(p);
        }

        return poly;
    }
    
    
	private class aaaGeocoderTask extends AsyncTask< ArrayList<String>, Void, String >
    {
 
		//http://www.androidtrainee.com/driving-distance-and-travel-time-duration-between-two-locations-in-google-map-android-api-v2/
		
		Context mContext;
		String str_departure = "";
	 	String str_destination = "";
		
        @Override
        protected String  doInBackground(ArrayList<String>... passing) 
        {
            	
            	List<Address> addresses_departure = null;
            	List<Address> addresses_destination = null;
            
            	//String msg_content = "";
            
            	ArrayList<String> tmp_array = passing[0]; 
            	String local_departure = tmp_array.get(0);
            	String local_destination = tmp_array.get(1);
            	
            	toto(local_departure);
            	toto(local_destination);
            	
            	// Origin of route
            	double lat1 = MainActivity.locations.get(0).latitude;
            	double lng1 = MainActivity.locations.get(0).longitude;
            	String str_origin = "origin=" +lat1 + "," + lng1;

    	        // Destination of route
    	        double lat2 = MainActivity.locations.get(1).latitude;
            	double lng2 = MainActivity.locations.get(1).longitude;
    	        String str_dest = "destination=" + lat2 + "," + lng2;


    	        // Sensor enabled
    	        String sensor = "sensor=false";

    	        // Building the parameters to the web service
    	        String parameters = str_origin + "&" + str_dest + "&" + sensor;

    	        // Output format
    	        String output = "json";

    	        // Building the url to the web service
    	        String strUrl = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;
            	
    	        String data = "";
    	        InputStream iStream = null;
    	        HttpURLConnection urlConnection = null;
    	        try {
    	            URL _url = new URL(strUrl);

    	            // Creating an http connection to communicate with url
    	            urlConnection = (HttpURLConnection) _url.openConnection();

    	            // Connecting to url
    	            urlConnection.connect();

    	            // Reading data from url
    	            iStream = urlConnection.getInputStream();

    	            BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

    	            StringBuffer sb = new StringBuffer();

    	            String line = "";
    	            while ((line = br.readLine()) != null) {
    	                sb.append(line);
    	            }

    	            data = sb.toString();
    	            
    	            MainActivity.global_data = sb.toString();
    	            
    	            Log.d("downloadUrl", data.toString());
    	            br.close();

    	        } catch (Exception e) {
    	            Log.d("Exception", e.toString());
    	        } finally {
    	            try {
						iStream.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
    	            urlConnection.disconnect();
    	        }
            	
    	        

				JSONObject jsonObject = null;
				try {
					jsonObject = new JSONObject(data);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				JSONArray jsonArray = null;
				try {
					jsonArray = (JSONArray) jsonObject.get("results");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            
				
				JSONObject jObject;
				List<List<HashMap<String, String>>> routes = null;
	 
				try
				{
					jObject = new JSONObject(data);
					DirectionsJSONParser_bis4 parser = new DirectionsJSONParser_bis4();
				 
					// Starts parsing data
					routes = parser.parse(jObject);
					
					List<HashMap<String, String>> path = routes.get(0);
					HashMap<String,String> point = path.get(0);
					String distance = (String)point.get("distance");		
					
					// path = routes.get(0);
					 point = path.get(1);
					String duration = (String)point.get("duration");	
					
					
					String cucu = "";
					
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				
				
				
				
            	
            	return data;
          
        }
        
        
        @Override
    	protected void onPreExecute() 
        {
				//ProgressDialog progressdialog = new ProgressDialog(mContext);

				//progressdialog = new ProgressDialog(MainActivity.this);
    			//progressdialog.setMessage("Directions processing...");
    			//progressdialog.setTitle("Please Wait...");
    			//progressdialog.show();
    	}
 
        @Override
        //protected void onPostExecute(List<Address> addresses)
        protected void onPostExecute(String data)
        {
        	    lulu(data);
        	    mark();
        	    
				//Toast.makeText(mContext , "Calculation ...", Toast.LENGTH_LONG).show();
        	    // arret
        	    /*
        		if (MainActivity.progressdialog.isShowing()) 
        		{
        			MainActivity.progressdialog.dismiss();
        		}*/
        }
	
        	private void toto(String place ) 
        	{
		
        			String url = "https://maps.googleapis.com/maps/api/geocode/json?";					
		
					try 
					{
						place = URLEncoder.encode(place, "utf-8");
					} 
					catch (UnsupportedEncodingException e)
					{
						e.printStackTrace();
					}
		
					String address = "address=" + place;
					String sensor = "sensor=false";
					String strurl = url + address + "&" + sensor;

		
					String data = "";
					InputStream iStream = null;
					HttpURLConnection urlConnection = null;
	        
					try
					{
						URL _url = new URL(strurl);

						// Creating an http connection to communicate with url 
						urlConnection = (HttpURLConnection) _url.openConnection();

						// 	Connecting to url 
						urlConnection.connect();

						// Reading data from url 
						iStream = urlConnection.getInputStream();

						BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

						StringBuffer sb  = new StringBuffer();

						String line = "";
						while( ( line = br.readLine())  != null)
						{
	                        sb.append(line);
						}

						data = sb.toString();

						br.close();
	                
						JSONObject jsonObject = new JSONObject(data);
						JSONArray jsonArray = (JSONArray) jsonObject.get("results");
                    
						if (jsonArray.length() > 0) 
						{
		                        jsonObject = (JSONObject) jsonArray.get(0);
		                        jsonObject = (JSONObject) jsonObject.get("geometry");
		                        JSONObject location = (JSONObject) jsonObject.get("location");
		                        Double lat = (Double) location.get("lat");
		                        Double lng = (Double) location.get("lng");
		                        System.out.println("lat - " + lat + " , lon - " + lng);
		                      
		                        MainActivity.locations.add(new LatLng(lat, lng));
						}
					}
	        
					catch(Exception e)
					{
						Log.d("Exception while downloading url", e.toString());
					}
	        
				    finally
				    {
				                try
				                {
										iStream.close();
								} 
				                catch (IOException e) 
				                {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
				                urlConnection.disconnect();
				    }
		
			}
	
			private void lulu(String data)
			{
    	 			JSONObject jObject;
         			List<List<HashMap<String, String>>> routes = null;

         			try 
         			{
         				jObject = new JSONObject(data);
         				Log.d("ParserTask",data.toString());
         				DataParser parser = new DataParser();
         				Log.d("ParserTask", parser.toString());
		
         				routes = parser.parse(jObject);
         				Log.d("ParserTask","Executing routes");
         				Log.d("ParserTask",routes.toString());

         			} 
         			catch (Exception e)
         			{
             			Log.d("ParserTask",e.toString());
             			e.printStackTrace();
         			}
         
         			ArrayList<LatLng> points;
         			PolylineOptions lineOptions = null;

			        for (int i = 0; i < routes.size(); i++)
			        {
			         	points = new ArrayList<>();
			         	lineOptions = new PolylineOptions();
			
			         	List<HashMap<String, String>> path = routes.get(i);
			
			         	for (int j = 0; j < path.size(); j++)
			         	{
			         		HashMap<String, String> point = path.get(j);
			         		double lat = Double.parseDouble(point.get("lat"));
			         		double lng = Double.parseDouble(point.get("lng"));
			         		LatLng position = new LatLng(lat, lng);
			         		points.add(position);
			         	}
			
			         	lineOptions.addAll(points);
			         	lineOptions.width(2); // 10
			         	lineOptions.color(Color.RED);
			
			         	Log.d("onPostExecute","onPostExecute lineoptions decoded");
			
			        }
        
			        if(lineOptions != null) 
			        {
			        	MainActivity.map.addPolyline(lineOptions);
			        }
			        else 
			        {
			        	Log.d("onPostExecute","without Polylines drawn");
			        }
	        	   
       
			}
	
			private void mark()
			{
				
					MarkerOptions options = new MarkerOptions();
				
					LatLng position =  MainActivity.locations.get(0);
					options.position(position);
					options.snippet("titi");
					
					Double a = position.latitude;
					Double b = position.longitude;
					String str1 = String.valueOf(a);
					String str2 = String.valueOf(b);
					String title = str_departure + " " +str1 + " " + str2; 
					options.title(title);
					options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
					MainActivity.map.addMarker(options);
					
					position =  MainActivity.locations.get(1);
					options.position(position);
					a = position.latitude;
					b = position.longitude;
					str1 = String.valueOf(a);
					str2 = String.valueOf(b);
					title = str_destination + " " +str1 + " " + str2; 
					options.title(title);
					options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
					MainActivity.map.addMarker(options);
					
			}
    
    }


}
    
    /********************    +++++   **************************/ 
    

	
	class DirectionsJSONParser_bis4 
	{
		public List<List<HashMap<String,String>>> parse(JSONObject jObject)
		{
			
			List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String,String>>>() ;
			JSONArray jRoutes = null;
			JSONArray jLegs = null;
			JSONArray jSteps = null;	
			JSONObject jDistance = null;
			JSONObject jDuration = null;
			
			try {			
				
				jRoutes = jObject.getJSONArray("routes");
				
				for(int i=0;i<jRoutes.length();i++)
				{			
					jLegs = ( (JSONObject)jRoutes.get(i)).getJSONArray("legs");				
					
					List<HashMap<String, String>> path = new ArrayList<HashMap<String, String>>();				
					
					/** Traversing all legs */
					for(int j=0;j<jLegs.length();j++)
					{
						
						/** Getting distance from the json data */
						jDistance = ((JSONObject) jLegs.get(j)).getJSONObject("distance");
						HashMap<String, String> hmDistance = new HashMap<String, String>();
						hmDistance.put("distance", jDistance.getString("text"));
						
						/** Getting duration from the json data */
						jDuration = ((JSONObject) jLegs.get(j)).getJSONObject("duration");
						HashMap<String, String> hmDuration = new HashMap<String, String>();
						hmDuration.put("duration", jDuration.getString("text"));
						
						/** Adding distance object to the path */
						path.add(hmDistance);
						
						/** Adding duration object to the path */
						path.add(hmDuration);					
						
						jSteps = ( (JSONObject)jLegs.get(j)).getJSONArray("steps");
						
						
					}
					routes.add(path);
				}
				
			} 
			catch (JSONException e) 
			{			
				e.printStackTrace();
			}
			catch (Exception e)
			{			
			}
			
			return routes;
		}
		
	}