package edu.kit.mindstormer.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import edu.kit.mindstormer.program.OperatingSystem;

public final class HttpLogger {

	private static String CHARSET = "UTF-8";
	private static String BASE_URL = "http://192.168.0.213:5000/log";

	private static final HttpLogger instance = new HttpLogger();
	private static boolean enabled = true;

	private HttpLogger() {
		// ctor
	}

	public static HttpLogger getInstance() {
		return instance;
	}

	public static void disable() {
		enabled = false;
	}

	public void log(String message) {
		if (!enabled)
			return;

		try {
			String logEntry = format(message);
			postMessage(logEntry);
		} catch (Exception e) {
			enabled = false;
			OperatingSystem.displayText("Log error: " + e.getMessage());
		}
	}

	private String format(String message) {
		StackTraceElement[] stackTrace = new Exception().getStackTrace();
		String name = stackTrace[stackTrace.length - 1].getClassName();
		name = pad(name.substring(name.lastIndexOf('.') + 1, name.length()), 15, ' ');
		return name + " | " + message;
	}

	private String pad(String s, int width, char fill) {
		int validLength = Math.min(s.length(), width);
		String padded = s.substring(0, validLength);
		padded += new String(new char[width - validLength]).replace('\0', fill);
		return padded;
	}

	private void postMessage(String parameter) throws IOException {
		try {
			String query = String.format("message=%s", URLEncoder.encode(parameter, CHARSET));
			String url = BASE_URL + "?" + query;
			new URL(url).openConnection();
		} catch (UnsupportedEncodingException | MalformedURLException e) {
			OperatingSystem.displayText("URL error: " + e.getMessage());
		}
	}
}

@@ -117,37 +117,19 @@
 ava.net.
-MalformedURLException
+URL
 ;%0Aimport
@@ -133,32 +133,42 @@
 ort java.net.URL
+Connection
 ;%0Aimport java.ne
@@ -186,61 +186,8 @@
 r;%0A%0A
-import edu.kit.mindstormer.program.OperatingSystem;%0A%0A
 publ
@@ -682,23 +682,15 @@
 y = 
-format(
 message
-)
 ;%0A%09%09
@@ -766,35 +766,26 @@
 %0A%09%09%09
-OperatingSystem.displayText
+System.out.println
 (%22Lo
@@ -818,24 +818,26 @@
 ));%0A%09%09%7D%0A%09%7D%0A%0A
+//
 %09private Str
@@ -865,16 +865,18 @@
 sage) %7B%0A
+//
 %09%09StackT
@@ -935,16 +935,18 @@
 race();%0A
+//
 %09%09String
@@ -1003,16 +1003,18 @@
 Name();%0A
+//
 %09%09name =
@@ -1086,16 +1086,18 @@
 , ' ');%0A
+//
 %09%09return
@@ -1121,20 +1121,26 @@
 essage;%0A
+//
 %09%7D%0A
-%0A
+//%0A//
 %09private
@@ -1185,16 +1185,18 @@
 fill) %7B%0A
+//
 %09%09int va
@@ -1236,16 +1236,18 @@
 width);%0A
+//
 %09%09String
@@ -1285,16 +1285,18 @@
 ength);%0A
+//
 %09%09padded
@@ -1362,16 +1362,18 @@
  fill);%0A
+//
 %09%09return
@@ -1381,16 +1381,18 @@
 padded;%0A
+//
 %09%7D%0A%0A%09pri
@@ -1589,16 +1589,43 @@
 ery;%0A%09%09%09
+URLConnection connection = 
 new URL(
@@ -1647,16 +1647,44 @@
 tion();%0A
+%09%09%09connection.getContent();%0A
 %09%09%7D catc
@@ -1719,67 +1719,34 @@
 ion 
-%7C MalformedURLException e) %7B%0A%09%09%09OperatingSystem.displayText
+e) %7B%0A%09%09%09System.out.println
 (%22UR

Fix http logger Add call to getContent on url connection