package heufybot.utils;

import heufybot.core.Logger;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import org.json.simple.*;
import org.json.simple.parser.JSONParser;

public class PasteUtils
{
	public static String post(final String data, final String title, String expire)
	{
		try
	    {
			URL url = new URL("http://paste.ee/api");
	        URLConnection connection = url.openConnection();
	        connection.setDoOutput(true);

	        String key = "public";
	        String format = "json";

	        String postData = URLEncoder.encode("key", "UTF8") + "=" + URLEncoder.encode(key, "UTF8") + "&" + 
	        		URLEncoder.encode("description", "UTF8") + "=" + URLEncoder.encode(title, "UTF8") + "&" + 
	        		URLEncoder.encode("paste", "UTF8") + "=" + URLEncoder.encode(data, "UTF8") + "&" + 
	        		URLEncoder.encode("expiration", "UTF8") + "=" + URLEncoder.encode(expire, "UTF8") + "&" + 
	        		URLEncoder.encode("format", "UTF8") + "=" + URLEncoder.encode(format, "UTF8") + "&";

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

	        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	        String decodedString;
	        String result = "";
	        while ((decodedString = in.readLine()) != null)
	        {
	        	result += decodedString;
	        }
	        in.close();
	        
	        JSONObject json = (JSONObject) new JSONParser().parse(result);
	        JSONObject paste = (JSONObject) json.get("paste");
	        return paste.get("raw").toString();
	    }
	    catch (Exception e)
	    {
	    	e.printStackTrace();
	    	Logger.error("Paste.ee", "Something went wrong while posting to Paste.ee");
	    	return null;
	    }
	}	
}
@@ -415,20 +415,17 @@
 %0A%09%09try%0A%09
-    
+%09
 %7B%0A%09%09%09URL
@@ -459,24 +459,25 @@
 e.ee/api%22);%0A
+%09
 %09        URL
@@ -518,24 +518,25 @@
 nnection();%0A
+%09
 %09        con
@@ -562,17 +562,19 @@
 (true);%0A
-%0A
+%09%0A%09
 %09       
@@ -597,16 +597,17 @@
 ublic%22;%0A
+%09
 %09       
@@ -631,17 +631,19 @@
 %22json%22;%0A
-%0A
+%09%0A%09
 %09       
@@ -734,32 +734,33 @@
 UTF8%22) + %22&%22 + %0A
+%09
 %09        %09%09URLEn
@@ -837,32 +837,33 @@
 UTF8%22) + %22&%22 + %0A
+%09
 %09        %09%09URLEn
@@ -933,32 +933,33 @@
 UTF8%22) + %22&%22 + %0A
+%09
 %09        %09%09URLEn
@@ -1044,16 +1044,17 @@
  %22&%22 + %0A
+%09
 %09       
@@ -1141,17 +1141,19 @@
  + %22&%22;%0A
-%0A
+%09%0A%09
 %09       
@@ -1228,24 +1228,25 @@
 tStream());%0A
+%09
 %09        out
@@ -1263,16 +1263,17 @@
 tData);%0A
+%09
 %09       
@@ -1286,17 +1286,19 @@
 lose();%0A
-%0A
+%09%0A%09
 %09       
@@ -1390,16 +1390,17 @@
 am()));%0A
+%09
 %09       
@@ -1414,32 +1414,33 @@
  decodedString;%0A
+%09
 %09        String 
@@ -1452,16 +1452,17 @@
 t = %22%22;%0A
+%09
 %09       
@@ -1510,16 +1510,17 @@
 = null)%0A
+%09
 %09       
@@ -1518,24 +1518,25 @@
 %09%09        %7B%0A
+%09
 %09        %09re
@@ -1558,16 +1558,17 @@
 String;%0A
+%09
 %09       
@@ -1566,24 +1566,25 @@
 %09%09        %7D%0A
+%09
 %09        in.
@@ -1588,24 +1588,25 @@
 in.close();%0A
+%09
 %09        %0A%09 
@@ -1603,16 +1603,17 @@
        %0A
+%09
 %09       
@@ -1676,16 +1676,17 @@
 esult);%0A
+%09
 %09       
@@ -1733,24 +1733,25 @@
 t(%22paste%22);%0A
+%09
 %09        ret
@@ -1788,19 +1788,13 @@
 );%0A%09
-    %7D%0A%09    
+%09%7D%0A%09%09
 catc
@@ -1814,20 +1814,17 @@
 e)%0A%09
-    
+%09
 %7B%0A
+%09
 %09    
-%09
 e.pr
@@ -1840,22 +1840,22 @@
 race();%0A
+%09
 %09    
-%09
 Logger.e
@@ -1926,14 +1926,14 @@
 %22);%0A
+%09
 %09    
-%09
 retu
@@ -1946,15 +1946,13 @@
 l;%0A%09
-    
+%09
 %7D%0A%09%7D%09%0A
-
 %7D
+%0A

Fix ALL the indents