<%@page import="com.gridnine.xtrip.server.ibus.rest.docs.RestDocItem"%>
<%@page import="com.gridnine.xtrip.server.ibus.rest.docs.RestDocMethod"%>
<%@page import="com.gridnine.xtrip.server.ibus.rest.docs.RestDocGroup"%>
<%@page import="java.util.List"%>
<%@page import="com.gridnine.xtrip.server.ibus.rest.docs.RestDocHelper"%>
<%@page import="com.gridnine.xtrip.common.util.TranslitUtil.TranslitRules"%>
<%@page import="com.gridnine.xtrip.common.util.TranslitUtil"%>
<%@page import="java.util.Map.Entry"%>
<%@page import="com.gridnine.xtrip.server.ibus.IntegrationBusRegistry"%>
<%@page import="com.gridnine.xtrip.common.Environment"%>
<%@page import="com.gridnine.xtrip.server.ibus.model.RestfulInEndpointDescription"%>
<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%!
public static boolean is(HttpServletRequest req, String path) {
    path = path.replace(" ", "%20");
    System.out.println("req.getQueryString()=" + req.getQueryString() + ", path=" + path);
	return req.getQueryString().endsWith(path);
}

public static String maybeActive(HttpServletRequest req, String suffix) {
    return is(req, suffix) ? "class=\"active\"" : "";
}
%>
<%
String leftDescrId = request.getParameter("id");
RestfulInEndpointDescription leftDescr =  Environment.getPublished(IntegrationBusRegistry.class).getEndpoint(leftDescrId, RestfulInEndpointDescription.class);
List<RestDocGroup> leftGroups = RestDocHelper.getGroups(leftDescr);
%>
<ul class="nav nav-list">
    <li class="nav-header"><h3>REST API</h3></li>
<%
for(String item: leftDescr.getIssues().keySet()){
	String translit = TranslitUtil.cyr2lat(item, TranslitRules.GOST_52535_1_2006);
%>
    <li <%= maybeActive(request, "&issueId=" + translit) %>><a href="showDescription.jsp?id=<%=leftDescrId%>&issueId=<%=translit%>"><%=item%></a></li>
<%
}
for(String item: leftDescr.getIssueBuilderList().keySet()){
	String translit = TranslitUtil.cyr2lat(item, TranslitRules.GOST_52535_1_2006);
%>
    <li <%= maybeActive(request, "&issueId=" + translit) %>><a href="showDescription.jsp?id=<%=leftDescrId%>&issueId=<%=translit%>"><%=item%></a></li>
<%
}
%>
	<li class="divider"></li>
<%
for(RestDocGroup group: leftGroups){
%>

    <li> <%= group.getGroupName()%>
    <ul class="nav nav-list">
<%
for(RestDocItem aMethod: group.getItems()){
	String translit = TranslitUtil.cyr2lat(aMethod.getIssueId(), TranslitRules.GOST_52535_1_2006);
	final StringBuffer sb = new StringBuffer(); 
    for(String docRequestType: aMethod.getMethods().keySet()){
        if (sb.length() > 0)
            sb.append("_");
        sb.append(docRequestType);
    }
	String toMethodId = TranslitUtil.cyr2lat(sb.toString(), TranslitRules.GOST_52535_1_2006);
%>
    <li <%= maybeActive(request, "&itemId=" + translit + "&methodId=" + toMethodId) %>><a href="showDescription.jsp?id=<%=leftDescrId%>&itemId=<%=translit%>&methodId=<%=toMethodId%>"><%=aMethod.getItemName()%><br/><small style="color:black">
<%
for(String docRequestType: aMethod.getMethods().keySet()){
    %><%=docRequestType + " "%><%
}
%>    
    	<%=translit%></small>
    </a></li>
<%
}
%>
</ul>
</li>
<%
}
%>
</ul>