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

package com.liferay.staging.processes.web.internal.portlet.action;

import com.liferay.exportimport.constants.ExportImportPortletKeys;
import com.liferay.exportimport.kernel.configuration.ExportImportConfigurationSettingsMapFactory;
import com.liferay.exportimport.kernel.lar.ExportImportHelper;
import com.liferay.petra.string.StringBundler;
import com.liferay.portal.kernel.portlet.PortletResponseUtil;
import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCResourceCommand;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCResourceCommand;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.staging.constants.StagingProcessesPortletKeys;
import com.liferay.taglib.ui.util.SessionTreeJSClicks;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Minhchau Dang
 */
@Component(
	immediate = true,
	property = {
		"javax.portlet.name=" + StagingProcessesPortletKeys.STAGING_PROCESSES,
		"mvc.command.name=/tree/info"
	},
	service = MVCResourceCommand.class
)
public class TreeInfoMVCResourceCommand extends BaseMVCResourceCommand {

	@Override
	protected void doServeResource(
			ResourceRequest resourceRequest, ResourceResponse resourceResponse)
		throws Exception {

		HttpServletRequest httpServletRequest = _portal.getHttpServletRequest(
			resourceRequest);

		long groupId = ParamUtil.getLong(resourceRequest, "groupId");
		boolean privateLayout = ParamUtil.getBoolean(
			resourceRequest, "privateLayout");

		String treeId = ParamUtil.getString(resourceRequest, "treeId");

		String openNodes = SessionTreeJSClicks.getOpenNodes(
			httpServletRequest, treeId + "SelectedNode");

		String selectedLayoutsJSON = _exportImportHelper.getSelectedLayoutsJSON(
			groupId, privateLayout, openNodes);

		resourceRequest.setAttribute("layoutIdMap", selectedLayoutsJSON);

		long[] layoutIds = _exportImportHelper.getLayoutIds(resourceRequest);

		int layoutIdsCount = layoutIds.length;

		for (long layoutId : layoutIds) {
			if (layoutId <= 0) {
				--layoutIdsCount;
			}
		}

		PortletResponseUtil.write(
			resourceResponse,
			StringBundler.concat(
				layoutIdsCount, " page", (layoutIdsCount == 1) ? "" : "s",
				" selected"));
	}

	@Reference
	private ExportImportConfigurationSettingsMapFactory
		_exportImportConfigurationSettingsMapFactory;

	@Reference
	private ExportImportHelper _exportImportHelper;

	@Reference
	private Portal _portal;

}