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

import com.eactive.eai.adapter.socket2.common.ExceptionStackTrace;
import com.eactive.eai.adapter.socket2.common.TimeUtil;
import com.eactive.eai.adapter.socket2.config.SocketAdapterManager;
import com.eactive.eai.adapter.socket2.config.SocketLoggerManager;
import com.eactive.eai.adapter.socket2.protocol.SocketAdapter;
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;

public class ErrorServlet extends HttpServlet {
	static final SocketAdapterManager manager = SocketAdapterManager.getInstance();
    static Logger logger = SocketLoggerManager.getInstance().getLogger("FileLogger{NET}_HTTP");
    static String uptime = TimeUtil.formatDate(System.currentTimeMillis());

	protected void doService(HttpRequest request, HttpResponse response) throws Exception {
		String filter = request.getContext();
    	filter = filter.substring(filter.indexOf('/')+1);
    	if ("error".equals(filter)) {
    		filter = null;
    	}

    	String result = "";
    	
    	try {
	    	result = error(filter);
		} catch(Throwable e) {
			result = ExceptionStackTrace.dump(e);
		}  	
		
		response.setContentType("text/html");
		response.setResponseCode(HttpResponse.HTTP_STATUS_SUCCESS);
		response.appendBody("<b>uptime : " + uptime + "</b>");
		response.appendBody("<br><br>");
		response.appendBody("<a href='/adapters'>socket adapters</a>&nbsp;&nbsp;");
		response.appendBody("<a href='/zombies'>socket zombies</a>&nbsp;&nbsp;|&nbsp;");
		response.appendBody("<a href='/http'>http adapters</a>&nbsp;&nbsp;|&nbsp;");
//		response.appendBody("<a href='/error'>socket adapters error</a>&nbsp;&nbsp;|&nbsp;");
		//2020.03.04 링크추가요청반영
		response.appendBody("<a href='/layout'>layout</a>&nbsp;&nbsp;");
		response.appendBody("<a href='/transform'>transform</a>&nbsp;&nbsp;");
		response.appendBody("<a href='/transformer/error'>transformer/error</a>&nbsp;&nbsp;");
		response.appendBody("<br>");
		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";
	
	private String error(String filter) throws Exception {
		StringBuffer sb = new StringBuffer();
    	String[] errors = SocketAdapter.getGlobalErrors();

    	sb.append(SCRIPT);
    	sb.append("<h3>socket adapter global errors</h3>");
    	for (int i=errors.length-1; i>=0; i--) {
	    	if (filter != null && errors[i].toLowerCase().indexOf(filter.toLowerCase()) == -1) {
		    	continue;	
	    	}
	    	
	    	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(errors[i].replace(">", "&gt;").replace("<", "&lt;"));
    	   	sb.append("</pre>");
    	   	sb.append("</div>");
    	   	sb.append("<div style=\"height: 1px; overflow: hidden;border:0px;background-color:black;\"></div>");
	    }
		return sb.toString();		
		
	}
}
