/**
 * 
 */
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.X43Toolkit.Exception_Exception;
import com.ejie.ab61.clientesWS.X43Toolkit.X43KWebService;
import com.ejie.ab61.clientesWS.X43Toolkit.X43KWebService_Service;
import com.ejie.ab61.comun.Constantes;
import com.ejie.ab61.comun.utilidades.config.Config;

/**
 *  
 * 
 */
@Service(value = "X43ToolkitServiceImpl")
public class X43ToolkitServiceImpl implements X43ToolkitService {

	private static final Logger logger = LoggerFactory.getLogger(X43ToolkitServiceImpl.class);

	private X43KWebService_Service service;

	private static final QName qname = new QName("com/ejie/toolkit/xml", "x43kWebService");

	/**
	 * 
	 * @return X43KWebService
	 */
	private X43KWebService getX43ToolkitServicePort() {
		return this.getX43ToolkitServicePort(true);
	}

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

			if (this.service == null) {

				X43ToolkitServiceImpl.logger.info("getX43ToolkitServicePort. Dentro de creaci�n del servicio");

				X43ToolkitServiceImpl.logger
						.info("URL del servicio X43Toolkit_URL = " + prop.getProperty("X43Toolkit_URL"));

				// Creamos el servicio
				this.service = new X43KWebService_Service(new URL(prop.getProperty("X43Toolkit_URL")),
						X43ToolkitServiceImpl.qname);

				// definimos un handler cliente
				this.service.setHandlerResolver(new HandlerResolver() {
					public List getHandlerChain(PortInfo portInfo) {
						return Collections.singletonList(new X43ToolkitServiceHandler());
					}
				});

			}
			if (getPort) {

				X43KWebService proxy = this.service.getX43KWebServicePort();
				Map<String, Object> ctxt = ((BindingProvider) proxy).getRequestContext();
				ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, prop.getProperty("X43Toolkit_URL"));
				X43ToolkitServiceImpl.logger.info("getX43ToolkitServicePort. Cargado el contexto");
				return proxy;
			}

		} catch (Exception e) {

			X43ToolkitServiceImpl.logger.info("getX43ToolkitServicePort. Excepcionnnn:" + e.getMessage());

			this.service = null;
		}
		return null;
	}

	/**
	 * Metodo que recupera el ejgvDocumento de la solicitud
	 * 
	 * @param idSolicitud
	 *            id del documento
	 * @param folderID
	 *            id del expediente
	 * 
	 * @return String: ejgvDocument Solicitud
	 * 
	 * @throws X43ToolkitException
	 *             X43ToolkitException
	 * @throws Exception_Exception
	 *             Exception_Exception
	 */
	public String getRequestInformation(String idSolicitud, String folderID) throws Exception_Exception {

		return this.getX43ToolkitServicePort().getRequestInformation(idSolicitud, folderID);
	}
}
