package vs;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

enum Type {
	REQ1, REQ2, FILE, UNKNOWN;
}

public class WebServer {
	public void start() {
		try (ServerSocket serverSocket = new ServerSocket(8000)) {
			System.out.println("WebServer gestartet ...");
			while (true) {
				Socket socket = serverSocket.accept();
				new WebThread(socket).start();
			}
		} catch (IOException e) {
			System.err.println(e);
		}
	}

	private class WebThread extends Thread {
		private Socket socket;

		public WebThread(Socket socket) {
			this.socket = socket;
		}

		@Override
		public void run() {
			SocketAddress socketAddress = this.socket.getRemoteSocketAddress();
			System.err.println("Verbindung zu " + socketAddress + " aufgebaut");
			try (BufferedReader in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
					PrintWriter out = new PrintWriter(this.socket.getOutputStream(), true)) {
				String input;
				Type type = Type.UNKNOWN;
				String fileName = "";
				input = in.readLine();
				if (input.matches("^GET /cgi/choice\\?selection=Latein HTTP/\\d\\.\\d$")) {
					type = Type.REQ1;
				} else if (input.matches("^GET /cgi/choice\\?selection=HS-Mannheim HTTP/\\d\\.\\d$")) {
					type = Type.REQ2;
				} else if (input.matches("^GET (.+) HTTP/\\d\\.\\d$")) {
					type = Type.FILE;
					fileName = input.substring(4); // "GET " abschneiden
					fileName = fileName.substring(0, fileName.length() - 9); // " HTTPx.x" abschneiden
				} else {
					type = Type.UNKNOWN;
				}
				while (!"".equals(input = in.readLine())) {
					System.out.println(input);
				}
				File file = null;
				if (type == Type.FILE) {
					file = new File("target/classes/docs" + fileName);
					if (!file.canRead()) {
						type = Type.UNKNOWN;
					}
				}
				switch (type) {
				case REQ1:
					out.printf("HTTP/1.1 200 OK\r\n");
					out.printf("Content-Type: text/html; charset=utf-8\r\n");
					out.printf("\r\n");
					out.printf("<html><body>Non scholae sed vitae discimus.</body></html>\r\n");
					break;
				case REQ2:
					out.printf("HTTP/1.1 200 OK\r\n");
					out.printf("Content-Type: text/html; charset=utf-8\r\n");
					out.printf("\r\n");
					out.printf(
							"<html><body>Die Hochschule Mannheim ist bekannt für eine praxisnahe und theoretisch fundierte Ausbildung</body></html>\r\n");
					break;
				case FILE:
					out.printf("HTTP/1.1 200 OK\r\n");
					if ("html".equalsIgnoreCase(fileName.substring(fileName.length() - 4))) {
						out.printf("Content-Type: text/html; charset=utf-8\r\n");
					} else {
						out.printf("Content-Type: application/octet-stream\r\n");
					}
					out.printf("\r\n");
					FileInputStream fileIn = new FileInputStream(file);
					OutputStream outBin = this.socket.getOutputStream();
					int c;
					while ((c = fileIn.read()) != -1) {
						outBin.write(c);
					}
					outBin.close();
					break;
				case UNKNOWN:
					out.printf("HTTP/1.1 404 Not Found\r\n");
					out.printf("Content-Type: text/plain; charset=utf-8\r\n");
					out.printf("\r\n");
					out.printf("nicht gefunden\r\n");
					break;
				}
			} catch (Exception e) {
				System.err.println(e);
			} finally {
				try {
					this.socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				System.err.println("Verbindung zu " + socketAddress + " abgebaut");
			}
		}

	}

	public static void main(String[] args) {
		new WebServer().start();
	}
}
