package net.csgeek.soop;

import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import cascading.stats.FlowStats;
import net.csgeek.soop.web.SoopWebServer;

public class Driver {
    private static final Logger LOG = Logger.getLogger(Driver.class);
    private static Docket docket; // I'm sure this isn't the right way
					  // to do this

    public static void reloadDocket() {
	LOG.info("Reloading Docket");
	docket.clear();
	try {
	    docket.load();
	} catch (IOException e) {
	    throw new IllegalStateException("Could not reload docket", e);
	}
    }
    
    public static enum FlowState { 	
	STARTED ("<div'>$1<br><progress max=100/></div>"), 
	SUCCESSFUL ("<div style='background: lightblue;'>$1</div>"), 
	ERROR ("<div style='background: red;'>$1</div>"),
	UNDEFINED ("$1");
	
	private String replacement;
    	private FlowState(String str) {
    	    replacement = str;
    	}
    	public String getReplacement() {
    	    return replacement;
    	}
    	public static FlowState forStatus(FlowStats stats) {
		if(stats.isSuccessful()) {
		    return SUCCESSFUL;
		} else if(stats.isEngaged()) {
		    return STARTED;
		} else if(stats.isFailed()) {
		    return ERROR;			    
		} else return UNDEFINED;
    	}
    };
    
    public static ConcurrentHashMap<String, FlowState> statusMap = new ConcurrentHashMap<String, FlowState>();

    public static void main(String[] args) throws Exception {
	PropertyConfigurator.configure("log4j.properties"); //Need to do this to override log4j config picked up from a dependency
	LOG.info("Starting Soop ...");
	docket = new Docket();
	docket.load();
	Runtime.getRuntime().addShutdownHook(new Thread() {
	    @Override
	    public void run() {
		LOG.info("Soop shutting down");
		docket.clear();
	    }
	});
	SoopWebServer webserver = new SoopWebServer();
	webserver.launch();
	LOG.info("Soop started.");
	webserver.waitFor();
    }

}

@@ -1052,32 +1052,184 @@
 acement;%0A    %09%7D%0A
+    %09protected FlowState setProgress(int p) %7B%0A    %09    replacement = replacement.replace(%22progress%22, %22progress value=%22+p);%0A    %09    return this;%0A    %09%7D%0A
     %09public stat
@@ -1367,22 +1367,99 @@
     
-return STARTED
+//TODO: Calculate actual percentage from FlowStepStats%0A%09%09    return STARTED.setProgress(50)
 ;%0A%09%09

Add more progress stuffs