<%
 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;
 import org.exoplatform.services.wcm.search.SiteSearchService;
 import org.exoplatform.wcm.webui.reader.ContentReader;
 
%>

<div id="$uicomponent.id" class="UIAdvanceSearchResultDefault">
 <%
  def showPaginator = false;
  def pageMode = uicomponent.getPageMode();
  def currentPageData = uicomponent.getCurrentPageData();
  def keyword = uicomponent.getKeyword(); 
  if(keyword != null)
    keyword = ContentReader.getEscapeHtmlContent(keyword);  
  def resultType = uicomponent.getResultType();
  def rcontext = _ctx.getRequestContext() ;
	def jsManager = rcontext.getJavascriptManager();
	jsManager.importJavascript('eXo.wcm.frontoffice.public.Search', '/eXoWCMResources/javascript/');
	jsManager.addOnLoadJavascript("eXo.ecm.SearchPortlet.scrollToBottom('${uicomponent.id}')");
	jsManager.addOnLoadJavascript("eXo.ecm.SearchPortlet.changeColor('NewResultData')");
  %>
   <div class="ResultHeader">
    <div class="CaptionSearchType"><strong><%= _ctx.appRes("UIDefaultSearchResult.label." + resultType) %></strong></div>
    <% 
    if (currentPageData != null && !currentPageData.isEmpty()) {
    	def currentPage = uicomponent.getCurrentPage();
    	def itemsPerPage = uicomponent.getItemsPerPage();
      def totalItem = uicomponent.getTotalItem();
      def startItemIndex = 1;
      def endItemIndex = (currentPage - 1) * itemsPerPage + currentPageData.size();
      def searchTime = uicomponent.getSearchTime();
      if (endItemIndex > totalItem && SiteSearchService.PAGE_MODE_PAGINATION.equals(pageMode) ) endItemIndex = totalItem;
      if (SiteSearchService.PAGE_MODE_PAGINATION.equals(pageMode) ) startItemIndex =  (currentPage - 1) * itemsPerPage + 1;
      showPaginator = (itemsPerPage==currentPageData.size() && SiteSearchService.PAGE_MODE_MORE.equals(pageMode)) || 
                                    (SiteSearchService.PAGE_MODE_PAGINATION.equals(pageMode));
      %>
       <div class="SearchSummary">
        <%= _ctx.appRes("UIDefaultSearchResult.label.resutlsCaption") %> <strong><%= startItemIndex %></strong> - <strong><%= endItemIndex %></strong>
        <% if (SiteSearchService.PAGE_MODE_PAGINATION.equals(pageMode)) { %>
          <%= _ctx.appRes("UIDefaultSearchResult.label.ofAboutCaption") %> <strong><%= totalItem %></strong>
          <%= _ctx.appRes("UIDefaultSearchResult.label.forCaption") %> <strong><%= keyword %></strong>
        <% } %>
        (<strong><%= searchTime %></strong> <%= _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") %> : <strong style="font-size: 15px; font-style: italic;"><a href="<%= suggestionURL %>"><%= suggestion %></a></strong>
     </p>
    <%
   }
   
   if (uicomponent.getRealCurrentPageData().isEmpty()) {
    %>
     <p>
      <%= _ctx.appRes("UIDefaultSearchResult.msg.your-search") %> <strong style="font-size: 15px; font-style: italic;"> <%= keyword %> </strong> - 
      <%= _ctx.appRes("UIDefaultSearchResult.msg.did-not-match") %> <%= _ctx.appRes("UIDefaultSearchResult.label." + resultType.toLowerCase()) %>
     </p>
    <%
   } else {
    def dataSize = uicomponent.getRealCurrentPageData().size();
    int dataIndex = 0;
    for (ResultNode resultNode : uicomponent.getRealCurrentPageData()) {
     if (dataIndex == dataSize - currentPageData.size()) {
       %>
         <div id="NewResultData">
       <%
     }
       
     dataIndex++;
     Node viewNode = resultNode.getNode();
     def itemName = viewNode.getName();
     def itemTitle = uicomponent.getTitle(viewNode);
     if (itemTitle != null && itemTitle.trim().length() != 0) itemName = itemTitle;
     itemName = ContentReader.getXSSCompatibilityContent(itemName);
     def itemUrl = resultNode.getUserNavigationURI();
     List<String> listNavigationNodeUris = new ArrayList<String>();
     if (itemUrl == null || itemUrl.trim().length() == 0) {
       itemUrl = uicomponent.getURL(viewNode);
       listNavigationNodeUris = uicomponent.getURLs(viewNode);
       if (!listNavigationNodeUris.isEmpty()) {
         itemUrl = uicomponent.getPublishedNodeURI(listNavigationNodeUris.get(0));
       }
     }
     def editor = resultNode.getEditor();
     def itemExcerpt = resultNode.getExcerpt();
     String description = resultNode.getDescription();
     def itemScore = resultNode.getScore();
     PortalRequestContext portalRequestContext = Util.getPortalRequestContext();
     String requestURL = portalRequestContext.getRequest().getRequestURL().toString();
     %>
      <div class="SearchItem">
       <div class="ItemTitle">
        <%if(resultType.equals("Document")){%>
          <a href="$itemUrl&urlSearch=$requestURL">$itemName</a>
        <%}else{%>
          <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
       <%} else if (description != null && description.trim().length() > 0){ %>
          <div><span>$description</span></div>
       <%}%>       
       <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">
            <%
             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>
     <%
     if (dataIndex == dataSize) {
       %>
         </div>
       <%
     }
    }
   %>
    <div class="ItemFunc">
     <% if (showPaginator) uicomponent.renderChild(UICustomizeablePaginator.class); %>
    </div>
   <%
  }
 %>
</div>
