/**
 * 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.portal.template;

import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
import com.liferay.portal.kernel.template.TemplateConstants;
import com.liferay.portal.kernel.template.TemplateException;
import com.liferay.portal.kernel.template.TemplateResource;
import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.portal.kernel.util.StringBundler;

import java.io.Writer;

import java.util.List;
import java.util.Map;

/**
 * @author Miroslav Ligas
 */
public abstract class AbstractMultiResourceTemplate extends AbstractTemplate {

	/**
	 * @deprecated As of Mueller (7.2.x), replaced by {@link
	 *             #AbstractMultiResourceTemplate(List, TemplateResource, Map,
	 *             TemplateContextHelper, String, long, boolean)}}
	 */
	@Deprecated
	public AbstractMultiResourceTemplate(
		List<TemplateResource> templateResources,
		TemplateResource errorTemplateResource, Map<String, Object> context,
		TemplateContextHelper templateContextHelper, String templateManagerName,
		long interval) {

		this(
			templateResources, errorTemplateResource, context,
			templateContextHelper, templateManagerName, interval, false);
	}

	public AbstractMultiResourceTemplate(
		List<TemplateResource> templateResources,
		TemplateResource errorTemplateResource, Map<String, Object> context,
		TemplateContextHelper templateContextHelper, String templateManagerName,
		long interval, boolean restricted) {

		super(
			errorTemplateResource, context, templateContextHelper,
			templateManagerName, restricted);

		if (ListUtil.isEmpty(templateResources)) {
			throw new IllegalArgumentException("Template resource is null");
		}

		this.templateResources = templateResources;
	}

	@Override
	public void doProcessTemplate(Writer writer) throws Exception {
		UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

		put(TemplateConstants.WRITER, unsyncStringWriter);

		processTemplates(templateResources, unsyncStringWriter);

		StringBundler sb = unsyncStringWriter.getStringBundler();

		sb.writeTo(writer);
	}

	@Override
	public void processTemplate(Writer writer) throws TemplateException {
		if (errorTemplateResource == null) {
			try {
				processTemplates(templateResources, writer);

				return;
			}
			catch (Exception e) {
				StringBuilder sb = new StringBuilder();

				for (TemplateResource templateResource : templateResources) {
					sb.append(templateResource.getTemplateId());
					sb.append(",");
				}

				throw new TemplateException("Unable to process templates", e);
			}
		}

		write(writer);
	}

	protected abstract void processTemplates(
			List<TemplateResource> templateResource, Writer writer)
		throws Exception;

	protected List<TemplateResource> templateResources;

}