<%
 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.Utils;
%>

<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 = Utils.sanitize(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"><b><%= _ctx.appRes("UIDefaultSearchResult.label." + resultType) %></b></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;
      showPaginator = (itemsPerPage==currentPageData.size() && SiteSearchService.PAGE_MODE_MORE.equals(pageMode)) || 
                                    (SiteSearchService.PAGE_MODE_PAGINATION.equals(pageMode));
      %>
       <div class="SearchSummary">
        <%= _ctx.appRes("UIDefaultSearchResult.label.resutlsCaption") %> <b><%= startItemIndex %></b> - <b><%= endItemIndex %></b>
        <% if (SiteSearchService.PAGE_MODE_PAGINATION.equals(pageMode)) { %>
          <%= _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 (uicomponent.getRealCurrentPageData().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 {
    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;
     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();
     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>
     <%
     if (dataIndex == dataSize) {
       %>
         </div>
       <%
     }
    }
   %>
    <div class="ItemFunc">
     <% if (showPaginator) uicomponent.renderChild(UICustomizeablePaginator.class); %>
    </div>
   <%
  }
 %>
</div>
