<%
 import java.net.URLEncoder;
 import java.util.ArrayList;
 import java.util.List;
 
 import javax.jcr.Node;
 import javax.jcr.Value;
 import javax.servlet.http.HttpServletRequest;
 
 import org.exoplatform.portal.application.PortalRequestContext;
 import org.exoplatform.portal.webui.portal.UIPortal;
 import org.exoplatform.portal.webui.util.Util;
 import org.exoplatform.services.wcm.search.ResultNode;
 import org.exoplatform.wcm.webui.paginator.UICustomizeablePaginator;
 import org.exoplatform.wcm.webui.search.UISearchForm;
 import org.exoplatform.wcm.webui.search.UISearchPageLayout;
%>

<div id="$uicomponent.id" class="UIAdvanceSearchResultDefault">
 <%
  def currentPageData = uicomponent.getCurrentPageData();
  def keyword = uicomponent.getKeyword();
  def resultType = uicomponent.getResultType();
  def rcontext = _ctx.getRequestContext() ;
	def jsManager = rcontext.getJavascriptManager();
	jsManager.importJavascript('eXo.wcm.frontoffice.public.Search', '/eXoWCMResources/javascript/') ;
  %>
   <div class="ResultHeader">
    <div class="CaptionSearchType"><b><%= _ctx.appRes("UIDefaultSearchResult.label." + resultType) %></b></div>
    <% 
     if (!currentPageData.isEmpty()) {
    	def currentPage = uicomponent.getCurrentPage();
    	def itemsPerPage = uicomponent.getItemsPerPage();
      def totalItem = uicomponent.getTotalItem();
      def startItemIndex = (currentPage - 1) * itemsPerPage + 1;
      def endItemIndex = currentPage* itemsPerPage;
      def searchTime = uicomponent.getSearchTime();
      if (endItemIndex > totalItem) endItemIndex = totalItem;
      %>
       <div class="SearchSummary">
        <%= _ctx.appRes("UIDefaultSearchResult.label.resutlsCaption") %> <b><%= startItemIndex %></b> - <b><%= endItemIndex %></b> 
        <%= _ctx.appRes("UIDefaultSearchResult.label.ofAboutCaption") %> <b><%= totalItem %></b> 
        <%= _ctx.appRes("UIDefaultSearchResult.label.forCaption") %> <b><%= keyword %></b> 
        (<b><%= searchTime %></b> <%= _ctx.appRes("UIDefaultSearchResult.label.timeUnitCaption") %>)
       </div>
      <%
     }
    %>
   </div>
  <%
   def suggestion = uicomponent.getSuggestion();
   if (suggestion != null) {
    def uiSearchForm = uicomponent.getAncestorOfType(UISearchPageLayout.class).getChild(UISearchForm.class);
    def suggestionURL = uiSearchForm.event("Search", suggestion);
     %>
     <p>
      <%= _ctx.appRes("UIDefaultSearchResult.msg.new-suggestion") %> : <b style="font-size: 15px; font-style: italic;"><a href="<%= suggestionURL %>"><%= suggestion %></a></b>
     </p>
    <%
   }
   
   if (currentPageData.isEmpty()) {
    %>
     <p>
      <%= _ctx.appRes("UIDefaultSearchResult.msg.your-search") %> <b style="font-size: 15px; font-style: italic;"> <%= keyword %> </b> - 
      <%= _ctx.appRes("UIDefaultSearchResult.msg.did-not-match") %> <%= _ctx.appRes("UIDefaultSearchResult.label." + resultType.toLowerCase()) %>
     </p>
    <%
   } else {
    for (ResultNode resultNode : currentPageData) {
     Node viewNode = resultNode.getNode();
     def itemName = viewNode.getName();
     def itemTitle = uicomponent.getTitle(viewNode);
     if (itemTitle != null && itemTitle.trim().length() != 0) itemName = itemTitle;
     def itemUrl = uicomponent.getURL(viewNode);
     List<String> listNavigationNodeUris = uicomponent.getURLs(viewNode);
     if (!listNavigationNodeUris.isEmpty()) {
      itemUrl = uicomponent.getPublishedNodeURI(listNavigationNodeUris.get(0));
     }
     def editor = resultNode.getEditor();
     def itemExcerpt = resultNode.getExcerpt();
     def itemScore = resultNode.getScore();
     %>
      <div class="SearchItem">
       <div class="ItemTitle">
        <a href="$itemUrl">$itemName</a>
       </div>
       <%if(itemExcerpt != null && itemExcerpt.trim().length() > 0){%>
       		<div class="ItemExcerpt" style="font-weight:bold;"><%=_ctx.appRes("UIDefaultSearchResult.label.editor")%>: $editor</div>
          $itemExcerpt
       <%}%>
       <div class="ItemInfo">
        <span class="ItemUrl"><%=itemUrl%></span> - <span class="ItemScore"><%= itemScore %></span>
        <%
         if (!listNavigationNodeUris.isEmpty()) {
          if (listNavigationNodeUris.size() != 1) {
           %> 
            - <span class="ItemSame" onclick="eXo.ecm.SearchPortlet.showObject(this)">
             <a href="javascript:void(0)"><%= _ctx.appRes("UIDefaultSearchResult.label.similar") %></a>
            </span>
           <%
          }
          %>
           <div class="ItemDetails" id="ItemDetails">
            <%
             for (int i = 1; i < listNavigationNodeUris.size(); i ++) {
              itemUrl = uicomponent.getPublishedNodeURI(listNavigationNodeUris.get(i));
              %>
               <div class="SearchItem">
                <div class="ItemTitle">
                 <a href="$itemUrl">$itemName</a>
                </div>
                <%if(itemExcerpt != null && itemExcerpt.trim().length() > 0){%>
                		<div class="ItemExcerpt"  style="font-weight:bold;"><%=_ctx.appRes("UIDefaultSearchResult.label.editor")%>: <%=itemExcerpt%></div>
                <%}%>
                <div class="ItemInfo">
                 <span class="ItemUrl"><%=itemUrl%></span> - <span class="ItemScore"><%=itemScore%></span>
                </div>
               </div>
              <%
             }
            %>
           </div>
          <%
         }
        %>
       </div>
      </div>
     <%
    }
   %>
    <div class="ItemFunc">
     <% if (uicomponent.getNumberOfPage() > 1) uicomponent.renderChild(UICustomizeablePaginator.class); %>
    </div>
   <%
  }
 %>
</div>
