/*******************************************************************************
 * Copyright (c) 2020 B2i Healthcare. All rights reserved.
 *******************************************************************************/
package com.b2international.snowowl.snomed.datastore.compare;

import com.b2international.commons.exceptions.BadRequestException;
import com.b2international.snowowl.core.compare.CompareSetResult;
import com.b2international.snowowl.core.compare.CompareSets;
import com.b2international.snowowl.snomed.common.SnomedRf2Headers;
import com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType;
import com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet;
import com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember;

/**
 * @since 7.8
 */
public final class ReferenceSetCompare implements CompareSets<SnomedReferenceSetMember, SnomedReferenceSet> {

	@Override
	public CompareSetResult<SnomedReferenceSetMember> doCompare(SnomedReferenceSet baseRf, SnomedReferenceSet compareRf) {
		if (!SnomedRefSetType.SIMPLE_MAP.equals(baseRf.getType()) || !SnomedRefSetType.SIMPLE_MAP.equals(compareRf.getType())) {
			throw new BadRequestException("Comparing Reference Sets requires the following Reference Sets (%s, %s) to be Simple Map Reference Sets!", baseRf.getId(), compareRf.getId());
		}
		if (!baseRf.getReferencedComponentType().equals(compareRf.getReferencedComponentType()) || !baseRf.getMapTargetComponentType().equals(compareRf.getMapTargetComponentType())) {
			throw new BadRequestException("The choosen Reference Set's referenced component types and map target types should match!", baseRf.getId(), compareRf.getId());
		}

		return compareDifferents(baseRf.getMembers().getItems(), compareRf.getMembers().getItems());
	}

	@Override
	public boolean isSourceEqual(SnomedReferenceSetMember memberA, SnomedReferenceSetMember memberB) {
		if (memberA.getReferencedComponent().getId().equals(memberB.getReferencedComponent().getId())) {
			return true;
		}
		return false;
	}

	@Override
	public boolean isTargetEqual(SnomedReferenceSetMember memberA, SnomedReferenceSetMember memberB) {
		if (memberA.getProperties().get(SnomedRf2Headers.FIELD_MAP_TARGET).equals(memberB.getProperties().get(SnomedRf2Headers.FIELD_MAP_TARGET))) {
			return true;
		}
		return false;
	}

}

@@ -282,75 +282,8 @@
 e;%0A%0A
-import com.b2international.commons.exceptions.BadRequestException;%0A
 impo
@@ -344,16 +344,16 @@
 Result;%0A
+
 import c
@@ -476,87 +476,8 @@
 rs;%0A
-import com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType;%0A
 impo
@@ -777,32 +777,32 @@
 %3E %7B%0A%0A%09@Override%0A
+
 %09public CompareS
@@ -909,657 +909,8 @@
 ) %7B%0A
-%09%09if (!SnomedRefSetType.SIMPLE_MAP.equals(baseRf.getType()) %7C%7C !SnomedRefSetType.SIMPLE_MAP.equals(compareRf.getType())) %7B%0A%09%09%09throw new BadRequestException(%22Comparing Reference Sets requires the following Reference Sets (%25s, %25s) to be Simple Map Reference Sets!%22, baseRf.getId(), compareRf.getId());%0A%09%09%7D%0A%09%09if (!baseRf.getReferencedComponentType().equals(compareRf.getReferencedComponentType()) %7C%7C !baseRf.getMapTargetComponentType().equals(compareRf.getMapTargetComponentType())) %7B%0A%09%09%09throw new BadRequestException(%22The choosen Reference Set's referenced component types and map target types should match!%22, baseRf.getId(), compareRf.getId());%0A%09%09%7D%0A%0A
 %09%09re

support to compare any type of sets