package net.ihe.xcpd.resp.process;

import java.util.List;
import java.util.Vector;

import org.openhealthexchange.openpixpdq.ihe.IPdSupplierAdapter;
import org.openhealthexchange.openpixpdq.ihe.configuration.ConfigurationLoader;
import org.openhealthexchange.openpixpdq.ihe.configuration.IheActorDescription;
import org.openhealthexchange.openpixpdq.ihe.configuration.ConfigurationLoader.ActorDescription;

import com.misyshealthcare.connect.net.IConnectionDescription;


/**
 * @author Abderrazek Boufahja > INRIA Rennes IHE development Project
 *
 */
public class ConfigPIXPDQ {
	
	public ConfigPIXPDQ(){
	}
	
	public List<IheActorDescription> getListIheActorDescription(){
		return this.getListIheActorDescription("IheActors.xml");
	}
	
	@SuppressWarnings("unchecked")
	public List<IheActorDescription> getListIheActorDescription(String pathActors){
		try {
			ConfigurationLoader.getInstance().resetConfiguration(null, null);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		try {
			ConfigurationLoader.getInstance().loadConfiguration(pathActors, false);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		List<IheActorDescription> l = (Vector) ConfigurationLoader.getInstance().getActorDescriptions();
		return l;
	}
	
	public ActorDescription getActorDescriptionSupplier(List<IheActorDescription> list_IAD){
		ConfigurationLoader.ActorDescription actor = null;
		for (IheActorDescription actorDescription : list_IAD) {
			if (actorDescription.getActorType().equalsIgnoreCase("PdSupplier")) {
				actor = (ConfigurationLoader.ActorDescription)actorDescription;
				System.out.println(actor.getDescription());
				break;
			}
		}
		return actor;
	}
	
	public IConnectionDescription getConnection(ConfigurationLoader.ActorDescription actor){
		return actor.getConnection();
	}
	
	public IConnectionDescription getConnection(){
		List<IheActorDescription> list_IAD = this.getListIheActorDescription();
		ConfigurationLoader.ActorDescription actor = this.getActorDescriptionSupplier(list_IAD);
		return this.getConnection(actor);
	}
	
	@SuppressWarnings("unchecked")
	public IPdSupplierAdapter getIPdSupplierAdapter(IConnectionDescription connection)
							throws ClassNotFoundException, InstantiationException, IllegalAccessException{
		String pdsuppAdapterClass = connection.getProperty("PdSupplierAdapter");
		Class c = null;
		c = Class.forName(pdsuppAdapterClass);
		IPdSupplierAdapter pdsupp = null;
		pdsupp = (IPdSupplierAdapter) c.newInstance();
		return pdsupp;
	}
	
}
