/**
 * 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.dynamic.data.lists.web.internal.template;

import com.liferay.dynamic.data.lists.constants.DDLConstants;
import com.liferay.dynamic.data.lists.model.DDLRecordSet;
import com.liferay.dynamic.data.lists.web.internal.configuration.DDLWebConfigurationKeys;
import com.liferay.dynamic.data.lists.web.internal.configuration.DDLWebConfigurationUtil;
import com.liferay.dynamic.data.mapping.model.DDMTemplate;
import com.liferay.dynamic.data.mapping.service.DDMTemplateLocalServiceUtil;
import com.liferay.portal.kernel.configuration.Filter;
import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
import com.liferay.portal.kernel.template.TemplateConstants;
import com.liferay.portal.kernel.template.TemplateHandler;
import com.liferay.portal.kernel.template.TemplateHandlerRegistryUtil;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.HashMapBuilder;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.templateparser.Transformer;

import java.util.Map;

import javax.portlet.RenderRequest;

/**
 * @author Marcellus Tavares
 */
public class DDLDisplayTemplateTransformer {

	public DDLDisplayTemplateTransformer(
		long ddmTemplateId, DDLRecordSet recordSet, ThemeDisplay themeDisplay,
		RenderRequest renderRequest) {

		_ddmTemplateId = ddmTemplateId;
		_recordSet = recordSet;
		_themeDisplay = themeDisplay;
		_renderRequest = renderRequest;
	}

	public String transform() throws Exception {
		Transformer transformer = TransformerHolder.getTransformer();

		String viewMode = Constants.VIEW;

		if (_renderRequest != null) {
			viewMode = ParamUtil.getString(
				_renderRequest, "viewMode", Constants.VIEW);
		}

		Map<String, Object> contextObjects = HashMapBuilder.<String, Object>put(
			DDLConstants.RESERVED_DDM_STRUCTURE_ID,
			_recordSet.getDDMStructureId()
		).put(
			DDLConstants.RESERVED_DDM_TEMPLATE_ID, _ddmTemplateId
		).put(
			DDLConstants.RESERVED_RECORD_SET_DESCRIPTION,
			_recordSet.getDescription(_themeDisplay.getLocale())
		).put(
			DDLConstants.RESERVED_RECORD_SET_ID, _recordSet.getRecordSetId()
		).put(
			DDLConstants.RESERVED_RECORD_SET_NAME,
			_recordSet.getName(_themeDisplay.getLocale())
		).put(
			TemplateConstants.TEMPLATE_ID, _ddmTemplateId
		).put(
			"viewMode", viewMode
		).build();

		DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(
			_ddmTemplateId);

		contextObjects.put(
			TemplateConstants.CLASS_NAME_ID, ddmTemplate.getClassNameId());

		TemplateHandler templateHandler =
			TemplateHandlerRegistryUtil.getTemplateHandler(
				DDLRecordSet.class.getName());

		contextObjects.putAll(templateHandler.getCustomContextObjects());

		return transformer.transform(
			_themeDisplay, contextObjects, ddmTemplate.getScript(),
			ddmTemplate.getLanguage(), new UnsyncStringWriter(),
			PortalUtil.getHttpServletRequest(_renderRequest),
			_themeDisplay.getResponse());
	}

	private final long _ddmTemplateId;
	private final DDLRecordSet _recordSet;
	private final RenderRequest _renderRequest;
	private final ThemeDisplay _themeDisplay;

	private static class TransformerHolder {

		public static Transformer getTransformer() {
			return _transformer;
		}

		private static final Transformer _transformer = new Transformer(
			DDLWebConfigurationKeys.DYNAMIC_DATA_LISTS_ERROR_TEMPLATE, true) {

			@Override
			protected String getErrorTemplateId(
				String errorTemplatePropertyKey, String langType) {

				return DDLWebConfigurationUtil.get(
					errorTemplatePropertyKey, new Filter(langType));
			}

		};

	}

}