package ch.elexis.core.data.lab;

import org.apache.commons.lang.StringUtils;

import ch.elexis.core.constants.Preferences;
import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.model.ILabItem;
import ch.elexis.core.types.Gender;
import ch.elexis.core.types.LabItemTyp;
import ch.elexis.core.types.PathologicDescription;
import ch.elexis.core.types.PathologicDescription.Description;
import ch.elexis.data.LabResult;

public class LabResultEvaluator {
	
	private LabResultEvaluationResult evaluateTextualResult(LabResult labResult,
		boolean isAbsoluteItem){
		
		String lcResult = labResult.getResult().trim();
		Object[] ref = getReferenceValueForLabResult(labResult);
		String refValue = (String) ref[0];
		Description description =
			(isAbsoluteItem) ? Description.PATHO_ABSOLUT : (Description) ref[1];
		
		if (lcResult.equals(refValue)) {
			return new LabResultEvaluationResult(true, false,
				new PathologicDescription(description, lcResult));
		}
		
		if (isAbsoluteItem) {
			if (lcResult.toLowerCase().startsWith("pos")
				|| lcResult.toLowerCase().startsWith("+")) {
				return new LabResultEvaluationResult(true, true,
					new PathologicDescription(description, lcResult));
			}
		}
		
		if (CoreHub.globalCfg.get(
			Preferences.LABSETTINGS_CFG_EVAL_PREFIX_TYPE_ABSOLUT
				+ Preferences.LABSETTINGS_CFG_EVAL_REFVAL_NON_EQUAL_RESVAL_MEANS_PATHOLOGIC,
			false)) {
			
			if (!lcResult.equalsIgnoreCase(refValue)) {
				return new LabResultEvaluationResult(true, true,
					new PathologicDescription(description, lcResult));
			}
		}
		
		return new LabResultEvaluationResult(true, false,
			new PathologicDescription(Description.UNKNOWN, lcResult));
	}
	
	private Object[] getReferenceValueForLabResult(LabResult labResult){
		Gender gender = labResult.getPatient().getGender();
		
		Description description = Description.PATHO_REF;
		String refValue = "";
		if (Gender.MALE == gender) {
			refValue = labResult.getRefMale();
			if (StringUtils.isEmpty(refValue)) {
				description = Description.PATHO_REF_ITEM;
				refValue = labResult.getItem().getReferenceMale();
			}
		} else {
			refValue = labResult.getRefFemale();
			if (StringUtils.isEmpty(refValue)) {
				description = Description.PATHO_REF_ITEM;
				refValue = labResult.getItem().getReferenceFemale();
			}
		}
		return new Object[] {
			refValue, description
		};
	}
	
	public LabResultEvaluationResult evaluate(LabResult labResult){
		ILabItem item = labResult.getItem();
		if (LabItemTyp.ABSOLUTE == item.getTyp()) {
			return evaluateTextualResult(labResult, true);
		} else if (LabItemTyp.TEXT == item.getTyp()) {
			return evaluateTextualResult(labResult, false);
		} else if (LabItemTyp.DOCUMENT == item.getTyp()) {
			/** document is never pathologic **/
			return new LabResultEvaluationResult(true, false, null);
		}
		return new LabResultEvaluationResult(false);
	}
	
}

@@ -541,16 +541,22 @@
 sult,%0A%09%09
+final 
 boolean 
@@ -1222,90 +1222,209 @@
 %09%0A%09%09
-if (CoreHub.globalCfg.get(%0A%09%09%09Preferences.LABSETTINGS_CFG_EVAL_PREFIX_TYPE_ABSOLUT
+String SELECTED_BASE =%0A%09%09%09(isAbsoluteItem) ? Preferences.LABSETTINGS_CFG_EVAL_PREFIX_TYPE_ABSOLUT%0A%09%09%09%09%09: Preferences.LABSETTINGS_CFG_EVAL_PREFIX_TYPE_TEXT;%0A%09%09if (CoreHub.globalCfg.get(%0A%09%09%09SELECTED_BASE
 %0A%09%09%09
@@ -1515,18 +1515,16 @@
 se)) %7B%0A%09
-%09%09
 %0A%09%09%09if (

Fix rule selection base