#{param name=labels/}
#{param name=currentLabelId/}
<%
import org.exoplatform.services.security.ConversationState;
import org.exoplatform.commons.utils.HTMLEntityEncoder;

def displayListLabelItem(lbl, level, encoder, identity, colors) {  	 
    def hasChild = (lbl.children != null && lbl.children.size() > 0);
    def hasChildClass = hasChild  ? 'has-child' : '';
	 def indent = (level - 1) * 20;
    def isHidden = lbl.isHidden();
    def isAllChildHidden = true;
    if (hasChild) {
      for (def l in lbl.children) {
            if (!l.isHidden()) {
                isAllChildHidden = false;
            }
        };
    } else {
        isAllChildHidden = false;
    }
%>
    <li class="dropdown label-item ${lbl.color == '' ? 'no-color' : ''} ${isAllChildHidden ? "all-children-hided" : ""} ${currentLabelId == lbl.id ? "active" : ""}" data-labelid="${lbl.id}"
    				data-hiddenlabel="${isHidden}">
        <div class="sub-item" style="padding-left: <%=indent%>px">
                <a class="dropdown-toggle pull-right"
                   data-toggle="dropdown">
                    <i class="uiIconRightMenu uiIconLightGray"></i>
                </a>
                <div class="dropdown-menu uiDropdownWithIcon" style="left: 100%;top: -10px;">
	        	<span class="arrowLeft" style = "top: 22px;"></span>
	        	<ul class="uiDropdownMenu label-menu" data-labelId="${lbl.id}">
                  <li><a href="#" class="openEditDialog"><i class="uiIconEdit uiIconLightGray"></i>&{label.edit}</a></li>
		            <li>
                      <a href="javascript:void(0)" class="actionHideLabel">
                          <i class="uiIconHide uiIconLightGray"></i><span class="label-show">&{label.show}</span><span class="label-hide">&{label.hide}</span>
                      </a>
                  </li>

                  <li><a class="delete-label" href="#"><i class="uiIconTrash uiIconLightGray"></i>&{label.delete}</a></li>
                  <li><a class="new-sub-label" data-labelId="${lbl.id}" href="#"><i class="uiIconAddLabel uiIconLightGray"></i>&{label.addLabel}</a></li>
                  <li class="noColor changeLabelColor" data-color="">&{label.noColor}</li>
                  <li class="calendarTableColor">
                      <%
                          for (def c in colors) { %>
                              <div>
                                  <%for (def color in c) {%>
                                  <a class="${color} colorCell changeLabelColor" data-color="${color}" href="#" style="display: block;">
                                  	<%
                                  		def check = lbl.getColor().equals(color) ? "uiIconTick" : ""; 
                                  	%>
                                  	<i class="${check}"></i></a>
                                  <%}%>
                              </div>
                          <%}
                      %>
                  </li>
		        </ul>
		    </div>
	        <a href="javascript:void(0)" class="${lbl.color == null ? '' : lbl.color} colorPie"><span class=""></span></a>
            <span>
                <a href="javascript:void(0);" class="collapseSubLabel">
                    <i class="${hasChild ? "uiIconArrowDownMini uiIconLightGray" : ""}"></i>
                </a>
                <a href="javascript:void(0);" class="label-name" data-labelid="${lbl.id}"
                        style="vertical-align: middle;display:inline-block;">
                    ${encoder.encode(lbl.name)}
                </a>
            </span>
		</div>
        <% if (lbl.children != null && lbl.children.size() > 0) {
            displayListLabels(lbl.children, lbl.getId(), level, encoder, identity, colors);
        }%>
    </li>
<% }

def displayListLabels(labels, parentId, level, encoder, identity, colors) {  
%>
    <ul class="list-labels" data-parentId="${parentId}">
        <%
            def allLabelAreHidden = true;
        %>
        <%if(labels.size() == 0) {
            allLabelAreHidden = false; 
        %>
        <li class="no-label center muted">
            &{label.noLabel}
        </li>
        <%} else {
        	level++;
        	for (def label in labels) {
                if (!label.isHidden()) allLabelAreHidden = false;
            	displayListLabelItem(label, level, encoder, identity, colors);
        	}
        	level--;
        }
        %>
        <% if (parentId == 0) {%>
        <li class="no-label center muted ${allLabelAreHidden ? "all-label-hidden" : "not-all-label-hidden"}">
            &{label.allLabelAreHidden}
        </li>
        <%}%>
    </ul>
<%}%>

<%
	def encoder = HTMLEntityEncoder.getInstance();
	def identity = ConversationState.getCurrent().getIdentity();
	def colors = [
	              ['asparagus', 'munsell_blue', 'navy_blue', 'purple', 'red', 'brown'],
	              ['laurel_green', 'sky_blue', 'blue_gray', 'light_purple', 'hot_pink', 'light_brown'],
	              ['moss_green', 'powder_blue', 'light_blue', 'pink', 'Orange', 'gray'],
	              ['green', 'baby_blue', 'light_gray', 'beige', 'yellow', 'plum']
	          ];
    displayListLabels(labels, 0, 0, encoder, identity, colors);
%>