package maps;

import java.io.IOException;
import java.net.*;
import java.util.*;

import databases.DatabaseLuoghi;

public class GoogleMaps extends GISFinder
{
	//private String citta;
	//private String categoria;
	private URL googlemaps;
	private URLConnection gm;
	private int maxdistance = 10;
	
	private Scanner sc;
	//private Map<String, List<Location>> map = new LinkedHashMap<String, List<Location>>();
	private DatabaseLuoghi map = DatabaseLuoghi.getInstance();
	
	public GoogleMaps()
	{
	}
	
	private void instance(int page, String citta, String categoria)
	{
		try
		{
			googlemaps = new URL("http://maps.google.it/maps?f=q&hl=it&geocode=&q=" + categoria + "+" + citta + "&start=" + page + "0");
			gm = googlemaps.openConnection();
			sc = new Scanner(gm.getInputStream());
		}
		catch (MalformedURLException e) 
		{
			e.printStackTrace();
		}
		catch (IOException e) 
		{
			e.printStackTrace();
		}		
	}
	
	public void calcolaDistanza(String saddr, Location l)
	{
		String result = null;
		String daddr = l.getAddress().replace(' ', '+');
		String url = new String("http://maps.google.com/maps?f=d&saddr=" + saddr + "&daddr=" +  daddr);
		//System.out.println(url);
		try 
		{
			URL u = new URL(url);
			URLConnection urlc = u.openConnection();
			Scanner sc = new Scanner(urlc.getInputStream());
			while(sc.hasNext())
			{
				String st = sc.next();
				if(st.contains("noprint") && st.contains("km"))
				{
					try
					{
						result = st.substring(st.indexOf("b\\x3e")+5, st.indexOf("x26")-1);
					}
					catch(StringIndexOutOfBoundsException sioobe)
					{
						result = st.substring(st.indexOf("b>")+2, st.indexOf("&"));
					}
					System.out.println(result);
					l.setDistance(Float.parseFloat(result));
					return;
				}
			}
			l.setDistance(-1);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	// trova il posto da cercare
	// per ognuno calcola distanza
	public void find(String via, String citta, String categoria)
	{
		List<Location> distanze = map.get(categoria);
		if(distanze==null)
		{
			distanze = new LinkedList<Location>();
			map.put(categoria, distanze);
		}
		for(int i=0; ; i++)
		{
			//citta = citta.replace(' ', '+');
			//String saddr = new String(via + "+" + citta);
			//saddr=saddr.replace(' ', '+');
			instance(i, citta, categoria);
			String input;
			StringTokenizer st;
			String id;
			Location l;
			while(sc.hasNext())
			{
				input = sc.nextLine();
				//System.out.println(input);
				if(input.contains("<title>403 Vietato</title>"))
				{
					System.out.println("403 Forbidden");
					return;
				}
				if(input.contains("laddr"))
				{
					st = new StringTokenizer(input, "\"");
					boolean found=false;
					while(st.hasMoreTokens())
					{
						id = st.nextToken();
						if(found)
						{
							l = new Location(id);
							calcolaDistanza(via, citta, l);	
							if(l.getDistance()>maxdistance)
							{
								System.out.println("Fine");
								return;
							}
							else if(l.getDistance()<0)
								System.out.println("errore su " +l.getName() + " " + l.getAddress());
							else
								distanze.add(l);
							found=false;
						}
						if(id.contains("laddr"))
							found=true;
					}
				}
			}
		}
	}
}