package de.quaddy_services.proxy;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.SystemTray;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

import javax.swing.WindowConstants;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 *
 */
public class EscapeProxy {

	private static Logger LOGGER;

	public static void main(String[] args) {
		initializeLogger();

		LOGGER.info("Start");
		Properties tempProperties = readConfig();

		EscapeProxyConfig tempEscapeProxyConfig = new EscapeProxyConfig(tempProperties);

		final EscapeProxyFrame tempEscapeProxyFrame = new EscapeProxyFrame(tempEscapeProxyConfig);

		tempEscapeProxyFrame.setSize(new Dimension(500, 400));
		tempEscapeProxyFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

		Thread.setDefaultUncaughtExceptionHandler(new LoggingUncaughtExceptionHandler());
		// See java.awt.EventDispatchThread.handlerPropName
		System.setProperty("sun.awt.exception.handler", LoggingUncaughtExceptionHandler.class.getName());

		addExitListener(tempEscapeProxyFrame, tempProperties);
		initSystemTray(tempEscapeProxyFrame);

		setVisible(tempEscapeProxyFrame);

		new EscapeProxyWorkerAccept(tempEscapeProxyConfig).start();
	}
	/**
	 * Before loading logback.xml, set the properties
	 */
	private static void initializeLogger() {
		String tempDefaultLevel = System.getProperty("defaultLogLevel");
		if (tempDefaultLevel ==null) {
			System.setProperty("defaultLogLevel","info");
		}
		LOGGER = LoggerFactory.getLogger(EscapeProxy.class);
	}



	/**
	 * @return
	 *
	 */
	private static Properties readConfig() {
		Properties tempProperties = new Properties();
		File tempFile = getFile();
		if (tempFile.exists()) {
			try {
				FileInputStream tempIn = new FileInputStream(tempFile);
				tempProperties.loadFromXML(tempIn);
				tempIn.close();
				LOGGER.info("Loaded config "+tempFile.getAbsolutePath());
				LOGGER.debug("Content={}",tempProperties);
			} catch (IOException e) {
				LOGGER.error("error", e);
			}
		}
		return tempProperties;
	}

	/**
	 *
	 */
	private static File getFile() {
		File tempFile = new File(System.getProperty("user.home") + "/" + "escape-from-intranet.xml");
		return tempFile;
	}

	/**
	 *
	 */
	private static void initSystemTray(EscapeProxyFrame aEscapeProxyFrame) {
		if (SystemTray.isSupported()) {
			// TODO
		}
	}

	/**
	 *
	 */
	private static void setVisible(final EscapeProxyFrame tempEscapeProxyFrame) {
		EventQueue.invokeLater(new Runnable() {
			/**
			 *
			 */
			@Override
			public void run() {
				tempEscapeProxyFrame.setVisible(true);
			}
		});
	}

	/**
	 * @param aProperties
	 *
	 */
	private static void addExitListener(final EscapeProxyFrame tempEscapeProxyFrame, Properties aProperties) {
		tempEscapeProxyFrame.addWindowListener(new WindowAdapter() {
			/**
			 *
			 */
			@Override
			public void windowClosing(WindowEvent aE) {
				saveConfig(aProperties);
				super.windowClosing(aE);
				LOGGER.info("Exit");
				System.exit(0);
			}
		});
	}

	/**
	 *
	 */
	protected static void saveConfig(Properties aProperties) {
		try {
			File tempFile = getFile();
			LOGGER.info("Save config "+tempFile.getAbsolutePath());
			OutputStream tempOut = new FileOutputStream(tempFile);
			aProperties.storeToXML(tempOut, "");
			tempOut.close();
		} catch (IOException e) {
			LOGGER.error("error", e);
		}
	}
}

@@ -494,18 +494,16 @@
 ory;%0D%0A%0D%0A
-%0D%0A
 /**%0D%0A *%0D
@@ -1491,16 +1491,18 @@
 );%0D%0A%09%7D%0D%0A
+%0D%0A
 %09/**%0D%0A%09 
@@ -1693,16 +1693,17 @@
 Level ==
+ 
 null) %7B%0D
@@ -1743,16 +1743,17 @@
 gLevel%22,
+ 
 %22info%22);
@@ -1821,20 +1821,16 @@
 %0D%0A%09%7D%0D%0A%0D%0A
-%0D%0A%0D%0A
 %09/**%0D%0A%09 
@@ -2158,33 +2158,35 @@
 %22Loaded config %22
-+
+ + 
 tempFile.getAbso
@@ -2229,16 +2229,17 @@
 ent=%7B%7D%22,
+ 
 tempProp
@@ -3343,32 +3343,189 @@
 .exit(0);%0D%0A%09%09%09%7D%0D
+%0A%0D%0A%09%09%09/**%0D%0A%09%09%09 *%0D%0A%09%09%09 */%0D%0A%09%09%09@Override%0D%0A%09%09%09public void windowLostFocus(WindowEvent aE) %7B%0D%0A%09%09%09%09saveConfig(aProperties);%0D%0A%09%09%09%09super.windowLostFocus(aE);%0D%0A%09%09%09%7D%0D
 %0A%09%09%7D);%0D%0A%09%7D%0D%0A%0D%0A%09/
@@ -3669,17 +3669,19 @@
 config %22
-+
+ + 
 tempFile

save config before e.g. system restart