/*
 * Copyright 2011-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.terminologyregistry.core.request;

import java.util.List;
import java.util.stream.Collectors;

import com.b2international.commons.exceptions.BadRequestException;
import com.b2international.snowowl.core.Repositories;
import com.b2international.snowowl.core.RepositoryInfo;
import com.b2international.snowowl.core.ServiceProvider;
import com.b2international.snowowl.datastore.CodeSystemEntry;
import com.b2international.snowowl.datastore.request.RepositoryRequest;
import com.b2international.snowowl.datastore.request.RepositoryRequests;
import com.b2international.snowowl.datastore.request.version.CodeSystemVersionCreateRequestBuilder;

/**
 * @since 4.7
 */
public class CodeSystemRequests {

	private CodeSystemRequests() {}
	
	public static CodeSystemCreateRequestBuilder prepareNewCodeSystem() {
		return new CodeSystemCreateRequestBuilder();
	}

	public static CodeSystemUpdateRequestBuilder prepareUpdateCodeSystem(final String uniqueId) {
		return new CodeSystemUpdateRequestBuilder(uniqueId);
	}

	public static CodeSystemGetRequestBuilder prepareGetCodeSystem(final String uniqeId) {
		return new CodeSystemGetRequestBuilder(uniqeId);
	}

	public static CodeSystemSearchRequestBuilder prepareSearchCodeSystem() {
		return new CodeSystemSearchRequestBuilder();
	}

	public static CodeSystemVersionSearchRequestBuilder prepareSearchCodeSystemVersion() {
		return new CodeSystemVersionSearchRequestBuilder();
	}
	
	public static CodeSystemVersionCreateRequestBuilder prepareNewCodeSystemVersion() {
		return new CodeSystemVersionCreateRequestBuilder();
	}

	/**
	 * Returns all {@link CodeSystemEntry}s from all repositories.
	 * @param context
	 * @return
	 */
	public static List<CodeSystemEntry> getAllCodeSystems(ServiceProvider context) {
		final Repositories repositories = RepositoryRequests.prepareSearch()
			.all()
			.build()
			.execute(context);
		
		return repositories.getItems()
			.stream()
			.filter( repository -> RepositoryInfo.Health.GREEN == repository.health() || RepositoryInfo.Health.YELLOW == repository.health() )
			.flatMap(repository -> {
				return new RepositoryRequest<>(repository.id(), CodeSystemRequests.prepareSearchCodeSystem()
						.all()
						.build()).execute(context).stream();
			})
			.collect(Collectors.toList());
	}

	public static CodeSystemEntry getCodeSystem(ServiceProvider context, String codeSystem) {
		final Repositories repositories = RepositoryRequests.prepareSearch()
				.all()
				.build()
				.execute(context);
			
		return repositories.getItems()
			.stream()
			.filter( repository -> RepositoryInfo.Health.GREEN == repository.health() || RepositoryInfo.Health.YELLOW == repository.health() )
			.flatMap(repository -> {
				return new RepositoryRequest<>(repository.id(), CodeSystemRequests.prepareSearchCodeSystem()
						.one()
						.filterById(codeSystem)
						.build()).execute(context).stream();
			})
			.findFirst()
			.orElseThrow(() -> new BadRequestException("CodeSystem '%s' cannot be found", codeSystem));
	}
	
}

@@ -877,64 +877,8 @@
 es;%0A
-import com.b2international.snowowl.core.RepositoryInfo;%0A
 impo
@@ -996,80 +996,8 @@
 ry;%0A
-import com.b2international.snowowl.datastore.request.RepositoryRequest;%0A
 impo
@@ -2452,216 +2452,41 @@
 %09%09.f
-ilter( repository -%3E RepositoryInfo.Health.GREEN == repository.health() %7C%7C RepositoryInfo.Health.YELLOW == repository.health() )%0A%09%09%09.flatMap(repository -%3E %7B%0A%09%09%09%09return new RepositoryRequest%3C%3E(repository.id(),
+latMap(repository -%3E %7B%0A%09%09%09%09return
  Cod
@@ -2545,34 +2545,75 @@
 ()%0A%09%09%09%09%09%09.build(
-))
+repository.id())%0A%09%09%09%09%09%09.getRequest()%0A%09%09%09%09%09%09
 .execute(context
@@ -2605,32 +2605,39 @@
 execute(context)
+%0A%09%09%09%09%09%09
 .stream();%0A%09%09%09%7D)
@@ -2943,216 +2943,41 @@
 %09%09.f
-ilter( repository -%3E RepositoryInfo.Health.GREEN == repository.health() %7C%7C RepositoryInfo.Health.YELLOW == repository.health() )%0A%09%09%09.flatMap(repository -%3E %7B%0A%09%09%09%09return new RepositoryRequest%3C%3E(repository.id(),
+latMap(repository -%3E %7B%0A%09%09%09%09return
  Cod
@@ -3074,18 +3074,59 @@
 %09.build(
-))
+repository.id())%0A%09%09%09%09%09%09.getRequest()%0A%09%09%09%09%09%09
 .execute
@@ -3134,16 +3134,23 @@
 context)
+%0A%09%09%09%09%09%09
 .stream(

use health checking request properly in new CodeSystemRequests...