package heufybot.utils;

import heufybot.core.Logger;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class URLUtils
{
	public static String grab(String urlString)
	{
		try
		{
			URL url = new URL(urlString);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestProperty("User-Agent", "Mozilla/5.0");
			BufferedReader bufReader;
			String line;
			String data = "";
		    bufReader = new BufferedReader( new InputStreamReader(connection.getInputStream()));
		    
		    while( (line = bufReader.readLine()) != null)
		    {
		    	data += line + " ";
		    }
		    bufReader.close();
		    return data;
		}
		catch(Exception e)
		{
			Logger.error("URL Utilities", "Couldn't grab URL \"" + urlString + "\"");
			return null;
		}
	}
	
	public static String getFullHostname(String urlString)
	{
		try
		{
			HttpURLConnection connection = (HttpURLConnection) new URL(urlString).openConnection();
			connection.setInstanceFollowRedirects(false);
			while (connection.getResponseCode() / 100 == 3)
			{
				urlString = connection.getHeaderField("location");
			    connection = (HttpURLConnection) new URL(urlString).openConnection();
			}
			return urlString;
		}
		catch (Exception e)
		{
			Logger.error("URL Utilities", "Couldn't get full hostname for \"" + urlString + "\"");
			return null;
		}
	}
	
	public static String getHost(String urlString)
	{
		URL url;
		try
		{
			url = new URL(urlString);
			return url.getHost();
		}
		catch (MalformedURLException e)
		{
			Logger.error("URL Utilities", "Couldn't get the host for \"" + urlString + "\"");
			return null;
		}
	}
	
	public static String shortenURL(String urlstring)
	{
		try
		{
			String json = "";
			if(urlstring.startsWith("http:"))
			{
				json = "{\"longUrl\": \"" + urlstring + "\"}";
			}
			else
			{
				json = "{\"longUrl\": \"http://"+urlstring+"/\"}";
			}
			
			URL url = new URL("https://www.googleapis.com/urlshortener/v1/url");
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setRequestMethod("POST"); 
	        connection.setRequestProperty("Content-Type", "application/json"); 
	        connection.setDoOutput(true);

	        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
	        out.write(json);
	        out.close();

	        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	        String decodedString;
	        String result = "";
	        while ((decodedString = in.readLine()) != null)
	        {
	        	result += decodedString;
	        }
	        in.close();
	        return result.substring(result.indexOf("http://goo.gl"), result.indexOf("\"", result.indexOf("http://goo.gl")));
		}
		catch(Exception e)
		{
			Logger.error("URL Utilities", "Couldn't shorten URL \"" + urlstring + "\"");
			return null;
		}
	}
	
	public static HashMap<String, String> grabRSSFeed(String url)
	{
		try
		{
			HashMap<String, String> elements = new HashMap<String, String>();
			
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(url);
			doc.getDocumentElement().normalize();
	
			XPathFactory factory = XPathFactory.newInstance();
			XPath xpath = factory.newXPath();
			XPathExpression expr = xpath.compile("//rss/channel/item/title/text()");
			
			Object result = expr.evaluate(doc, XPathConstants.NODESET);
			NodeList nodes = (NodeList) result;
			
			String[] titles = new String[nodes.getLength()];
			
			for (int i = 0; i < nodes.getLength(); i++) 
			{
			    titles[i] = nodes.item(i).getNodeValue();
			}
			
			expr = xpath.compile("//rss/channel/item/link/text()");
			result = expr.evaluate(doc, XPathConstants.NODESET);
			nodes = (NodeList) result;

			for (int i = 0; i < nodes.getLength(); i++) 
			{
				elements.put(titles[i], nodes.item(i).getNodeValue());
			}
			return elements;
		}
		catch(Exception e)
		{
			Logger.error("URL Utilities", "Couldn't grab RSS feed at " + url);
			return null;
		}
	}
}
@@ -625,32 +625,65 @@
 String urlString
+, HashMap%3CString, String%3E headers
 )%0A%09%7B%0A%09%09try%0A%09%09%7B%0A%09
@@ -782,32 +782,36 @@
 enConnection();%0A
+%09%09%09%0A
 %09%09%09connection.se
@@ -864,76 +864,165 @@
 %0A%09%09%09
-BufferedR
+for(String requestH
 eader 
-bufR
+: h
 eader
-;%0A%09%09%09String line;%0A%09%09%09String data = %22%22;%0A%09%09   
+s.keySet())%0A%09%09%09%7B%0A%09%09%09%09connection.setRequestProperty(requestHeader, headers.get(requestHeader));%0A%09%09%09%7D%0A%09%09%09%09%09%09%0A%09%09%09BufferedReader
  buf
@@ -1105,20 +1105,54 @@
 ()));%0A%09%09
-    
+%09String line;%0A%09%09%09String data = %22%22;%0A%09%09%09
 %0A%09%09    w
@@ -1406,32 +1406,142 @@
  null;%0A%09%09%7D%0A%09%7D%0A%09%0A
+%09public static String grab(String urlString)%0A%09%7B%0A%09%09return grab(urlString, new HashMap%3CString, String%3E());%0A%09%7D%0A%09%0A
 %09public static S

Add the option to use HTTP headers when grabbing a URL