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

<%
List<AssetRendererFactory<?>> availableAssetRendererFactories = ListUtil.filter(AssetRendererFactoryRegistryUtil.getAssetRendererFactories(company.getCompanyId()), AssetRendererFactory::isCategorizable);

List<AssetRendererFactory<?>> classTypedAssetRendererFactories = ListUtil.filter(availableAssetRendererFactories, AssetRendererFactory::isSupportsClassTypes);

long[] selectedClassNameIds = AssetVocabularySettingsHelper.DEFAULT_SELECTED_CLASS_NAME_IDS;
long[] selectedClassTypePKs = AssetVocabularySettingsHelper.DEFAULT_SELECTED_CLASS_TYPE_PKS;

if (vocabulary != null) {
	selectedClassNameIds = vocabulary.getSelectedClassNameIds();
	selectedClassTypePKs = vocabulary.getSelectedClassTypePKs();
}
%>

<liferay-ui:error exception="<%= NoSuchClassTypeException.class %>" message="one-of-your-associated-asset-types-contains-an-invalid-value.-please-configure-it-again-or-remove-it" />

<liferay-frontend:fieldset
	collapsed="<%= true %>"
	collapsible="<%= true %>"
	id='<%= liferayPortletResponse.getNamespace() + "extraFields" %>'
	label="associated-asset-types"
>

	<%
	List<String> indexes = new ArrayList<String>();

	for (int index = 0; index < selectedClassNameIds.length; index++) {
		indexes.add(String.valueOf(index));

		long classNameId = selectedClassNameIds[index];
		final long classTypePK = selectedClassTypePKs[index];

		boolean exist = true;

		if ((classNameId > 0) && (classTypePK != AssetCategoryConstants.ALL_CLASS_TYPE_PK)) {
			AssetRendererFactory<?> assetRendererFactory = AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassNameId(classNameId);

			ClassTypeReader classTypeReader = assetRendererFactory.getClassTypeReader();

			List<ClassType> classTypes = classTypeReader.getAvailableClassTypes(new long[] {themeDisplay.getCompanyGroupId(), scopeGroupId}, themeDisplay.getLocale());

			if (!classTypes.isEmpty()) {
				exist = ListUtil.exists(classTypes, classType -> classType.getClassTypeId() == classTypePK);
			}
		}
	%>

		<div class="lfr-form-row lfr-form-row-inline">
			<div class="row-fields">
				<aui:select cssClass="vocabulary-asset-type-select" inlineField="<%= true %>" label="choose-asset-type" name='<%= "classNameId" + index %>'>
					<aui:option label="all-asset-types" value="<%= AssetCategoryConstants.ALL_CLASS_NAME_ID %>" />

					<%
					for (AssetRendererFactory<?> assetRendererFactory : availableAssetRendererFactories) {
					%>

						<aui:option label="<%= ResourceActionsUtil.getModelResource(locale, assetRendererFactory.getClassName()) %>" selected="<%= classNameId == assetRendererFactory.getClassNameId() %>" value="<%= assetRendererFactory.getClassNameId() %>" />

					<%
					}
					%>

				</aui:select>

				<%
				for (AssetRendererFactory<?> assetRendererFactory : classTypedAssetRendererFactories) {
					ClassTypeReader classTypeReader = assetRendererFactory.getClassTypeReader();

					List<ClassType> classTypes = classTypeReader.getAvailableClassTypes(new long[] {themeDisplay.getCompanyGroupId(), scopeGroupId}, themeDisplay.getLocale());

					if (classTypes.isEmpty()) {
						continue;
					}
				%>

					<aui:select inlineField="<%= true %>" label="" name='<%= "subtype" + assetRendererFactory.getClassNameId() + "-classNameId" + index %>' wrapperCssClass='<%= ((classNameId != assetRendererFactory.getClassNameId()) ? "hide " : "") + "subtype-select" %>'>
						<c:if test="<%= !exist %>">
							<aui:option label="none" selected="<%= true %>" value="<%= classTypePK %>" />
						</c:if>

						<aui:option label="all-asset-subtypes" selected="<%= false %>" value="<%= AssetCategoryConstants.ALL_CLASS_TYPE_PK %>" />

						<%
						for (ClassType classType : classTypes) {
						%>

							<aui:option label="<%= HtmlUtil.escape(classType.getName()) %>" selected="<%= (classNameId == assetRendererFactory.getClassNameId()) && (classTypePK == classType.getClassTypeId()) %>" value="<%= classType.getClassTypeId() %>" />

						<%
						}
						%>

					</aui:select>

				<%
				}
				%>

				<c:if test="<%= !exist %>">
					<div class="has-warning">
						<div class="form-feedback-item">
							<liferay-ui:message key="the-asset-subtype-no-longer-exists" />
						</div>
					</div>
				</c:if>

				<aui:input inlineLabel="right" label="required" labelCssClass="simple-toggle-switch" name='<%= "required" + index %>' type="toggle-switch" value="<%= (vocabulary != null) ? vocabulary.isRequired(classNameId, classTypePK) : false %>" />
			</div>
		</div>

	<%
	}
	%>

	<aui:input name="indexes" type="hidden" value="<%= StringUtil.merge(indexes) %>" />
</liferay-frontend:fieldset>

<aui:script use="liferay-auto-fields">
	var extraFields = A.one('#<portlet:namespace />extraFields');

	var autoFields = new Liferay.AutoFields({
		contentBox: '#<portlet:namespace />extraFields',
		fieldIndexes: '<portlet:namespace />indexes',
		minimumRows: 1,
		namespace: '<portlet:namespace />',
		on: {
			clone: function (event) {
				event.row.all('.subtype-select').hide();
			},
		},
	}).render();

	extraFields.setData('autoFieldsInstance', autoFields);

	var form = A.one('#<portlet:namespace />fm');

	form.delegate(
		'change',
		(event) => {
			var target = event.currentTarget;

			var row = target.ancestor('.lfr-form-row');

			row.all('.subtype-select').hide();

			var select = row.one('[name*=subtype' + target.val() + ']');

			if (select) {
				select.ancestor('.subtype-select').show();
			}

			autoFields.save(form);
		},
		'.vocabulary-asset-type-select'
	);
</aui:script>