package com.jessieamorris.httpserver;

import com.jessieamorris.httpserver.exceptions.InvalidRequestException;
import com.jessieamorris.httpserver.exceptions.NotImplementedException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by jessie on 15-02-05.
 */
public class HttpRequest {
	public enum Method {
		OPTIONS("OPTIONS"),
		GET("GET"),
		HEAD("HEAD"),
		POST("POST"),
		PUT("PUT"),
		DELETE("DELETE"),
		TRACE("TRACE"),
		CONNECT("CONNECT");

		private String method;

		Method(String method) {
			this.method = method;
		}

		public String getMethod() {
			return method;
		}
	}

	private Method method;
	private URI uri;
	private String version;
	private List<Header> headers = new ArrayList<Header>();

	public HttpRequest() {}

	public void setMethod(Method method) {
		this.method = method;
	}

	public Method getMethod() {
		return method;
	}

	public void setURI(URI uri) {
		this.uri = uri;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getVersion() {
		return version;
	}

	public void validateRequestLine() throws InvalidRequestException {
		if(!version.equalsIgnoreCase("HTTP/1.1")) {
			throw new InvalidRequestException();
		}
	}

	public void addHeader(Header header) {
		headers.add(header);
	}

	public List<Header> getHeaders() {
		return headers;
	}

	public void parseRequest(BufferedReader in) throws IOException, NotImplementedException, InvalidRequestException {
		System.out.println("Got some input");

		String requestLine = in.readLine();
		System.out.println("Input line was: " + requestLine);

		String[] splitInput = requestLine.split("\\s+");

		System.out.println("Split input was: " + Arrays.toString(splitInput));

		if (splitInput.length != 3) {
			throw new InvalidRequestException();
		}

		if (splitInput[0].equals("GET")) {
			setMethod(HttpRequest.Method.GET);
			setURI(URI.create(splitInput[1]));
			setVersion(splitInput[2]);
		}

		validateRequestLine();

		System.out.println("Method: " + getMethod());

		switch(getMethod()) {
			case GET:
				parseHeaders(in);
				break;

			default:
				throw new NotImplementedException();
		}

		System.out.println("Headers done");
	}

	private void parseHeaders(BufferedReader in) throws IOException {
		String headerLine;

		System.out.println("Parsing headers!");

		while((headerLine = in.readLine()) != null && (!headerLine.equals("\r\n") && !headerLine.equals(""))) {
			System.out.println("Got a header: " + headerLine);

			String[] headerSplit = headerLine.split("\\s+");

			if(headerSplit.length == 2) {
				String headerName = headerSplit[0];
				String headerValue = headerSplit[1];

				if(headerName.charAt(headerName.length() - 1) == ':') {
					headerName = headerName.substring(0, headerName.length() - 1);
				}

				addHeader(new Header(headerName, headerValue));
			}
		}
	}
}

@@ -306,65 +306,9 @@
 til.
-ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.List
+*
 ;%0A%0A/
@@ -754,21 +754,28 @@
 private 
-List%3C
+Map%3CString, 
 Header%3E 
@@ -792,18 +792,24 @@
 new 
-ArrayList%3C
+HashMap%3CString, 
 Head
@@ -954,37 +954,38 @@
 n method;%0A%09%7D%0A%0A%09p
-ublic
+rivate
  void setURI(URI
@@ -1010,24 +1010,65 @@
  = uri;%0A%09%7D%0A%0A
+%09public URI getURI() %7B%0A%09%09return uri;%0A%09%7D%0A%0A
 %09public void
@@ -1395,38 +1395,146 @@
 ers.
-add(header);%0A%09%7D%0A%0A%09public List%3C
+put(header.getName(), header);%0A%09%7D%0A%0A%09public Collection%3CHeader%3E getHeadersCollection() %7B%0A%09%09return headers.values();%0A%09%7D%0A%0A%09public Map%3CString, 
 Head
@@ -1547,16 +1547,19 @@
 tHeaders
+Map
 () %7B%0A%09%09r
@@ -1825,24 +1825,68 @@
 uestLine);%0A%0A
+%09%09if(requestLine == null) %7B%0A%09%09%09return;%0A%09%09%7D%0A%0A
 %09%09String%5B%5D s
@@ -2808,24 +2808,27 @@
 split(%22%5C%5Cs+%22
+, 2
 );%0A%0A%09%09%09if(he

Fix some issues in header splitting.