package es.fhir.rest.core.model.util.transformer.helper;

import java.util.Optional;

import org.hl7.fhir.dstu3.model.BaseResource;
import org.hl7.fhir.dstu3.model.IdType;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.parser.IParser;
import ch.elexis.core.findings.IFinding;
import ch.elexis.core.findings.IFinding.RawContentFormat;

public class FindingsContentHelper {
	private static FhirContext context = FhirContext.forDstu3();

	private IParser jsonParser;

	public IParser getJsonParser() {
		if (jsonParser == null) {
			jsonParser = context.newJsonParser();
		}
		return jsonParser;
	}

	private IParser xmlParser;

	public IParser getXmlParser() {
		if (xmlParser == null) {
			xmlParser = context.newXmlParser();
		}
		return xmlParser;
	}

	private Logger getLogger() {
		return LoggerFactory.getLogger(FindingsContentHelper.class);
	}

	public Optional<IBaseResource> getResource(IFinding finding) throws DataFormatException {
		IBaseResource resource = null;

		RawContentFormat contentFormat = finding.getRawContentFormat();
		if (contentFormat == RawContentFormat.FHIR_JSON) {
			String jsonContent = finding.getRawContent();
			if (jsonContent != null && !jsonContent.isEmpty()) {
				if (finding.getRawContent() != null && !finding.getRawContent().isEmpty()) {
					resource = getJsonParser().parseResource(finding.getRawContent());
				}
			}
		} else if (contentFormat == RawContentFormat.FHIR_XML) {
			String xmlContent = finding.getRawContent();
			if (xmlContent != null && !xmlContent.isEmpty()) {
				if (finding.getRawContent() != null && !finding.getRawContent().isEmpty()) {
					resource = getXmlParser().parseResource(finding.getRawContent());
				}
			}
		} else {
			getLogger().error("Could not get resource because of unknown content format [" + contentFormat + "]");
		}
		return Optional.ofNullable(resource);
	}

	public void setResource(BaseResource resource, IFinding finding) throws DataFormatException {
		RawContentFormat contentFormat = finding.getRawContentFormat();
		if (contentFormat == RawContentFormat.FHIR_JSON) {
			String jsonContent = finding.getRawContent();
			if (jsonContent != null && !jsonContent.isEmpty()) {
				if (resource != null) {
					if (resource.getId() == null) {
						resource.setId(new IdType(resource.getClass().getSimpleName(), finding.getId()));
					}
					String resourceJson = getJsonParser().encodeResourceToString(resource);
					finding.setRawContent(resourceJson);
				}
			}
		} else if (contentFormat == RawContentFormat.FHIR_XML) {
			String xmlContent = finding.getRawContent();
			if (xmlContent != null && !xmlContent.isEmpty()) {
				if (resource != null) {
					if (resource.getIdElement() == null) {
						resource.setId(new IdType(resource.getClass().getSimpleName(), finding.getId()));
					}
					String resourceJson = getXmlParser().encodeResourceToString(resource);
					finding.setRawContent(resourceJson);
				}
			}
		} else {
			getLogger().error("Could not get resource because of unknown content format [" + contentFormat + "]");
		}
	}
}

@@ -1181,20 +1181,45 @@
  null;%0D%0A
+%09%09if (finding != null) %7B
 %0D%0A%09%09
+%09
 RawConte
@@ -1269,32 +1269,33 @@
 entFormat();%0D%0A%09%09
+%09
 if (contentForma
@@ -1322,32 +1322,33 @@
 t.FHIR_JSON) %7B%0D%0A
+%09
 %09%09%09String jsonCo
@@ -1376,32 +1376,33 @@
 wContent();%0D%0A%09%09%09
+%09
 if (jsonContent 
@@ -1431,32 +1431,33 @@
 t.isEmpty()) %7B%0D%0A
+%09
 %09%09%09%09if (finding.
@@ -1514,32 +1514,33 @@
 ).isEmpty()) %7B%0D%0A
+%09
 %09%09%09%09%09resource = 
@@ -1596,33 +1596,36 @@
 nt());%0D%0A%09%09%09%09
+%09
 %7D%0D%0A
+%09
 %09%09%09%7D%0D%0A
+%09
 %09%09%7D else if 
@@ -1667,32 +1667,33 @@
 FHIR_XML) %7B%0D%0A%09%09%09
+%09
 String xmlConten
@@ -1714,32 +1714,33 @@
 tRawContent();%0D%0A
+%09
 %09%09%09if (xmlConten
@@ -1770,32 +1770,33 @@
 t.isEmpty()) %7B%0D%0A
+%09
 %09%09%09%09if (finding.
@@ -1862,24 +1862,25 @@
 ()) %7B%0D%0A%09%09%09%09%09
+%09
 resource = g
@@ -1934,35 +1934,37 @@
 nt());%0D%0A%09%09%09%09
+%09
 %7D%0D%0A
+%09
 %09%09%09%7D%0D%0A%09%09
 %7D else %7B%0D%0A%09%09
@@ -1943,32 +1943,33 @@
 %09%09%09%09%7D%0D%0A%09%09%09%09%7D%0D%0A%09%09
+%09
 %7D else %7B%0D%0A%09%09%09get
@@ -1954,32 +1954,33 @@
 %7D%0D%0A%09%09%09%7D else %7B%0D%0A
+%09
 %09%09%09getLogger().e
@@ -2066,24 +2066,30 @@
 at + %22%5D%22);%0D%0A
+%09%09%09%7D%0D%0A
 %09%09%7D%0D%0A%09%09retur

check for existing finding when getting resource