package com.ociweb.gl.example;

import com.ociweb.gl.api.Builder;
import com.ociweb.gl.api.GreenApp;
import com.ociweb.gl.api.GreenRuntime;
import com.ociweb.gl.api.TimeTrigger;
import com.ociweb.pronghorn.network.config.HTTPHeaderDefaults;

public class SimpleApp implements GreenApp {

	public int ADD_ID1;
	public int ADD_ID2;
	public int FILE_ID1;
	
	private final int port;
	private final boolean isLarge;
	private final boolean isTLS;
	private MathUnit singleInstance;	
	
	public SimpleApp(int port, boolean isLarge, boolean isTLS) {
		this.port = port;
		this.isLarge = isLarge;
		this.isTLS = isTLS;
	}
	
    public static void main( String[] args ) {
        GreenRuntime.run(new SimpleApp(8081,true,false));
    }
	
    
	@Override
	public void declareConfiguration(Builder builder) {
		
		builder.setTriggerRate(TimeTrigger.OnTheSecond);
	//	builder.limitThreads(8); TODO: this works however its spending too much time spinning on the threads.
		builder.parallelism(8);
				
		String bindHost = "127.0.0.1";
		builder.enableServer(isTLS, isLarge, bindHost, port);
		
		ADD_ID1 = builder.registerRoute("/groovyadd/^{a}/^{b}",HTTPHeaderDefaults.COOKIE.rootBytes());
		ADD_ID2 = builder.registerRoute("/add/^a/^b");//, HTTPHeaderKeyDefaults.CONTENT_TYPE, HTTPHeaderKeyDefaults.UPGRADE);
		
		FILE_ID1 = builder.registerRoute("/${unknown}");//TODO: if this is first it ignores the rest of the paths, TODO: should fix bug
		
		//int fieldId = builder.fieldId(ADD_ID2, "name".getBytes());
		
		
		//ADD_ID1 = builder.registerRoute("/groovyadd/%i/%i");
		//ADD_ID2 = builder.registerRoute("/add/%i/%i");//, HTTPHeaderKeyDefaults.CONTENT_TYPE, HTTPHeaderKeyDefaults.UPGRADE);
		
		//TODO: must add support for Unroutable choices
//		FILE_ID1 = builder.registerRoute("/favicon.ico"); //Just eat the reequest??
		
       //TODO: this breaks when we add headers....
	}

	
	
	@Override
	public void declareBehavior(GreenRuntime runtime) {		
		
		
		runtime.addRestListener(singleInstance = new MathUnit(runtime), ADD_ID1, ADD_ID2); //accept all registered routes
		
		
//		runtime.addTimeListener((now)->{Appendables.appendValue(System.out, "This is run every second on the second: ",now,"ms\n");
//		                               });
//		
//		runtime.addStartupListener(()->{System.out.println("this is run first");  });
	
		///TODO: this gets skipped as an older response?
		
//		runtime.addFileServer("/home/nate/git/GreenLightning/src/main/resources/site/index.html", FILE_ID1); 

		//runtime.addFileServer("/home/nate/git/GreenLightning/src/main/resources/site/index.html", FILE_ID1);    
  
		
		
		//runtime.addRestListener(new MathUnit(runtime, ADD_ID1, ADD_ID2)); //accept all registered routes
		//runtime.addRestListener(new MathUnit(runtime, ADD_ID2));//, ADD_ID2)); //accept all registered routes
		
		
		
	}

	@Override
	public void declareParallelBehavior(GreenRuntime runtime) {	
		
	//	runtime.addRestListener(new MathUnit(runtime, ADD_ID1));//, ADD_ID2)); //accept all registered routes

		runtime.addFileServer("/home/nate/git/GreenLightning/src/main/resources/site/index.html", FILE_ID1); 
//		runtime.addFileServer("/home/nate/git/GreenLightning/src/main/resources/site/index.html", FILE_ID1);  
		
		//TODO wrong matching IDS plus no server startup??
		
		//runtime.addRestListener(new MathUnit(runtime), ADD_ID1, ADD_ID2); //accept all registered routes
		
	}
	
	public String getLastCookie() {
		return singleInstance.getLastCookie();
	}
	
}

@@ -846,114 +846,8 @@
 d);%0A
-%09//%09builder.limitThreads(8); TODO: this works however its spending too much time spinning on the threads.%0A
 %09%09bu
@@ -1319,445 +1319,8 @@
 bug%0A
-%09%09%0A%09%09//int fieldId = builder.fieldId(ADD_ID2, %22name%22.getBytes());%0A%09%09%0A%09%09%0A%09%09//ADD_ID1 = builder.registerRoute(%22/groovyadd/%25i/%25i%22);%0A%09%09//ADD_ID2 = builder.registerRoute(%22/add/%25i/%25i%22);//, HTTPHeaderKeyDefaults.CONTENT_TYPE, HTTPHeaderKeyDefaults.UPGRADE);%0A%09%09%0A%09%09//TODO: must add support for Unroutable choices%0A//%09%09FILE_ID1 = builder.registerRoute(%22/favicon.ico%22); //Just eat the reequest??%0A%09%09%0A       //TODO: this breaks when we add headers....
 %0A%09%7D%0A
@@ -1388,25 +1388,24 @@
 time) %7B%09%09%0A%09%09
-%0A
 %09%09%0A%09%09runtime
@@ -1519,1321 +1519,84 @@
 %09%09%0A%09
-%09%0A//%09%09runtime.addTimeListener((now)-%3E%7BAppendables.appendValue(System.out, %22This is run every second on the second: %22,now,%22ms%5Cn%22);%0A//%09%09                               %7D);%0A//%09%09%0A//%09%09runtime.addStartupListener(()-%3E%7BSystem.out.println(%22this is run first%22);  %7D);%0A%09%0A%09%09///TODO: this gets skipped as an older response?%0A%09%09%0A//%09%09runtime.addFileServer(%22/home/nate/git/GreenLightning/src/main/resources/site/index.html%22, FILE_ID1); %0A%0A%09%09//runtime.addFileServer(%22/home/nate/git/GreenLightning/src/main/resources/site/index.html%22, FILE_ID1);    %0A  %0A%09%09%0A%09%09%0A%09%09//runtime.addRestListener(new MathUnit(runtime, ADD_ID1, ADD_ID2)); //accept all registered routes%0A%09%09//runtime.addRestListener(new MathUnit(runtime, ADD_ID2));//, ADD_ID2)); //accept all registered routes%0A%09%09%0A%09%09%0A%09%09%0A%09%7D%0A%0A%09@Override%0A%09public void declareParallelBehavior(GreenRuntime runtime) %7B%09%0A%09%09%0A%09//%09runtime.addRestListener(new MathUnit(runtime, ADD_ID1));//, ADD_ID2)); //accept all registered routes%0A%0A%09%09runtime.addFileServer(%22/home/nate/git/GreenLightning/src/main/resources/site/index.html%22, FILE_ID1); %0A//%09%09runtime.addFileServer(%22/home/nate/git/GreenLightning/src/main/resources/site/index.html%22, FILE_ID1);  %0A%09%09%0A%09%09//TODO wrong matching IDS plus no server startup??%0A%09%09%0A%09%09//runtime.addRestListener(new MathUnit(runtime), ADD_ID1, ADD_ID2); //accept all registered routes%0A%09%09
+%7D%0A%0A%09@Override%0A%09public void declareParallelBehavior(GreenRuntime runtime) %7B%09%0A
 %0A%09%7D%0A

remove file server this is just testing rest