package com.haxademic.core.system;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.swing.Timer;

import com.haxademic.core.app.P;

import processing.core.PApplet;

public class SystemUtil {
	
	public static String getJavaVersion() {
		return System.getProperty("java.version");
	}

	public static boolean isOSX() {
		return System.getProperty("os.name").contains("OS X");
	}
	
	public static void runGarbageCollection() {
		System.gc();
	}
	
	public static String getTimestamp( PApplet p ) {
		// use P.nf to pad date components to 2 digits for more consistent ordering across systems
		return  String.valueOf( P.year() ) + "-" + 
				P.nf( P.month(), 2 ) + "-" + 
				P.nf( P.day(), 2 ) + "-" + 
				P.nf( P.hour(), 2 ) + "-" + 
				P.nf( P.minute(), 2 ) + "-" + 
				P.nf( P.second(), 2 );
	}

	public static String getTimestampFine( PApplet p ) {
		return SystemUtil.getTimestamp(p) + "-" + P.nf( p.frameCount, 8 ); 
	}
	
	public static void setTimeout(ActionListener callback, int delay) {
		Timer deferredStateTimer = new Timer(delay, callback);
		deferredStateTimer.setRepeats(false);
		deferredStateTimer.start();
	}
	
	public static void printRunningProcesses() {
	    try {
	        String line;
	        Process p = Runtime.getRuntime().exec("ps -e");
	        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
	        while ((line = input.readLine()) != null) {
	            System.out.println("## "+line); //<-- Parse data here.
	        }
	        input.close();
	    } catch (Exception err) {
	        err.printStackTrace();
	    }
	}
	
	public static boolean hasProcessContainingString(String processName) {
		try {
			String line;
			Process p = Runtime.getRuntime().exec("ps -e");
			BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
			while ((line = input.readLine()) != null) {
				if(line.indexOf(processName) != -1) {
					return true;
				}
			}
			input.close();
		} catch (Exception err) {
			err.printStackTrace();
		}
		return false;
	}
	
	public static void killProcessContainingString(String processName) {
		try {
			String line;
			Process p = Runtime.getRuntime().exec("ps -e");
			BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
			while ((line = input.readLine()) != null) {
				if(line.indexOf(processName) != -1) {
					P.println("found & killing: "+line);
					String processline = line.trim().split(" ")[0];
					String killCmd = "kill -9 "+processline;
					P.println("killCmd: "+killCmd);
					Process d = Runtime.getRuntime().exec(killCmd);
				}
			}
			input.close();
		} catch (Exception err) {
			err.printStackTrace();
		}
	}
	

}

@@ -49,25 +49,15 @@
 awt.
-event.ActionEvent
+Desktop
 ;%0Aim
@@ -119,24 +119,52 @@
 eredReader;%0A
+import java.io.IOException;%0A
 import java.
@@ -184,16 +184,73 @@
 mReader;
+%0Aimport java.net.URI;%0Aimport java.net.URISyntaxException;
 %0A%0Aimport
@@ -2858,11 +2858,268 @@
 %09%7D%0A%09%7D%0A%09%0A
+%09public static void openWebPage(String url) %7B%0A%09%09if(Desktop.isDesktopSupported()) %7B%0A%09%09%09try %7B%0A%09%09%09%09Desktop.getDesktop().browse(new URI(url));%0A%09%09%09%7D catch (IOException e) %7B e.printStackTrace(); %7D %0A%09%09%09  catch (URISyntaxException e) %7B e.printStackTrace(); %7D%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Add launch URL method