/*
 * 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.test.commons.snomed;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

import org.eclipse.emf.common.util.WrappedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.b2international.commons.ReflectionUtils;
import com.b2international.commons.options.MetadataImpl;
import com.b2international.index.es.client.EsIndexStatus;
import com.b2international.index.revision.RevisionBranch.BranchState;
import com.b2international.snowowl.core.ServiceProvider;
import com.b2international.snowowl.core.api.IBranchPath;
import com.b2international.snowowl.core.branch.Branch;
import com.b2international.snowowl.core.branch.BranchPathUtils;
import com.b2international.snowowl.core.config.SnowOwlConfiguration;
import com.b2international.snowowl.core.domain.BranchContext;
import com.b2international.snowowl.core.domain.DelegatingContext;
import com.b2international.snowowl.core.domain.RepositoryContext;
import com.b2international.snowowl.core.domain.RepositoryContextProvider;
import com.b2international.snowowl.core.events.DelegatingRequest;
import com.b2international.snowowl.core.events.Request;
import com.b2international.snowowl.core.request.BranchRequest;
import com.b2international.snowowl.core.request.HealthCheckingRequest;
import com.b2international.snowowl.core.request.RepositoryRequest;
import com.b2international.snowowl.eventbus.EventBusUtil;
import com.b2international.snowowl.eventbus.IEventBus;

/**
 * @since 6.4
 */
public final class TestBranchContext extends DelegatingContext implements BranchContext, RepositoryContextProvider {

	private final String repositoryId;
	private final Branch branch;

	private TestBranchContext(String repositoryId, Branch branch) {
		super(ServiceProvider.EMPTY);
		this.repositoryId = repositoryId;
		this.branch = branch;
	}
	
	@Override
	public RepositoryContext getContext(String repositoryId) {
		return this;
	}
	
	@Override
	public Logger log() {
		return LoggerFactory.getLogger(repositoryId);
	}
	
	@Override
	public Branch branch() {
		return branch;
	}
	
	@Override
	public String path() {
		return branch.path();
	}
	
	@Override
	public SnowOwlConfiguration config() {
		return service(SnowOwlConfiguration.class);
	}
	
	@Override
	public String id() {
		return repositoryId;
	}
	
	@Override
	public Health health() {
		return Health.GREEN;
	}
	
	@Override
	public List<EsIndexStatus> indices() {
		return Collections.emptyList();
	}
	
	public static TestBranchContext.Builder on(String branch) {
		return new TestBranchContext.Builder(branch);
	}
	
	public static class Builder {
		
		private TestBranchContext context;
		
		Builder(String branch) {
			final String repositoryId = UUID.randomUUID().toString();
			IBranchPath branchPath = BranchPathUtils.createPath(branch);
			final Branch mockBranch = new Branch(1L, branchPath.lastSegment(), branchPath.getParentPath(), 0L, 1L, false, new MetadataImpl(), BranchState.FORWARD, branchPath, Collections.emptyList());
			context = new TestBranchContext(repositoryId, mockBranch);
			final IEventBus bus = EventBusUtil.getDirectBus(repositoryId);
			bus.registerHandler(Request.ADDRESS, message -> {
				try {
					final RepositoryRequest<?> repoReq = message.body(RepositoryRequest.class);
					final HealthCheckingRequest<?> healthReq = ReflectionUtils.getField(DelegatingRequest.class, repoReq, "next");
					final BranchRequest<?> branchReq = ReflectionUtils.getField(DelegatingRequest.class, healthReq, "next");
					final Request<BranchContext, ?> innerReq = ReflectionUtils.getField(DelegatingRequest.class, branchReq, "next");
					message.reply(innerReq.execute(context));
				} catch (WrappedException e1) {
					message.fail(e1.getCause());
				} catch (Throwable e2) {
					message.fail(e2);
				}
			});
			with(IEventBus.class, bus);
			with(RepositoryContextProvider.class, context);
		}
		
		public <T> Builder with(Class<T> type, T object) {
			context.bind(type, object);
			return this;
		}
		
		public BranchContext build() {
			return context;
		}
		
	}
	
}
@@ -1911,89 +1911,26 @@
 est.
-HealthCheckingRequest;%0Aimport com.b2international.snowowl.core.request.Repository
+CodeSystemResource
 Requ
@@ -3776,32 +3776,21 @@
 l Re
-positoryRequest%3C?%3E repoR
+quest%3C?, ?%3E r
 eq =
@@ -3803,26 +3803,16 @@
 ge.body(
-Repository
 Request.
@@ -3834,22 +3834,14 @@
 nal 
-HealthChecking
+Branch
 Requ
@@ -3847,21 +3847,21 @@
 uest%3C?%3E 
-healt
+branc
 hReq = R
@@ -3865,181 +3865,187 @@
 = Re
-flectionUtils.getField(DelegatingRequest.class, repoReq, %22next%22);%0A%09%09%09%09%09final BranchRequest%3C?%3E branchReq = ReflectionUtils.getField(DelegatingRequest.class, healthReq, %22next%22
+quest.getNestedRequest(req, BranchRequest.class);%0A%09%09%09%09%09final CodeSystemResourceRequest%3C?%3E codeSystemResourceRequest = Request.getNestedRequest(req, CodeSystemResourceRequest.class
 );%0A%09
@@ -4090,16 +4090,36 @@
 nerReq =
+ branchReq != null ?
  Reflect
@@ -4171,16 +4171,103 @@
 anchReq,
+ %22next%22) : ReflectionUtils.getField(DelegatingRequest.class, codeSystemResourceRequest,
  %22next%22)

support generic CodeSystemResourceRequest in TestBranchContext