package net.ihe.xcpd.resp.process;

import java.util.List;

import org.openhealthexchange.openpixpdq.ihe.IPdSupplierAdapter;
import org.openhealthexchange.openpixpdq.ihe.configuration.IheActorDescription;
import org.openhealthexchange.openpixpdq.ihe.configuration.IheConfigurationException;
import org.openhealthexchange.openpixpdq.ihe.configuration.ConfigurationLoader.ActorDescription;
import org.openhealthexchange.openpixpdq.ihe.impl_v2.PdQueryHandler;
import org.openhealthexchange.openpixpdq.ihe.impl_v2.PdSupplier;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.app.ApplicationException;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
import ca.uhn.hl7v2.parser.PipeParser;

import com.misyshealthcare.connect.net.IConnectionDescription;

/**
 * @author Abderrazek Boufahja > INRIA Rennes IHE development Project
 *
 */
public class MessageResponder extends ConfigPIXPDQ{
	
	public MessageResponder(){
	}
	
	public Message getResponseForMessage(Message inMsg, IConnectionDescription connection, IPdSupplierAdapter pdsupp) 
												throws IheConfigurationException, ApplicationException, HL7Exception{
		PdSupplier pd = new PdSupplier(connection, null);
		PdQueryHandler pqh = new PdQueryHandler(pd);
		pqh.setPdqAdapter(pdsupp);
		Message resMsg = pqh.processMessage(inMsg);
		return resMsg;
	}
	
	public Message getResponseForMessage(String msgString, IConnectionDescription connection, IPdSupplierAdapter pdsupp) 
							throws EncodingNotSupportedException, HL7Exception, IheConfigurationException, ApplicationException{
		Message inMsg = this.getMessageFromString(msgString);
		return this.getResponseForMessage(inMsg, connection, pdsupp);
	}
	
	public Message getResponseForMessage(Message inMsg) 
			throws ClassNotFoundException, InstantiationException, IllegalAccessException, IheConfigurationException, ApplicationException, HL7Exception{
		List<IheActorDescription> list_IAD = this.getListIheActorDescription();
		ActorDescription actor = this.getActorDescriptionSupplier(list_IAD);
		IConnectionDescription connection = this.getConnection(actor);
		IPdSupplierAdapter pdsupp = this.getIPdSupplierAdapter(connection);
		return this.getResponseForMessage(inMsg, connection, pdsupp);
	}
	
	public Message  getResponseForMessage(String msgString) 
			throws EncodingNotSupportedException, HL7Exception, ClassNotFoundException, InstantiationException, IllegalAccessException, IheConfigurationException, ApplicationException{
		Message inMsg = this.getMessageFromString(msgString);
		return this.getResponseForMessage(inMsg); 
	}
	
	public String getResponseStringForMessage(String msgString){
	    if (msgString != null){
	        try {
	            Message resMsg = this.getResponseForMessage(msgString);
	            String resString = this.getEncodedMessage(resMsg);
	            return resString;
	        } catch (Exception e) {
	            e.printStackTrace();
	        } 
	    }
	    return null;
	}
	
	public Message getMessageFromString(String msgString) throws EncodingNotSupportedException, HL7Exception{
		PipeParser pipeParser = new PipeParser();
		Message message = pipeParser.parse(msgString);
		return message;
	}
	
	public String getEncodedMessage(Message inMsg) throws HL7Exception{
		PipeParser pipeParser = new PipeParser();
		return pipeParser.encode(inMsg);
	}
	
	
}
