package com.eactive.eai.transformer.servlet;

import com.eactive.eai.adapter.socket2.common.ExceptionStackTrace;
import com.eactive.eai.adapter.socket2.config.SocketAdapterManager;
import com.eactive.eai.adapter.socket2.config.SocketLoggerManager;
import com.eactive.eai.adapter.socket2.protocol.http.HttpServlet;
import com.eactive.eai.adapter.socket2.protocol.http.codec.HttpRequest;
import com.eactive.eai.adapter.socket2.protocol.http.codec.HttpResponse;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.transformer.function.FunctionManager;

public class FunctionServlet extends HttpServlet {
	final static SocketAdapterManager manager = SocketAdapterManager.getInstance();
    static Logger logger = SocketLoggerManager.getInstance().getLogger("FileLogger{NET}_HTTP");

	protected void doService(HttpRequest request, HttpResponse response) throws Exception {
		String key = request.getContext();
    	if (key.lastIndexOf('/') == -1) {
    		key = "";
    	} else {
    		key = key.substring(key.indexOf('/')+1);
    	}

    	String result = "";
		try {
			result = show(key);
		} catch(Throwable e) {
			result = "<pre>" + ExceptionStackTrace.dump(e) + "</pre>";
		}	
    	
		response.setContentType("text/html");
		response.setResponseCode(HttpResponse.HTTP_STATUS_SUCCESS);
		response.appendBody(result);
	}
	
	final static String SCRIPT =
		"	<script>                                        \n" +
        "		function toggle(obj){                       \n" +
        "			if (obj.style.height == \"17px\"){      \n" +
        "				obj.style.height=\"\";              \n" +
        "				obj.style.overflow=\"visible\";     \n" +
        "			}else{                                  \n" +
        "				obj.style.height=\"17px\";          \n" +
        "				obj.style.overflow=\"hidden\";      \n" +
        "			}                                       \n" +
        "		}                                           \n" +
        "	</script>                                       \n";	

	public String show(String key) {
		StringBuffer sb = new StringBuffer();
		FunctionManager manager = FunctionManager.getInstance();
		String[] functionNames = manager.getAllFunctionNames();
		
		if(functionNames == null) return "";
		if(key == null) key = "";
		
    	sb.append(SCRIPT);
    	sb.append("message | key = " + key + "<br>");
		for(int i=0; i<functionNames.length; i++) {
			if(functionNames[i] == null) continue;
			if ("".equals(key.trim()) || functionNames[i].indexOf(key) != -1) {
	    		sb.append("<div style=\"height: 17px; margin: 0px 0px 0px 0px;overflow: hidden;padding:0px 0px 0px 0px;cursor:hand;\" ondblclick=\"toggle(this)\">");
	        	sb.append("<pre style=\"padding:0px 0px 0px 0px;margin: 0px 0px 0px 0px\">");
				sb.append(manager.getFunctionDefinition(functionNames[i]));
	    	   	sb.append("</pre>");
	    	   	sb.append("</div>");
	    	   	sb.append("<div style=\"height: 1px; overflow: hidden;border:0px;background-color:black;\"></div>");
			}
		}
		return sb.toString();
	}
}
