/**
 * 
 */
package com.ejie.ab61.adaptadores.service;

import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.PortInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.ejie.ab61.clientesWS.X43ExpedienteElectronico.AExtensionPoint;
import com.ejie.ab61.clientesWS.X43ExpedienteElectronico.TBoolean;
import com.ejie.ab61.clientesWS.X43ExpedienteElectronico.TFolderDocumentList;
import com.ejie.ab61.clientesWS.X43ExpedienteElectronico.TFolderList;
import com.ejie.ab61.clientesWS.X43ExpedienteElectronico.TFolderQuery;
import com.ejie.ab61.clientesWS.X43ExpedienteElectronico.TGetTelActionDocumentList;
import com.ejie.ab61.clientesWS.X43ExpedienteElectronico.X43REE;
import com.ejie.ab61.clientesWS.X43ExpedienteElectronico.X43REEPortType;
import com.ejie.ab61.clientesWS.X43Toolkit.Exception_Exception;
import com.ejie.ab61.comun.Constantes;
import com.ejie.ab61.comun.utilidades.config.Config;

/**
 *  
 * 
 */
@Service(value = "X43ExpedienteElectronicoServiceImpl")
public class X43ExpedienteElectronicoServiceImpl implements X43ExpedienteElectronicoService {

	private static final Logger logger = LoggerFactory.getLogger(X43ExpedienteElectronicoServiceImpl.class);
	private X43REE service;

	private static final QName qname = new QName("com/ejie/x43s/v2/ree/ws", "X43REE");

	/**
	 * 
	 * @return X43REEPortType
	 */
	private X43REEPortType getX43ExpedienteElectronicoServicePort() {
		return this.getX43ExpedienteElectronicoServicePort(true);
	}

	/**
	 * 
	 * @param getPort
	 *            boolean
	 * 
	 * @return X43REEPortType
	 */
	private X43REEPortType getX43ExpedienteElectronicoServicePort(boolean getPort) {
		try {
			Properties prop = Config.loadProperties(Constantes.FICHERO_CONFIGURACION);

			if (this.service == null) {

				X43ExpedienteElectronicoServiceImpl.logger.info("URL del servicio X43ExpedienteElectronico = "
						+ prop.getProperty("X43ExpedienteElectronico_URL"));

				// Creamos el servicio
				this.service = new X43REE(new URL(prop.getProperty("X43ExpedienteElectronico_URL")),
						X43ExpedienteElectronicoServiceImpl.qname);

				// definimos un handler cliente
				this.service.setHandlerResolver(new HandlerResolver() {
					@SuppressWarnings(value = "unchecked")
					public List getHandlerChain(PortInfo portInfo) {
						return Collections.singletonList(new X43ExpedienteElectronicoServiceHandler());
					}
				});

				X43ExpedienteElectronicoServiceImpl.logger.info(
						"getX43ExpedienteElectronicoServicePort. Dentro de creaci�n del servicio - Definido el handler");
			}
			if (getPort) {

				X43ExpedienteElectronicoServiceImpl.logger
						.info("getX43ExpedienteElectronicoServicePort. Antes de obtener el proxy");

				X43REEPortType proxy = this.service.getX43REEPort();

				X43ExpedienteElectronicoServiceImpl.logger
						.info("getX43ExpedienteElectronicoServicePort. Despues de obtener el proxy");

				Map<String, Object> ctxt = ((BindingProvider) proxy).getRequestContext();

				X43ExpedienteElectronicoServiceImpl.logger
						.info("getX43ExpedienteElectronicoServicePort. Despues de obtener el contexto");

				ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
						// prop.getProperty("URL_WS_X43ExpedienteElectronico"));
						prop.getProperty("X43ExpedienteElectronico_URL"));

				X43ExpedienteElectronicoServiceImpl.logger
						.info("getX43ExpedienteElectronicoServicePort. Cargado el contexto");

				return proxy;
			}

		} catch (Exception e) {

			X43ExpedienteElectronicoServiceImpl.logger
					.info("getX43ExpedienteElectronicoServicePort. Excepcionnnn:" + e.getMessage());

			this.service = null;
		}
		return null;
	}

	/**
	 * Metodo que recupera el ejgvDocumento de la solicitud
	 * 
	 * @param request
	 *            TDocumentSignRequest
	 * @param auditUser
	 *            String
	 * @param ep
	 *            AExtensionPoint
	 * 
	 * @return TBoolean
	 * 
	 * @throws X43ExpedienteElectronicoException
	 *             X43ExpedienteElectronicoException
	 */
	public TBoolean updateFolderTitle(String folderID, String folderTitle, AExtensionPoint ep)
			throws Exception_Exception {

		return this.getX43ExpedienteElectronicoServicePort().updateFolderTitle(folderID, folderTitle, ep);

	}

	/**
	 * Metodo que devuele los canalees de aviso de las personas de un expediente
	 * 
	 * @param query
	 *            TFolderQuery
	 * @param ep
	 *            AExtensionPoint
	 * @return TFolderList
	 * @throws Exception_Exception
	 *             Exception_Exception
	 */
	public TFolderList SearchFolders(TFolderQuery query, AExtensionPoint ep) throws Exception_Exception {

		return this.getX43ExpedienteElectronicoServicePort().searchFolders(query, ep);
	}

	/**
	 * Metodo que recupera los documentos del expediente
	 * 
	 * @param folderID
	 *            id del expediente
	 * @param procedureID
	 *            del expediente
	 * @param auditUser
	 *            audit user
	 * @param ep
	 *            AExtensionPoint
	 * @return TFolderDocumentList
	 * @throws Exception_Exception
	 */
	public TFolderDocumentList getFolderDocuments(String folderID, String procedureID, String auditUser,
			AExtensionPoint ep) throws Exception_Exception {
		return this.getX43ExpedienteElectronicoServicePort().getFolderDocuments(folderID, procedureID, auditUser, ep);
	}

	/**
	 * Metodo que devuele los documentos de un expediente
	 * 
	 * @param query
	 *            TFolderQuery
	 * @param ep
	 *            AExtensionPoint
	 * @return TFolderList
	 * @throws Exception_Exception
	 *             Exception_Exception
	 */
	public TGetTelActionDocumentList GetTelActionDocuments(String folderID, String procedureID, String auditUser,
			AExtensionPoint ep) throws Exception {

		return this.getX43ExpedienteElectronicoServicePort().getTelActionDocuments(folderID, procedureID, auditUser,
				ep);
	}

}
