package edu.kit.mindstormer.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
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 = 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;
			HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
			connection.getResponseCode();
		} catch (UnsupportedEncodingException e) {
			OperatingSystem.displayText("URL error: " + e.getMessage());
		}
	}
}

@@ -1548,22 +1548,8 @@
 y = 
-String.format(
 %22mes
@@ -1557,12 +1557,11 @@
 age=
-%25s%22,
+%22 +
  URL
@@ -1594,17 +1594,16 @@
 CHARSET)
-)
 ;%0A%09%09%09Str

Remove inperformant call to String.format