<%
import org.exoplatform.portal.webui.util.Util;
import org.exoplatform.portal.config.model.PageNode;
import org.exoplatform.portal.config.model.PageNavigation;
import org.exoplatform.web.application.JavascriptManager;
import org.exoplatform.commons.utils.HTMLEntityEncoder;

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 + '");');

def encoder = HTMLEntityEncoder.getInstance();
def user = uicomponent.getUser();
def fullName = encoder.encode(user.getFullName());

void renderLink() {
		def portalURI = Util.getPortalRequestContext().getPortalURI();
		def labelActivities = _ctx.appRes("UIUserPlatformToolBarPortlet.label.activities");
		def labelProfile = _ctx.appRes("UIUserPlatformToolBarPortlet.label.profile");
		def labelnetwork = _ctx.appRes("UIUserPlatformToolBarPortlet.label.network");
		def labelpeople = _ctx.appRes("UIUserPlatformToolBarPortlet.label.find");
		def labelaccount = _ctx.appRes("UIUserPlatformToolBarPortlet.label.account");
		def labellanguage = _ctx.appRes("UIUserPlatformToolBarPortlet.label.language");
		def labellogout = _ctx.appRes("UIUserPlatformToolBarPortlet.label.logout");
		def remoteUser = Util.getPortalRequestContext().getRemoteUser();
		def activitiesLink = portalURI + "activities";
		def profileLink = portalURI + "profile/" + remoteUser;
		def connectionLink = portalURI + "connections/myConnections/" + remoteUser;
		def directoryLink = portalURI + "people";
		def changeLanguageAction = "if(document.getElementById('UIMaskWorkspace')) ajaxGet(eXo.env.server.createPortalURL('UIPortal', 'ChangeLanguage', true));";
		def accountSetting = "javascript:if(document.getElementById('UIMaskWorkspace')) ajaxGet(eXo.env.server.createPortalURL('UIPortal', 'AccountSettings', true));"
		
		def navigations = uicomponent.getNavigations();

 		String clazz = "";
 		
			print """
				<div style="position: absolute; display:none" class="MenuItemContainer">
					<div class="SubBlock">
				""";
				
				if(navigations != null) {
				 for(nav in navigations) {
					 def pageNodes = nav.getNodes();
				
					 for(PageNode node in pageNodes) {
					 	if (node.getUri() == "profile") {
			         		print """
									<div class="MenuItem">
										<div class="$clazz">						
											<a href="$profileLink" class="ToolBarProfileIcon">$labelProfile</a>
										</div>
									</div>
								""";
			       		} else if (node.getUri() == "activities") {
							print """
									<div class="MenuItem">
										<div class="$clazz">						
											<a href="$activitiesLink" class="ToolBarActivityIcon">$labelActivities</a>
										</div>
									</div>
								""";
			       		} else if (node.getUri() == "connections") {
			         		print """
									<div class="MenuItem">
										<div class="$clazz">						
											<a href="$connectionLink" class="ToolBarNetworkIcon">$labelnetwork</a>
										</div>
									</div>
								""";
			       		} else if (node.getUri() == "people") {
			         		print """
									<div class="MenuItem">
										<div class="$clazz">						
											<a href="$directoryLink" class="FindPeopleIcon">$labelpeople</a>
										</div>
									</div>
								""";
			       		} // end if				   
					  } // end for
				  } // end for				   
				} // end if
				

       			print """
						<div class="MenuItem">
							<div class="$clazz">						
								<a href="$accountSetting" class="AccountSettingIcon">$labelaccount</a>				
							</div>
						</div>       
						<div class="MenuItem" onclick="$changeLanguageAction">
							<div class="$clazz">						
								<a href="#" class="ChangeLanguageIcon">$labellanguage</a>
							</div>
						</div>						
						<div class="MenuItem" onclick="eXo.portal.logout();">
							<div class="$clazz">								
								<a href="#" class="LogoutIcon">$labellogout</a> 						
							</div>
						</div>
				""";				
				
				
			print """
					</div>
				</div>
			""";
	}
%>
<div class="UIUserToolBarPortlet" id="$uicomponent.id">
    <div class="UIHorizontalTabs">
        <div class="TabsContainer" style="position: relative">
            <div class="UITab NormalToolbarTab">
                <div class="">
                            <a class="ArrowIcon TBIcon" href="#">
                                   $fullName
                            </a>
                </div>
                <% renderLink() %>
                
            </div>
        </div>
    </div>
</div>
