<%--
/**
 * 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.
 *
 *
 *
 */
--%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %><%@
taglib uri="http://liferay.com/tld/clay" prefix="clay" %><%@
taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

<%@ page import="com.liferay.petra.string.StringPool" %><%@
page import="com.liferay.portal.kernel.backgroundtask.BackgroundTask" %><%@
page import="com.liferay.portal.kernel.backgroundtask.BackgroundTaskManagerUtil" %><%@
page import="com.liferay.portal.kernel.backgroundtask.constants.BackgroundTaskConstants" %><%@
page import="com.liferay.portal.kernel.backgroundtask.display.BackgroundTaskDisplay" %><%@
page import="com.liferay.portal.kernel.backgroundtask.display.BackgroundTaskDisplayFactoryUtil" %><%@
page import="com.liferay.portal.kernel.model.CompanyConstants" %><%@
page import="com.liferay.portal.kernel.search.Indexer" %><%@
page import="com.liferay.portal.kernel.search.IndexerClassNameComparator" %><%@
page import="com.liferay.portal.kernel.search.IndexerRegistryUtil" %><%@
page import="com.liferay.portal.kernel.util.ParamUtil" %><%@
page import="com.liferay.product.navigation.control.menu.constants.ProductNavigationControlMenuCategoryKeys" %>

<%@ page import="java.io.Serializable" %>

<%@ page import="java.util.ArrayList" %><%@
page import="java.util.Collections" %><%@
page import="java.util.HashMap" %><%@
page import="java.util.List" %><%@
page import="java.util.Map" %>

<%@ page import="javax.portlet.PortletURL" %>

<portlet:defineObjects />

<%
PortletURL portletURL = renderResponse.createRenderURL();

portletURL.setParameter("mvcRenderCommandName", "/search_admin/view");
%>

<portlet:renderURL var="redirectURL">
	<portlet:param name="mvcRenderCommandName" value="/search_admin/view" />
	<portlet:param name="tabs1" value="index-actions" />
</portlet:renderURL>

<aui:form action="<%= portletURL.toString() %>" method="post" name="fm">
	<aui:input name="redirect" type="hidden" value="<%= redirectURL %>" />

	<%
	Map<String, BackgroundTaskDisplay> classNameToBackgroundTaskDisplayMap = new HashMap<>();

	List<BackgroundTask> reindexPortalBackgroundTasks = BackgroundTaskManagerUtil.getBackgroundTasks(CompanyConstants.SYSTEM, "com.liferay.portal.search.internal.background.task.ReindexPortalBackgroundTaskExecutor", BackgroundTaskConstants.STATUS_IN_PROGRESS);
	List<BackgroundTask> reindexSingleBackgroundTasks = BackgroundTaskManagerUtil.getBackgroundTasks(CompanyConstants.SYSTEM, "com.liferay.portal.search.internal.background.task.ReindexSingleIndexerBackgroundTaskExecutor", BackgroundTaskConstants.STATUS_IN_PROGRESS);

	if (!reindexSingleBackgroundTasks.isEmpty()) {
		for (BackgroundTask backgroundTask : reindexSingleBackgroundTasks) {
			Map<String, Serializable> taskContextMap = backgroundTask.getTaskContextMap();

			String className = (String)taskContextMap.get("className");

			classNameToBackgroundTaskDisplayMap.put(className, BackgroundTaskDisplayFactoryUtil.getBackgroundTaskDisplay(backgroundTask));
		}
	}
	%>

	<clay:container-fluid
		cssClass="container-form-lg search-admin-index-actions-container sheet-lg"
		id='<%= liferayPortletResponse.getNamespace() + "adminSearchAdminIndexActionsPanel" %>'
	>
		<ul class="list-group">
			<li class="list-group-item list-group-item-flex">
				<div class="autofit-col autofit-col-expand">
					<p class="list-group-title">
						<liferay-ui:message key="reindex-all-search-indexes" />
					</p>
				</div>

				<%
				BackgroundTask backgroundTask = null;
				BackgroundTaskDisplay backgroundTaskDisplay = null;

				if (!reindexPortalBackgroundTasks.isEmpty()) {
					backgroundTask = reindexPortalBackgroundTasks.get(0);

					backgroundTaskDisplay = BackgroundTaskDisplayFactoryUtil.getBackgroundTaskDisplay(backgroundTask);
				}
				%>

				<div class="autofit-col index-action-wrapper" data-type="portal">
					<c:choose>
						<c:when test="<%= (backgroundTaskDisplay == null) || !backgroundTaskDisplay.hasPercentage() %>">

							<%
							long timeout = ParamUtil.getLong(request, "timeout");
							%>

							<aui:button cssClass="save-server-button" data-blocking='<%= ParamUtil.getBoolean(request, "blocking") %>' data-cmd="reindex" data-timeout="<%= (timeout == 0) ? StringPool.BLANK : timeout %>" value="execute" />
						</c:when>
						<c:otherwise>
							<%= backgroundTaskDisplay.renderDisplayTemplate() %>
						</c:otherwise>
					</c:choose>
				</div>
			</li>
			<li class="list-group-item list-group-item-flex">
				<div class="autofit-col autofit-col-expand">
					<p class="list-group-title">
						<liferay-ui:message key="reindex-all-spell-check-indexes" />
					</p>
				</div>

				<div class="autofit-col">
					<aui:button cssClass="save-server-button" data-cmd="reindexDictionaries" value="execute" />
				</div>
			</li>

			<%
			List<Indexer<?>> indexers = new ArrayList<>(IndexerRegistryUtil.getIndexers());

			Collections.sort(indexers, new IndexerClassNameComparator(true));

			for (Indexer<?> indexer : indexers) {
				backgroundTaskDisplay = classNameToBackgroundTaskDisplayMap.get(indexer.getClassName());
			%>

				<li class="list-group-item list-group-item-flex">
					<div class="autofit-col autofit-col-expand">
						<p class="list-group-title">
							<liferay-ui:message arguments="<%= indexer.getClassName() %>" key="reindex-x" />
						</p>
					</div>

					<div class="autofit-col index-action-wrapper" data-type="<%= indexer.getClassName() %>">
						<c:choose>
							<c:when test="<%= (backgroundTaskDisplay == null) || !backgroundTaskDisplay.hasPercentage() %>">
								<aui:button cssClass="save-server-button" data-classname="<%= indexer.getClassName() %>" data-cmd="reindex" disabled="<%= !indexer.isIndexerEnabled() %>" value="execute" />
							</c:when>
							<c:otherwise>
								<%= backgroundTaskDisplay.renderDisplayTemplate() %>
							</c:otherwise>
						</c:choose>
					</div>
				</li>

			<%
			}
			%>

		</ul>
	</clay:container-fluid>
</aui:form>

<aui:script use="liferay-admin">
	new Liferay.Portlet.Admin({
		controlMenuCategoryKey:
			'<%= ProductNavigationControlMenuCategoryKeys.TOOLS %>',
		form: document.<portlet:namespace />fm,
		indexActionWrapperSelector: '.index-action-wrapper',
		indexActionsPanel:
			'<%= '#' + liferayPortletResponse.getNamespace() + "adminSearchAdminIndexActionsPanel" %>',
		namespace: '<portlet:namespace />',
		redirectUrl: '<%= redirectURL %>',
		submitButton: '.save-server-button',
		url:
			'<portlet:actionURL name="/search_admin/edit"><portlet:param name="redirect" value="<%= redirectURL %>" /></portlet:actionURL>',
	});
</aui:script>