/*
 * Copyright 2021 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.core.request;

import java.util.Map;

import com.b2international.index.Hits;
import com.b2international.index.query.Expressions.ExpressionBuilder;
import com.b2international.index.query.SortBy.Builder;
import com.b2international.index.query.SortBy.Order;
import com.b2international.snowowl.core.Resources;
import com.b2international.snowowl.core.domain.RepositoryContext;
import com.b2international.snowowl.core.internal.ResourceDocument;

/**
 * @since 8.0
 */
final class ResourceSearchRequest extends BaseResourceSearchRequest<Resources> {

	private static final long serialVersionUID = 1L;
	
	private static final Map<String, Object> ORDER_BY_TYPE = Map.of("orderByType", 
			Map.of(
					"bundles", "1",
					"codesystems", "2",
					"valuesets", "3",
					"conceptmaps", "4"
					));

	/**
	 * @since 8.0
	 */
	enum OptionKey {

		/**
		 * Filter matches by their resource type.
		 */
		RESOURCE_TYPE,

		/**
		 * Filter matches by their tooling id property.
		 */
		TOOLING_ID,

		/**
		 * Filter matches by their currently associated working branch (exact match).
		 */
		BRANCH,

	}

	@Override
	protected void prepareAdditionalFilters(RepositoryContext context, ExpressionBuilder queryBuilder) {
		addFilter(queryBuilder, OptionKey.RESOURCE_TYPE, String.class, ResourceDocument.Expressions::resourceTypes);
		addFilter(queryBuilder, OptionKey.TOOLING_ID, String.class, ResourceDocument.Expressions::toolingIds);
		addFilter(queryBuilder, OptionKey.BRANCH, String.class, ResourceDocument.Expressions::branchPaths);
	}

	@Override
	protected Resources toCollectionResource(RepositoryContext context, Hits<ResourceDocument> hits) {
		return new ResourceConverter(context, expand(), locales()).convert(hits);
	}

	@Override
	protected Resources createEmptyResult(int limit) {
		return new Resources(limit, 0);
	}
	
	@Override
	protected void toQuerySortBy(RepositoryContext context, Builder sortBuilder, Sort sort) {
		if (sort instanceof SortField) {
			SortField sortField = (SortField) sort;
			if (ResourceSearchRequestBuilder.TYPE_SORT.equals(sortField.getField())) {
				sortBuilder.sortByScript(ResourceSearchRequestBuilder.TYPE_SORT, ORDER_BY_TYPE, sort.isAscending() ? Order.ASC : Order.DESC);
				return;
			}
		}
		super.toQuerySortBy(context, sortBuilder, sort);
	}
	
}

@@ -667,16 +667,42 @@
 quest;%0A%0A
+import java.util.HashMap;%0A
 import j
@@ -715,16 +715,16 @@
 il.Map;%0A
-
 %0Aimport 
@@ -929,24 +929,159 @@
 rtBy.Order;%0A
+import com.b2international.snowowl.core.ResourceTypeConverter;%0Aimport com.b2international.snowowl.core.ResourceTypeConverter.Registry;%0A
 import com.b
@@ -1251,16 +1251,55 @@
 ocument;
+%0Aimport com.google.common.collect.Maps;
 %0A%0A/**%0A *
@@ -1450,205 +1450,10 @@
 1L;%0A
+
 %09%0A
-%09private static final Map%3CString, Object%3E ORDER_BY_TYPE = Map.of(%22orderByType%22, %0A%09%09%09Map.of(%0A%09%09%09%09%09%22bundles%22, %221%22,%0A%09%09%09%09%09%22codesystems%22, %222%22,%0A%09%09%09%09%09%22valuesets%22, %223%22,%0A%09%09%09%09%09%22conceptmaps%22, %224%22%0A%09%09%09%09%09));%0A%0A
 %09/**
@@ -2736,16 +2736,303 @@
 d())) %7B%0A
+%09%09%09%09HashMap%3CString, String%3E orderMap = Maps.newHashMap();%0A%09%09%09%09Registry registry = context.service(ResourceTypeConverter.Registry.class);%0A%09%09%09%09registry.getResourceTypeConverters().values().forEach(converter -%3E %7B%0A%09%09%09%09%09orderMap.put(converter.getResourceType(), converter.getRank());%0A%09%09%09%09%7D);%0A
 %09%09%09%09sort
@@ -3096,21 +3096,40 @@
 RT, 
-ORDER_BY_TYPE
+Map.of(%22orderByType%22, orderMap) 
 , so

use the registry to create the typeOrder configuration map