<%
        import org.exoplatform.portal.mop.user.UserNode;
        import org.exoplatform.portal.mop.SiteKey;
        import org.exoplatform.portal.mop.SiteType;
	import org.exoplatform.portal.webui.util.Util ;
	import org.exoplatform.web.application.JavascriptManager;
	import org.exoplatform.web.url.navigation.NavigationResource;
	import org.exoplatform.web.url.PortalURL;
	import org.exoplatform.platform.webui.NavigationURLUtils;
    
	import java.util.Collection;
    
	import javax.portlet.MimeResponse;
	import javax.portlet.ResourceURL;

	def rcontext = _ctx.getRequestContext() ;
	JavascriptManager jsmanager = rcontext.getJavascriptManager();
	jsmanager.importJavascript('eXo.portal.UIPortalNavigation');
	jsmanager.importJavascript('eXo.portal.UIAdminToolbar');
	jsmanager.addCustomizedOnLoadScript('eXo.portal.UIAdminToolbar.onLoad("' + uicomponent.id + '");');

	void renderDashboards(PortalURL nodeURL, Collection nodes) {
		String clazz = "";

		print """
		<ul style="display:none" class="MenuItemContainer">
		""";
		//int idx = 0;
		for(UserNode node : nodes) {
			renderPageNode(nodeURL, node);
		}
			   print """
		</ul>
		""" ;
	}

	void renderPageNode(PortalURL nodeURL, UserNode node) {
		if (uicomponent.isWebOSNode(node)) return;
		UserNode selectedNode = uicomponent.getSelectedNode();
		String tabStyleNavigation = "";
		if(selectedNode != null && node.getURI() == selectedNode.getURI()) {
			tabStyleNavigation = "SelectedItem";
		}

		boolean hasChild = (node.getChildren() != null && node.getChildren().size() > 0);
		String clazz = "";
		if(hasChild) clazz = "ArrowIcon";
		String href = nodeURL.setNode(node);
		String icon = node.getIcon();
		if(icon == null) icon = "DefaultPageIcon";
		def resolvedLabel = node.getEncodedResolvedLabel();
		/*if(resolvedLabel == null) {
		  resolvedLabel = node.getName();
		}*/
		if ("Tab_Default".equals(node.getName())){
			resolvedLabel = _ctx.appRes("UIUserToolbarDesktopPortlet.page." + resolvedLabel);
		}

		boolean toolong = (resolvedLabel.length() > 60);
		String label = ( toolong ? resolvedLabel.substring(0, 57) + "..." : resolvedLabel);
		String title = "";
		if(toolong) title = "title='$resolvedLabel'";
		else title = "";
				
		def getNodeURL = "";
		if (hasChild) {
			MimeResponse res = _ctx.getRequestContext().getResponse();
			ResourceURL resourceURL = res.createResourceURL();
			resourceURL.setResourceID(res.encodeURL(node.getURI()));
			getNodeURL = "exo:getNodeURL='" + resourceURL.toString() + "'";
		}
			
		print """
		<li class="MenuItem $clazz $tabStyleNavigation" $getNodeURL>
				""";
				if(node.pageRef != null) {
					print """<a class="ItemIcon $icon" href="$href" $title>$label</a>""";
				} else {
					print """<a class="ItemIcon $icon" href="#" $title>$label</a>""";
				}
			if(hasChild) {
				print """
				<ul class="MenuItemContainer">
						""" ;
						for(UserNode child : node.getChildren()) {

                                                          renderPageNode(nodeURL, child);
						}
						print """
				</ul>
				""" ;
			}
			print """
		</li>
		""" ;
	}
%>

<%
	def userNodes = uicomponent.getUserNodes(uicomponent.getCurrentUserNavigation());
	boolean webosProfile = uicomponent.isWebOsProfileActivated();
    String dashboardLink;
    
    PortalURL dashboardUrl = nodeurl();
    UserNode dashboardNode = uicomponent.findDashboardNode();
    if (dashboardNode != null)
    {
       dashboardLink = dashboardUrl.setNode(dashboardNode).toString();
     }
    else
    {
       dashboardUrl.setResource(new NavigationResource(SiteType.USER, rcontext.getRemoteUser(), null));
       dashboardLink = dashboardUrl.toString();
     }
    String desktopLink = nodeurl().setResource(new NavigationResource(SiteType.USER, rcontext.getRemoteUser(), "classicWebosPage")).toString();
    
    
    /*String desktopLink;
    if (uicomponent.hasDashboardNode()) {
       dashboardLink = uicomponent.getDashboardURL();
    } else {
       dashboardLink = uicomponent.url("AddDefaultDashboard", uicomponent.DEFAULT_TAB_NAME);
    }
    
    if (uicomponent.isWebOSCreated()) {
       desktopLink = NavigationURLUtils.getURL(SiteKey.user(rcontext.getRemoteUser()), UIDesktopPage.NODE_NAME);
    } else {
       desktopLink = uicomponent.url("CreateWebOS");
    } */
%>
<ul class="UIUserToolBarDesktopPortlet UIHorizontalTabs" id="$uicomponent.id">
	<li class="UITab NormalToolbarTab">
		<a class="DashboardIcon TBIcon" href="<%= dashboardLink %>"><%=_ctx.appRes("UIUserPlatformToolbarDesktopPortlet.header.dashboard")%></a>
		
		<% renderDashboards(dashboardUrl, userNodes); %>
		
		
	</li>
<%if(webosProfile) {%>		
	<li class="UITab NormalToolbarTab">
			<a class="DashboardIcon TBIcon" href="<%= desktopLink %>"><%=_ctx.appRes("UIUserPlatformToolbarDesktopPortlet.header.desktop")%></a>
	</li>
<% } %>
</ul>
