/*
 * Copyright 2020 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.Collection;

import com.b2international.commons.options.Options;
import com.b2international.snowowl.core.domain.BranchContext;
import com.b2international.snowowl.core.domain.Concept;
import com.b2international.snowowl.core.domain.Concepts;
import com.b2international.snowowl.core.domain.IComponent;
import com.b2international.snowowl.core.uri.CodeSystemURI;

/**
 * @since 7.5
 */
public interface ConceptSearchRequestEvaluator {

	public enum OptionKey {

		/**
		 * Explicit ID filter to return all concepts that have any of the given IDs.
		 */
		ID,
		
		/**
		 * Match concepts that have the specified active status. Accepts a boolean <code>true</code> or <code>false</code> value.
		 */
		ACTIVE,

		/**
		 * A term filter that matches concepts having a term match. The exact semantics of how a term match works depends on the given code system,
		 * but usually it supports exact, partial word and prefix matches.
		 */
		TERM,

		/**
		 * A term filter that matches concepts having an exact term match regardless of case.
		 */
		TERM_EXACT,

		/**
		 * The minimum number of terms to match.
		 */
		MIN_TERM_MATCH,
		
		/**
		 * One or more query expressions (defined in the target code system's query language) to include matches.
		 */
		QUERY,

		/**
		 * One or more query expressions (defined in the target code system's query language) to exclude matches from the results.
		 */
		MUST_NOT_QUERY, 
		
		/**
		 * Language locales (tag, Accept-Language header, etc.) to use in order of preference when determining the display label or term for a match.
		 */
		LOCALES,
		
		/**
		 * Search matches after the specified sort key.
		 */
		AFTER,
		
		/**
		 * Number of matches to return.
		 */
		LIMIT, 
	}

	/**
	 * Evaluate the given search options on the given context and return generic {@link Concept} instances back in a {@link Concepts} pageable
	 * resource.
	 * 
	 * @param uri - the code system uri where the search is being evaluated
	 * @param context - the context prepared for the search
	 * @param search - the search filters and options to apply to the code system specific search
	 * @return
	 */
	Concepts evaluate(CodeSystemURI uri, BranchContext context, Options search);
	
	default Concept toConcept(CodeSystemURI codeSystem, IComponent concept, String iconId, String term) {
		Concept result = new Concept(codeSystem.toString(), concept.getTerminologyComponentId());
		result.setId(concept.getId());
		result.setReleased(concept.isReleased());
		result.setIconId(iconId);
		result.setTerm(term);
		return result;
	}
	
	/**
	 * No-op request evaluator that returns zero results
	 * @since 7.5
	 */
	ConceptSearchRequestEvaluator NOOP = new ConceptSearchRequestEvaluator() {
		
		@Override
		public Concepts evaluate(CodeSystemURI uri, BranchContext context, Options search) {
			return new Concepts(search.get(OptionKey.LIMIT, Integer.class), 0);
		}
	};
	
	/**
	 * Support value set clause evaluation
	 * @since 7.7
	 */
	public static String getId(Collection <String> query) {
		return query.toString().replaceAll("\\[|\\]|\\|", "").split(" ", 1).toString();
	}
	
}

@@ -1041,16 +1041,56 @@
 stemURI;
+%0Aimport com.google.common.collect.Lists;
 %0A%0A/**%0A *
@@ -3698,20 +3698,33 @@
 tic 
+Collection%3C
 String
+%3E
  getId
+s
 (Col
@@ -3730,17 +3730,16 @@
 llection
- 
 %3CString%3E
@@ -3748,37 +3748,112 @@
 uery
+s
 ) %7B%0A%09%09
-return query.toString()
+Collection%3CString%3E ids = Lists.newArrayList();%0A%09%09querys.forEach(query -%3E %7B%0A%09%09%09String%5B%5D id = query
 .rep
@@ -3867,16 +3867,8 @@
 (%22%5C%5C
-%5B%7C%5C%5C%5D%7C%5C%5C
 %7C%22, 
@@ -3886,21 +3886,49 @@
  %22, 
-1).toString()
+2);%0A%09%09%09ids.add(id%5B0%5D);%0A%09%09%7D);%0A%09%09return ids
 ;%0A%09%7D

fix getIds()