/**
 * 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.headless.delivery.internal.dto.v1_0.converter;

import com.liferay.document.library.kernel.model.DLFolder;
import com.liferay.headless.delivery.dto.v1_0.StructuredContentFolder;
import com.liferay.headless.delivery.internal.dto.v1_0.util.CreatorUtil;
import com.liferay.headless.delivery.internal.dto.v1_0.util.CustomFieldsUtil;
import com.liferay.journal.model.JournalFolder;
import com.liferay.journal.service.JournalArticleService;
import com.liferay.journal.service.JournalFolderService;
import com.liferay.portal.kernel.service.UserLocalService;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portal.vulcan.dto.converter.DTOConverter;
import com.liferay.portal.vulcan.dto.converter.DTOConverterContext;
import com.liferay.subscription.service.SubscriptionLocalService;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Rubén Pulido
 * @author Víctor Galán
 */
@Component(
	property = "dto.class.name=com.liferay.journal.model.JournalFolder",
	service = {DTOConverter.class, StructuredContentFolderDTOConverter.class}
)
public class StructuredContentFolderDTOConverter
	implements DTOConverter<DLFolder, StructuredContentFolder> {

	@Override
	public String getContentType() {
		return StructuredContentFolder.class.getSimpleName();
	}

	@Override
	public StructuredContentFolder toDTO(
			DTOConverterContext dtoConverterContext)
		throws Exception {

		JournalFolder journalFolder = _journalFolderService.getFolder(
			(Long)dtoConverterContext.getId());

		return new StructuredContentFolder() {
			{
				creator = CreatorUtil.toCreator(
					_portal, dtoConverterContext.getUriInfoOptional(),
					_userLocalService.fetchUser(journalFolder.getUserId()));
				customFields = CustomFieldsUtil.toCustomFields(
					dtoConverterContext.isAcceptAllLanguages(),
					JournalFolder.class.getName(), journalFolder.getFolderId(),
					journalFolder.getCompanyId(),
					dtoConverterContext.getLocale());
				dateCreated = journalFolder.getCreateDate();
				dateModified = journalFolder.getModifiedDate();
				description = journalFolder.getDescription();
				id = journalFolder.getFolderId();
				name = journalFolder.getName();
				numberOfStructuredContentFolders =
					_journalFolderService.getFoldersCount(
						journalFolder.getGroupId(),
						journalFolder.getFolderId());
				numberOfStructuredContents =
					_journalArticleService.getArticlesCount(
						journalFolder.getGroupId(), journalFolder.getFolderId(),
						WorkflowConstants.STATUS_APPROVED);
				siteId = journalFolder.getGroupId();
				subscribed = _subscriptionLocalService.isSubscribed(
					journalFolder.getCompanyId(),
					dtoConverterContext.getUserId(),
					JournalFolder.class.getName(), journalFolder.getFolderId());

				setParentStructuredContentFolderId(
					() -> {
						if (journalFolder.getParentFolderId() == 0L) {
							return null;
						}

						return journalFolder.getParentFolderId();
					});
			}
		};
	}

	@Reference
	private JournalArticleService _journalArticleService;

	@Reference
	private JournalFolderService _journalFolderService;

	@Reference
	private Portal _portal;

	@Reference
	private SubscriptionLocalService _subscriptionLocalService;

	@Reference
	private UserLocalService _userLocalService;

}