package com.namelessmc.api.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.namelessmc.api.NamelessConnectException;

public class RequestUtil {

	/**
	 * @param type
	 * @param url Full URL
	 * @param postString
	 * @param https
	 * @return
	 */
	public static Request createRequest(RequestType type, URL url, String postString, boolean https) {
		
		if (type == null) {
			throw new IllegalArgumentException("Request type must not be null");
		}
		
		if (url == null) {
			throw new IllegalArgumentException("URL must not be null");
		}
			
		if (postString == null) {
			throw new IllegalArgumentException("Post string must not be null");
		}
			
		if (type.equals(RequestType.POST)) {
			Exception exception;
			try {
				URLConnection connection;
				
				if (https) {
					HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection();
					
					httpsConnection.setRequestMethod("POST");
					httpsConnection.setRequestProperty("Content-Length", Integer.toString(postString.length()));
					httpsConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
					httpsConnection.setDoOutput(true);
					httpsConnection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
					
					connection = httpsConnection;
				} else {
					HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
					
					httpConnection.setRequestMethod("POST");
					httpConnection.setRequestProperty("Content-Length", Integer.toString(postString.length()));
					httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
					httpConnection.setDoOutput(true);
					httpConnection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
					
					connection = httpConnection;
				}
					

				// Initialise output stream
				DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());

				// Write request
				outputStream.writeBytes(postString);

				// Initialise input stream
				InputStream inputStream = connection.getInputStream();

				// Handle response
				BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
				StringBuilder responseBuilder = new StringBuilder();

				String responseString;
				while ((responseString = streamReader.readLine()) != null)
					responseBuilder.append(responseString);

				JsonParser parser = new JsonParser();

				JsonObject response = parser.parse(responseBuilder.toString()).getAsJsonObject();

				if (response.has("error")) {
					// Error with request
					String errorMessage = response.get("message").getAsString();
					exception = new NamelessConnectException(errorMessage);
				}

				// Close output/input stream
				outputStream.flush();
				outputStream.close();
				inputStream.close();

				// Disconnect
				if (https) {
					((HttpsURLConnection) connection).disconnect();
				} else {
					((HttpURLConnection) connection).disconnect();
				}

				exception = null;
			} catch (Exception e) {
				exception = e;
			}
			
			return new Request(exception);
		} else {
			throw new IllegalArgumentException("This request type is not yet supported.");
		}
	}
	
	public static String getPostString(String id) {
		String postString = null;
		try {
			if (id.length() >= 17) {
				postString = "uuid=" + URLEncoder.encode(id, "UTF-8");
			} else if (id.length() <= 16) {
				postString = "username=" + URLEncoder.encode(id, "UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return postString;
	}
	
	public static class Request {
		
		private Exception exception;
		
		public Request(Exception exception) {
			this.exception = exception;
		}
		
		public Exception getException() {
			return exception;
		}
		
		public boolean hasSucceeded() {
			return exception == null;
		}
		
	}
	
	public enum RequestType {
		POST;
	}

}
@@ -313,16 +313,39 @@
 Encoder;
+%0Aimport java.util.UUID;
 %0A%0Aimport
@@ -1098,16 +1098,40 @@
 eption;%0A
+%09%09%09JsonObject response;%0A
 %09%09%09try %7B
@@ -2878,27 +2878,16 @@
 );%0A%0A%09%09%09%09
-JsonObject 
 response
@@ -3483,16 +3483,37 @@
 on = e;%0A
+%09%09%09%09response = null;%0A
 %09%09%09%7D%0A%09%09%09
@@ -3544,16 +3544,26 @@
 xception
+, response
 );%0A%09%09%7D e
@@ -3656,24 +3656,26 @@
 ;%0A%09%09%7D%0A%09%7D%0A%09%0A%09
+/*
 public stati
@@ -4030,16 +4030,234 @@
 String;%0A
+%09%7D*/%0A%09%0A%09public static String getPostString(UUID uuid) %7B%0A%09%09try %7B%0A%09%09%09return %22uuid=%22 + URLEncoder.encode(uuid.toString(), %22UTF-8%22);%0A%09%09%7D catch (UnsupportedEncodingException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%09return null;%0A%09%09%7D%0A
 %09%7D%0A%09%0A%09pu
@@ -4318,16 +4318,47 @@
 eption;%0A
+%09%09private JsonObject response;%0A
 %09%09%0A%09%09pub
@@ -4388,16 +4388,37 @@
 xception
+, JsonObject response
 ) %7B%0A%09%09%09t
@@ -4585,16 +4585,79 @@
 %0A%09%09%7D%0A%09%09%0A
+%09%09public JsonObject getResponse() %7B%0A%09%09%09return response;%0A%09%09%7D%0A%09%09%0A
 %09%7D%0A%09%0A%09pu

Include response in Request