<%--
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */
--%>

<aui:button-row>
	<aui:button onClick='<%= liferayPortletResponse.getNamespace() + "addSearchProvider();" %>' value='<%= LanguageUtil.format(request, "add-x-as-a-search-provider", HtmlUtil.escape(company.getName()), false) %>' />
</aui:button-row>

<%
String primarySearch = ParamUtil.getString(request, "primarySearch");

if (Validator.isNotNull(primarySearch)) {
	portalPreferences.setValue(SearchPortletKeys.SEARCH, "primary-search", primarySearch);
}
else {
	primarySearch = portalPreferences.getValue(SearchPortletKeys.SEARCH, "primary-search", StringPool.BLANK);
}

List<OpenSearch> openSearchInstances = com.liferay.portal.search.web.internal.util.SearchUtil.getOpenSearchInstances(primarySearch);

List<String> modelResources = new ArrayList<String>();

for (OpenSearch openSearchInstance : openSearchInstances) {
	modelResources.add(ResourceActionsUtil.getModelResource(locale, openSearchInstance.getClassName()));
}
%>

<div class="search-msg">
	<c:choose>
		<c:when test="<%= modelResources.isEmpty() %>">
			<liferay-ui:message key="no-widgets-were-searched" />
		</c:when>
		<c:otherwise>
			<liferay-ui:message key="searched" /> <%= StringUtil.merge(modelResources, StringPool.COMMA_AND_SPACE) %>
		</c:otherwise>
	</c:choose>
</div>

<%
int totalResults = 0;

for (int i = 0; i < openSearchInstances.size(); i++) {
	OpenSearch openSearch = openSearchInstances.get(i);

	int delta = 5;

	if (Validator.isNotNull(primarySearch) && primarySearch.equals(openSearch.getClassName())) {
		delta = ParamUtil.getInteger(request, SearchContainer.DEFAULT_DELTA_PARAM + i, SearchContainer.DEFAULT_DELTA);
	}

	SearchContainer<?> openSearchSearchContainer = new SearchContainer(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM + i, delta, searchDisplayContext.getPortletURL(), null, LanguageUtil.format(request, "no-results-were-found-that-matched-the-keywords-x", "<strong>" + HtmlUtil.escape(searchDisplayContext.getKeywords()) + "</strong>", false));

	String xml = openSearch.search(request, groupId, themeDisplay.getUserId(), searchDisplayContext.getKeywords(), openSearchSearchContainer.getCur(), openSearchSearchContainer.getDelta(), format);

	int inactiveGroupsCount = GroupLocalServiceUtil.searchCount(
		themeDisplay.getCompanyId(), null, null,
		LinkedHashMapBuilder.<String, Object>put(
			"active", Boolean.FALSE
		).build());

	Tuple tuple = com.liferay.portal.search.web.internal.util.SearchUtil.getElements(xml, openSearch.getClassName(), inactiveGroupsCount);

	List<Element> resultRows = (List<Element>)tuple.getObject(0);
	int totalRows = (Integer)tuple.getObject(1);
%>

	<c:if test="<%= !resultRows.isEmpty() %>">

		<%
		totalResults = totalResults + totalRows;
		%>

		<liferay-ui:search-container
			searchContainer="<%= openSearchSearchContainer %>"
			total="<%= totalRows %>"
		>
			<liferay-ui:search-container-results
				results="<%= resultRows %>"
			/>

			<div class="section-title">

				<%
				boolean filterSearch = false;

				if (openSearch instanceof HitsOpenSearchImpl) {
					HitsOpenSearchImpl hitsOpenSearchImpl = (HitsOpenSearchImpl)openSearch;

					Indexer<?> hitsOpenSearchImplIndexer = hitsOpenSearchImpl.getIndexer();

					filterSearch = hitsOpenSearchImplIndexer.isFilterSearch();
				}

				// When the total is <= to the delta, we know the result is accurate
				// since we've filtered the entire result set. Otherwise, we simply
				// indicate that there are possibly many more. (� la Google)

				%>

				<c:choose>
					<c:when test="<%= filterSearch && (searchContainer.getTotal() > searchContainer.getDelta()) %>">
						<%= ResourceActionsUtil.getModelResource(locale, openSearch.getClassName()) %> (<%= searchContainer.getDelta() %> <liferay-ui:message key="of-many" />)
					</c:when>
					<c:otherwise>
						<%= ResourceActionsUtil.getModelResource(locale, openSearch.getClassName()) %> (<%= searchContainer.getTotal() %>)
					</c:otherwise>
				</c:choose>
			</div>

			<liferay-ui:search-container-row
				className="com.liferay.portal.kernel.xml.Element"
				modelVar="element"
			>
				<liferay-ui:search-container-column-text>

					<%
					String entryClassName = element.elementText("entryClassName");
					long entryClassPK = GetterUtil.getLong(element.elementText("entryClassPK"));
					String entryTitle = element.elementText("title");
					String[] queryTerms = StringUtil.split(element.elementText("queryTerms"), StringPool.COMMA_AND_SPACE);
					String summary = element.elementText("summary");
					%>

					<a class="entry-title" href="<%= com.liferay.portal.search.web.internal.util.SearchUtil.getSearchResultViewURL(renderRequest, renderResponse, entryClassName, entryClassPK, searchDisplayContext.isViewInContext(), currentURL) %>">
						<%= HighlightUtil.highlight(HtmlUtil.escape(entryTitle), queryTerms) %>
					</a>

					<c:if test="<%= Validator.isNotNull(summary) %>">
						<br />

						<%= HighlightUtil.highlight(HtmlUtil.escape(summary), queryTerms) %>
					</c:if>

					<br />

					<liferay-asset:asset-tags-summary
						className="<%= entryClassName %>"
						classPK="<%= entryClassPK %>"
						paramName="<%= Field.ASSET_TAG_NAMES %>"
						portletURL="<%= searchDisplayContext.getPortletURL() %>"
					/>
				</liferay-ui:search-container-column-text>
			</liferay-ui:search-container-row>

			<liferay-ui:search-iterator
				markupView="deprecated"
				paginate="<%= false %>"
			/>

			<c:choose>
				<c:when test="<%= (searchContainer.getTotal() == resultRows.size()) || (Validator.isNotNull(primarySearch) && openSearch.getClassName().equals(primarySearch)) %>">
					<div class="search-paginator-container">
						<liferay-ui:search-paginator
							searchContainer="<%= searchContainer %>"
							type="more"
						/>
					</div>
				</c:when>
				<c:otherwise>
					<div class="more-results">
						<portlet:renderURL var="moreResultsURL">
							<portlet:param name="struts_action" value="/search/search" />
							<portlet:param name="groupId" value="<%= String.valueOf(groupId) %>" />
							<portlet:param name="primarySearch" value="<%= openSearch.getClassName() %>" />
							<portlet:param name="keywords" value="<%= HtmlUtil.escape(searchDisplayContext.getKeywords()) %>" />
							<portlet:param name="format" value="<%= format %>" />
						</portlet:renderURL>

						<aui:a href="<%= moreResultsURL %>">
							<liferay-ui:message arguments="<%= ResourceActionsUtil.getModelResource(locale, openSearch.getClassName()) %>" key="more-x-results" translateArguments="<%= false %>" /> &raquo;
						</aui:a>
					</div>
				</c:otherwise>
			</c:choose>
		</liferay-ui:search-container>
	</c:if>

<%
}
%>

<c:if test="<%= totalResults == 0 %>">
	<div class="no-results">
		<liferay-ui:message arguments='<%= "<strong>" + HtmlUtil.escape(searchDisplayContext.getKeywords()) + "</strong>" %>' key="no-results-were-found-that-matched-the-keywords-x" translateArguments="<%= false %>" />
	</div>
</c:if>

<%
String pageDescription = LanguageUtil.get(request, "search-results");

if (!modelResources.isEmpty()) {
	pageDescription = LanguageUtil.get(request, "searched") + StringPool.SPACE + StringUtil.merge(modelResources, StringPool.COMMA_AND_SPACE);
}

PortalUtil.setPageDescription(pageDescription, request);
%>