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

import java.io.IOException;
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.ab63.clientesWS.X43ExpedienteElectronico.AExtensionPoint;
import com.ejie.ab63.clientesWS.X43ExpedienteElectronico.TBoolean;
import com.ejie.ab63.clientesWS.X43ExpedienteElectronico.TFolderList;
import com.ejie.ab63.clientesWS.X43ExpedienteElectronico.TFolderQuery;
import com.ejie.ab63.clientesWS.X43ExpedienteElectronico.TGetTelActionDocumentList;
import com.ejie.ab63.clientesWS.X43ExpedienteElectronico.X43REE;
import com.ejie.ab63.clientesWS.X43ExpedienteElectronico.X43REEPortType;
import com.ejie.ab63.comun.Constantes;
import com.ejie.ab63.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
	 * @throws IOException
	 */
	private X43REEPortType getX43ExpedienteElectronicoServicePort() throws IOException {
		return this.getX43ExpedienteElectronicoServicePort(true);
	}

	/**
	 * 
	 * @param getPort
	 *            boolean
	 * 
	 * @return X43REEPortType
	 * @throws IOException
	 */
	private X43REEPortType getX43ExpedienteElectronicoServicePort(boolean getPort) throws IOException {
		// 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,
					// "http://svc.extra.integracion.jakina.ejiedes.net/ctxapp/X43ExpedienteElectronico?WSDL");
					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 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 {
		return this.getX43ExpedienteElectronicoServicePort().searchFolders(query, 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);
	}

	/**
	 * Metodo que devuele los documentos de un expediente
	 * 
	 * @param query
	 *            TFolderQuery
	 * @param ep
	 *            AExtensionPoint
	 * @return TFolderList
	 * @throws Exception_Exception
	 *             Exception_Exception
	 */
	public TBoolean updateTitle(String folderID, String title, String auditUser, AExtensionPoint ep) throws Exception {
		return this.getX43ExpedienteElectronicoServicePort().updateFolderTitle(folderID, title, ep);
	}

}
