#set ($results = $params.get("results"))
#set ($showExcerpts = "true")
#set ($searchWords = $params.get("searchWords"))
#set ($queryString = $params.get("queryString"))
#set ($ignoredWords = $params.get("ignoredWords"))
#set ($topCell = $params.get("topCell"))
#set ($start = $params.get("start"))
#set ($end = $params.get("end"))
#set ($total = $params.get("total"))

#if ($params.get("showExcerpts"))
    #set ($showExcerpts = $params.get("showExcerpts"))
#end

#macro (didyoumean)
    #if ($action.plainQuerySuggestion)
    <div id="didyoumean-suggestion-container">
        <span id="didyoumean-label">Did you mean:</span> <a href="$req.contextPath/${formAction}queryString=$action.plainQuerySuggestion">$action.decoratedQuerySuggestion</a>
    </div>
    #end
#end


##TODO: Refactor these into another vm file.
#macro (searchResultIcon $searchResult)
    #if ($searchResult.type == "userinfo")
        <img src="$req.contextPath$action.contentTypesDisplayMapper.getIconUrlForUsername($searchResult.extraFields.username)" />
    #end
#end

#macro (newSearchResultContentLink $searchResult)
    #set ($path = $searchResult.urlPath)
	#if ($searchResult.type == "attachment")
        #set ($path = $searchResult.extraFields.attachmentDownloadPath)
	#end
    <a href="$req.contextPath$path" class="#if ($searchResult.type == "userinfo") confluence-userlink username:$generalUtil.urlEncode($searchResult.extraFields.username) url fn #end $action.contentTypesDisplayMapper.getClassName($searchResult)">$action.getTitleForResult($searchResult)<span class="icon">#searchResultIcon($searchResult)</span></a>##TODO: Highlight matches in titles
#end

#macro (newSearchResult $searchResult $showExcerpts $queryString)
    ## TODO: Show labels only if there is a matching label
	<h3 class="search-result-title">#newSearchResultContentLink($searchResult)#if ($searchResult.type == "userinfo") ($generalUtil.htmlEncode($searchResult.extraFields.username))#end</h3>

    <fieldset class="hidden">
        <input type="hidden" class="search-result-entry-content-id" value="${searchResult.handle.toString()}">
    </fieldset>

	#set ($webInterfaceContext = $action.getWebInterfaceContext($searchResult))
	#set ($primaryItems = $webInterfaceManager.getDisplayableItems("system.search/result.primary", $webInterfaceContext))

	#if ($searchResult.type == "attachment" || $primaryLinks.size())
    <ul class="search-result-metadata">
        #if($searchResult.type == "attachment")
        <li>$!searchResult.extraFields.attachmentReadableFileSize</li>
        #if ($searchResult.extraFields.attachmentTypeDescription)
            <li>$!searchResult.extraFields.attachmentTypeDescription</li>
        #end
        #end
        #foreach ($item in $primaryItems)
            <li><a #if ($item.link.id) id="$item.link.id" #end href="$item.link.getDisplayableUrl($req, $webInterfaceContext)">$item.label.getDisplayableLabel($req, $webInterfaceContext)</a></li>
        #end
        <li><a href="$req.contextPath$searchResult.extraFields.attachmentDownloadPath">$action.getText('download.name')</a></li>
    </ul>
	#end
    #if ("true" == $showExcerpts)
        <span class="search-result-summary">$action.getSummaryForResult($searchResult)</span>
    #end
    #if ($searchResult.type != "spacedesc" && $searchResult.type != "personalspacedesc")
    <ul class="search-result-metadata">
        #if ($searchResult.type != "userinfo")
            #if ($searchResult.type == "attachment")
                <li><a href="$req.contextPath/display/$generalUtil.urlEncode($searchResult.spaceKey)">$generalUtil.htmlEncode($searchResult.spaceName)</a>
                    &gt; <a href="$req.contextPath$searchResult.extraFields.containingContentUrlPath">$generalUtil.htmlEncode($searchResult.extraFields.containingContentDisplayTitle)</a>
                    &gt; <a href="$req.contextPath$searchResult.urlPath">$action.getText('type.attachments')</a></li>
            #elseif ($searchResult.type == "mail")
                <li><a href="$req.contextPath/display/$generalUtil.urlEncode($searchResult.spaceKey)">$generalUtil.htmlEncode($searchResult.spaceName)</a>
                    &gt; <a href="$req.contextPath$searchResult.urlPath">$action.getText('type.mail')</a></li>
            #elseif ($searchResult.type == "status")
                <li><a href="$req.contextPath/display/~$generalUtil.urlEncode($searchResult.creator)">$generalUtil.htmlEncode($searchResult.extraFields.creatorFullName)</a></li>
            #else
                <li><a href="$req.contextPath/display/$generalUtil.urlEncode($searchResult.spaceKey)">$generalUtil.htmlEncode($searchResult.spaceName)</a></li>
            #end
            <li>$action.dateFormatter.format($searchResult.lastModificationDate)</li>
        #else
            #set ($email = $generalUtil.maskEmail($searchResult.extraFields.email))
            #if ($email && "hidden" != $email)
            <li>#if ($email == $searchResult.extraFields.email)<a href="mailto:$email">$email</a>
                #else $email #end</li>
            #end
        #end
    </ul>
    #end
#end


#set ($formAction = "dosearchsite.action?")
#if ($stringUtils.isNotBlank($queryString))
    #if ($results && $results.size() > 0)
		<h2 class="search-results-count">
		#if ($topCell)
			$topCell
		#else
			#set ($start = $start + 1)
			#if($ignoredWords && !$ignoredWords.isEmpty())
				$action.getText("search.result.with.ignored", [$start, $end, $total, $searchWords, $ignoredWords])
			#else
				$action.getText("search.result", [$start, $end, $total, $generalUtil.escapeXml($queryString)])
			#end
		#end
		</h2>
        #didyoumean()
		<div id="search-results-body">
            <ul class="search-results">
                #foreach($searchResult in $results)
                <li>
                    #newSearchResult($searchResult $showExcerpts $queryString)
                </li>
                #end
            </ul>
        </div>
		#pagination($action.paginationSupport "${formAction}${action.currentSearch.queryParameters}&")
	#else
        #didyoumean()
		$action.getText("search.no.results.found", ["<strong>${generalUtil.htmlEncode($queryString)}</strong>"])
		<ul>
			<li>$action.getText("search.tip.general")</li>
			<li>$action.getText("search.tip.restrictions")</li>
		</ul>
	#end
#end
