package geisplugin.popup.actions;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

public class NESAS107 extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		Shell shell = HandlerUtil.getActiveShell(event);
		final String today = new SimpleDateFormat("d/M/yyyy").format(new Date());
		try {
//			String log =  Files.readAllLines(Paths.get("\\\\nesas008\\C$\\Program Files\\UltraVNC\\mslogon.log"), Charset.forName("UTF-8")).stream()
//				.filter(s -> s.startsWith(today) && s.contains("Connection received from ") && s.contains("using"))
//				.collect(Collectors.joining("\n"));
//			MessageDialog.openInformation(shell, "Log", log);
			
			StringBuilder log = new StringBuilder();
			Pattern pattern = Pattern.compile("^\\d+\\/\\d+\\/\\d+\\s((?:[0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9])\\s+(?:.*)\\s+((?:25[0-5]\\.|2[0-4][0-9]\\.|[01]?[0-9][0-9]?\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:\\s\\w+$|\\s\\w+\\\\n|\\s\\w+\\s(\\w+))");
				
			HashMap<String, String> map = new HashMap<String, String>();
			for (String string : Files.readAllLines(Paths.get("\\\\nesas107\\C$\\Program Files\\UltraVNC\\mslogon.log"), Charset.forName("UTF-8"))) {
				if (string.startsWith(today) && string.contains("Connection received") && string.contains("using") || string.startsWith(today) && string.contains("disconnected") && string.contains("Client ")) {
					Matcher matcher = pattern.matcher(string);
					while (matcher.find()) {
						if ((map.get(matcher.group(2)) == null || map.get(matcher.group(2)).trim().isEmpty()) && matcher.group(3) != null) {
							map.put(matcher.group(2), matcher.group(1)+";"+matcher.group(2)+";"+matcher.group(3));
							continue;
						}
						if (string.startsWith(today) && string.contains("disconnected") && string.contains("Client ") && map.get(matcher.group(2)) != null) {
							String[] t1 = map.get(matcher.group(2)).split(";");
							if (t1.length > 2) {
								log.append("Connection received from " + t1[0] + " till " + matcher.group(1) + " with user " + t1[2] + " and IP " + t1[1] + "\n");
								map.put(t1[1], "");
							}
						}
					}
				}
			}
			if (!map.isEmpty()) {
				for (Entry<String, String> entry : map.entrySet()) {
					if (entry.getValue().equals("")) {
						continue;
					}
					String[] t1 = entry.getValue().split(";");
					log.append("Currently online is user "+ t1[2] + " since " + t1[0] + " with IP "+ t1[1] + "\n"); 
				}
			}
			MessageDialog.openInformation(shell, "Log", log.toString());
		} catch (final IOException e) {
			MessageDialog.openError(shell, "Fehler", "Fehler " + e.getMessage());
		}	 
		return null;
	}
	
}
