package ch.elexis.core.importer.div.tasks.internal;

import java.util.List;

import org.slf4j.Logger;

import ch.elexis.core.importer.div.importers.IContactResolver;
import ch.elexis.core.importer.div.importers.ILabContactResolver;
import ch.elexis.core.importer.div.importers.ILabImportUtil;
import ch.elexis.core.model.ILaboratory;
import ch.elexis.core.model.ModelPackage;
import ch.elexis.core.model.builder.IContactBuilder;
import ch.elexis.core.services.IModelService;
import ch.elexis.core.services.IQuery;
import ch.elexis.core.services.IQuery.COMPARATOR;

public class HL7ImporterLabContactResolver implements ILabContactResolver {
	
	private Logger logger;
	private IModelService coreModelService;
	private ILabImportUtil labImportUtil;
	private boolean bCreateLaboratoryIfNotExists;
	
	public HL7ImporterLabContactResolver(IModelService coreModelService,
		ILabImportUtil labImportUtil, Logger logger, boolean bCreateLaboratoryIfNotExists){
		this.coreModelService = coreModelService;
		this.labImportUtil = labImportUtil;
		this.logger = logger;
		this.bCreateLaboratoryIfNotExists = bCreateLaboratoryIfNotExists;
	}
	
	@Override
	public ILaboratory getLabContact(String identifier, String sendingFacility){
		return labImportUtil.getLinkLabor(sendingFacility, new MyContactResolver(identifier));
	}
	
	private class MyContactResolver implements IContactResolver<ILaboratory> {
		
		private final String identifier;
		
		public MyContactResolver(String identifier){
			this.identifier = identifier;
		}
		
		@Override
		public ILaboratory getContact(String message){
			ILaboratory laboratory;
			IQuery<ILaboratory> query = coreModelService.getQuery(ILaboratory.class);
			query.and(ModelPackage.Literals.ICONTACT__CODE, COMPARATOR.LIKE,
				"%" + identifier + "%");
			query.or(ModelPackage.Literals.ICONTACT__DESCRIPTION1, COMPARATOR.LIKE,
				"%" + identifier + "%");
			List<ILaboratory> results = query.execute();
			if (results.isEmpty()) {
				if (!bCreateLaboratoryIfNotExists) {
					logger.warn(
						"Found no Labor for identifier [{}]. Automatic creation deactivated.",
						identifier);
					return null;
				}
				logger.info("Found no Labor for identifier [{}]. Created new Labor contact.",
					identifier);
				laboratory = new IContactBuilder.LaboratoryBuilder(coreModelService, identifier)
					.buildAndSave();
			} else {
				laboratory = results.get(0);
				if (results.size() > 1) {
					logger.warn(
						"Found more than one Labor for identifier [{}]. This can cause problems when importing results.",
						identifier);
				}
			}
			
			return laboratory;
		}
		
	}
	
}

@@ -1572,16 +1572,110 @@
 ssage)%7B%0A
+%09%09%09if (identifier == null) %7B%0A%09%09%09%09logger.warn(%22identifier is null%22);%0A%09%09%09%09return null;%0A%09%09%09%7D%0A%09%09%09%0A
 %09%09%09ILabo
@@ -1687,24 +1687,31 @@
 y laboratory
+ = null
 ;%0A%09%09%09IQuery%3C
@@ -2460,205 +2460,392 @@
 %09%09%09%09
-laboratory = results.get(0);%0A%09%09%09%09if (results.size() %3E 1) %7B%0A%09%09%09%09%09logger.warn(%0A%09%09%09%09%09%09%22Found more than one Labor for identifier %5B%7B%7D%5D. This can cause problems when importing results.%22,%0A%09%09%09%09%09%09identifier
+if (results.size() == 1) %7B%0A%09%09%09%09%09return results.get(0);%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09for (ILaboratory lab : results) %7B%0A%09%09%09%09%09%09if (lab.getCode().equalsIgnoreCase(identifier.trim())) %7B%0A%09%09%09%09%09%09%09return lab;%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%09laboratory = results.get(0);%0A%09%09%09%09%09logger.warn(%0A%09%09%09%09%09%09%22Found more than one Labor for identifier %5B%7B%7D%5D but no exact match, returning %5B%7B%7D%5D%22,%0A%09%09%09%09%09%09identifier, laboratory.getCode()
 );%0A%09

Handle multi lab resolution