package com.eactive.eai.adapter.http.servlet;

import com.eactive.eai.adapter.AdapterGroupVO;
import com.eactive.eai.adapter.AdapterManager;
import com.eactive.eai.adapter.http.HttpMemoryLogger;
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 java.util.HashMap;
import java.util.List;

public class HttpAdapterLogServlet 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 command = request.getContext();
    	if (command.lastIndexOf('/') == -1) {
    		command = "";
    	} else {
    		command = command.substring(command.indexOf('/')+1);
    	}

    	String result = "";
    	
    	try {
	    	if (command == null || "".equals(command) || "show".equals(command)) {
	    		result = show(request.getParameter("adapter"));
	    	} else if (command.startsWith("errorLog")) {
	    		result = errorLog(request.getParameter("adapter"));
	    	} else if (command.startsWith("txLog")) {
	    		result = txLog(request.getParameter("adapter"));
	    	} else {
	    		response.setResponseCode(404);
	    		return;
	    	}
		} catch(Throwable e) {
			result = "<pre>" + ExceptionStackTrace.dump(e) + "</pre>";
		}  	
		
		response.setContentType("text/html;charset=utf-8;");
		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();
		AdapterManager manager = AdapterManager.getInstance();
		//List<String> list = manager.getAllAdapterGroupNames();
		
		HashMap<String,AdapterGroupVO> hm = manager.getAllAdapterGroupVO();
		List<String> names = manager.getAllAdapterGroupNames();

    	sb.append(SCRIPT);
    	sb.append("http adapter log | keyword = " + key);
    	sb.append("<br>");

    	
    	for(String name : names){
    		AdapterGroupVO vo = hm.get(name);
    		if ("HTT".equals(vo.getType())){
    			if(name.indexOf(key) >=0){
    				sb.append("<pre style=\"padding:0px 0px 0px 0px;margin: 0px 0px 0px 0px\">");
    				sb.append(String.format("ⓖ %-50s ", name)+"\n");
    				for(String adapter :  vo.getAdapterNames()){
    					sb.append(String.format("    ⓐ %-50s ", adapter));
    					sb.append("txLog : <a href='/http/txLog?adapter="+name+adapter+"'>"+HttpMemoryLogger.getTransactionCount(name+adapter)+"</a>&nbsp;");
    					sb.append("errorLog : <a href='/http/errorLog?adapter="+name+adapter+"'>"+HttpMemoryLogger.getErrorCount(name+adapter)+"</a>&nbsp;");
    					sb.append("<br/>"+"\n");
    				}
    				sb.append("</pre>"+"\n");
				    	
    			}
    		}
    		
    	}

		return sb.toString();
	}
	public String txLog(String name) {
		StringBuffer sb = new StringBuffer();

    	sb.append(SCRIPT);
    	sb.append("adapter ="+name+"<br>");
    	String[] logs = HttpMemoryLogger.getTransactions(name);
    	for (int i=logs.length-1; i>=0; i--) {
    		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(logs[i]);
    	   	sb.append("</pre>");
    	   	sb.append("</div>");
    	   	sb.append("<div style=\"height: 1px; overflow: hidden;border:0px;background-color:black;\"></div>");
    		
    	}
		return sb.toString();
	}	
	public String errorLog(String name) {
		StringBuffer sb = new StringBuffer();

    	sb.append(SCRIPT);
    	sb.append("adapter ="+name+"<br>");
    	String[] logs = HttpMemoryLogger.getErrors(name);
    	for (int i=logs.length-1; i>=0; i--) {
    		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(logs[i]);
    	   	sb.append("</pre>");
    	   	sb.append("</div>");
    	   	sb.append("<div style=\"height: 1px; overflow: hidden;border:0px;background-color:black;\"></div>");
    		
    	}
		return sb.toString();
	}		
}