/*
 * Copyright 2011-2018 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.snomed.fhir.codesystems;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.b2international.commons.http.ExtendedLocale;
import com.b2international.snowowl.core.ApplicationContext;
import com.b2international.snowowl.core.api.IBranchPath;
import com.b2international.snowowl.eventbus.IEventBus;
import com.b2international.snowowl.fhir.core.codesystems.CommonConceptProperties;
import com.b2international.snowowl.fhir.core.codesystems.ConceptPropertyType;
import com.b2international.snowowl.fhir.core.model.dt.Code;
import com.b2international.snowowl.fhir.core.model.dt.Uri;
import com.b2international.snowowl.snomed.SnomedConstants.Concepts;
import com.b2international.snowowl.snomed.core.lang.LanguageSetting;
import com.b2international.snowowl.snomed.datastore.SnomedDatastoreActivator;
import com.b2international.snowowl.snomed.datastore.request.SnomedRequests;
import com.google.common.collect.Sets;

/**
 * Experimental.
 * If a keeper.
 * @since 6.3
 */
public class AllSnomedConceptProperties {
	
	private static Collection<Property> properties = Sets.newHashSet();

	static {

		//Common properties
		Arrays.stream(CommonConceptProperties.values()).map(p -> {
			return new AllSnomedConceptProperties.Property(p.getCode(), p.getUri(), p.getDisplayName(), p.getConceptPropertyType());
		}).forEach(properties::add);
		
		//Core SNOMED CT properties
		Arrays.stream(CoreSnomedConceptProperties.values()).map(p -> {
			return new AllSnomedConceptProperties.Property(p.getCode(), p.getUri(), p.getCodeValue(), p.getConceptPropertyType());
		}).forEach(properties::add);

		//what should be the locale here? Likely we need to add the config locale as well
		final List<ExtendedLocale> locales = ApplicationContext.getServiceForClass(LanguageSetting.class).getLanguagePreference();
		locales.add(ExtendedLocale.valueOf("en-x-" + Concepts.REFSET_LANGUAGE_TYPE_US));

		//add the dynamic relationship types as properties
		Set<Property> dynamicProperties = SnomedRequests.prepareSearchConcept()
			.all()
			.filterByActive(true)
			.filterByAncestor("246061005") //Attribute
			.setExpand("pt()")
			.setLocales(locales)
			.build(SnomedDatastoreActivator.REPOSITORY_UUID, IBranchPath.MAIN_BRANCH)
			.execute(ApplicationContext.getServiceForClass(IEventBus.class))
			.then(sc -> sc.stream()
					.map(c -> {
							return new AllSnomedConceptProperties.Property(new Code(c.getPt().getTerm()), getUri(c.getId()), c.getId(), ConceptPropertyType.CODE);
						})
					.collect(Collectors.toSet())
				)
			.getSync();
		
		properties.addAll(dynamicProperties);
		
	}
	
	public static Uri getUri(String id) {
		return new Uri(CoreSnomedConceptProperties.CODE_SYSTEM_URI+"/id" + id);
	}
	
	static class Property {

		private Code code;
		private Uri uri;
		private String description;
		private ConceptPropertyType type;
		
		public Property(Code code, Uri uri, String description, ConceptPropertyType type) {
			this.code = code;
			this.uri = uri;
			this.description = description;
			this.type = type;
		}
	}
	
	
	
}

@@ -2673,16 +2673,18 @@
 perties%0A
+//
 %09%09Set%3CPr
@@ -2749,16 +2749,18 @@
 ncept()%0A
+//
 %09%09%09.all(
@@ -2761,16 +2761,18 @@
 %09.all()%0A
+//
 %09%09%09.filt
@@ -2788,16 +2788,18 @@
 e(true)%0A
+//
 %09%09%09.filt
@@ -2836,16 +2836,18 @@
 tribute%0A
+//
 %09%09%09.setE
@@ -2860,16 +2860,18 @@
 %22pt()%22)%0A
+//
 %09%09%09.setL
@@ -2886,16 +2886,18 @@
 ocales)%0A
+//
 %09%09%09.buil
@@ -2965,16 +2965,18 @@
 BRANCH)%0A
+//
 %09%09%09.exec
@@ -3035,16 +3035,18 @@
 class))%0A
+//
 %09%09%09.then
@@ -3064,16 +3064,18 @@
 tream()%0A
+//
 %09%09%09%09%09.ma
@@ -3083,16 +3083,18 @@
 (c -%3E %7B%0A
+//
 %09%09%09%09%09%09%09r
@@ -3227,16 +3227,18 @@
 .CODE);%0A
+//
 %09%09%09%09%09%09%7D)
@@ -3238,16 +3238,18 @@
 %09%09%09%09%09%7D)%0A
+//
 %09%09%09%09%09.co
@@ -3278,14 +3278,18 @@
 ())%0A
+//
 %09%09%09%09)%0A
+//
 %09%09%09.
@@ -3299,19 +3299,18 @@
 Sync();%0A
-%09%09%0A
+//
 %09%09proper

remove fetching of all SNOMED CT attribute types