package gov.va.med.cds.testharness.vhim400.builders;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;

public class HAPatientJournalEntryDietJournalBuilderFactory implements HAClinicalDomainBuilderFactoryInterface {
	
	
	private HADietJournalBuilder dietJournalBuilder = null;
	



	@Override
	public void createClinicalDomainData(Document clinicalData, int cnt,Element patientIdentifier) {
		
		buildDomainData(clinicalData, cnt, patientIdentifier);
	}
	
    
	public void createClinicalDomainData(Document clinicalData, String recordIdentifier, String recordStatus)
	{
	     buildDomainData(clinicalData, recordIdentifier, recordStatus);	
	}
	
	 private void buildDomainData( Document clinicalData, int count, Element patientIdentifier )
	    {
	        Namespace patientJournalingNS = DocumentHelper.createNamespace( "patientjournaling", "PatientJournaling" );
	        Namespace xsiNS = DocumentHelper.createNamespace( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );
	        
	        //determine if we need a record identifier for an Update template else it will be excluded for a create xml 
	        Element rootElement = clinicalData.getRootElement();
	        rootElement.add( patientJournalingNS );
	        rootElement.add( xsiNS );
	        
	        Element templateId = rootElement.element( "templateId" );
	        boolean updateRecordXml = templateId.getText().endsWith( "Update1" ) ? true : false;

	        Element patient = rootElement.element( "patient" );
	        Element patientJournalEntry = dietJournalBuilder.makeDietPatientJournalEntry(patientIdentifier, count, updateRecordXml);
	        patient.add( patientJournalEntry );
	    }
	 
	 private void buildDomainData( Document clinicalData, String recordIdentifier, String recordStatus )
	    {
	        Namespace patientJournalingNS = DocumentHelper.createNamespace( "patientjournaling", "PatientJournaling" );
	        Namespace xsiNS = DocumentHelper.createNamespace( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );
	        Element rootElement = clinicalData.getRootElement();
	        rootElement.add( patientJournalingNS );
	        rootElement.add( xsiNS );

	        Element patient = rootElement.element( "patient" );
	        Element patientJournalEntries = dietJournalBuilder.makeJournalEntry( "patientJournalEntries", recordIdentifier, recordStatus, "patientjournaling:patientEnteredDietJournalEntry" );
	        patient.add( patientJournalEntries );
	    }

	 
	 
     
	 public void setDietJournalBuilder(HADietJournalBuilder dietJournalBuilder) {
			this.dietJournalBuilder = dietJournalBuilder;
		}
	 
	 public void setBuilder( AbstractEntryPointBuilder builder )
	    {    
	     setDietJournalBuilder((HADietJournalBuilder)builder);
	    }
}
