package com.ejie.ab79.webservice;

import java.net.URL;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Service;

import com.ejie.ab79.clientesWS.ab63WebService.Ab61Exception;
import com.ejie.ab79.clientesWS.ab63WebService.Ab63Exception;
import com.ejie.ab79.clientesWS.ab63WebService.Ab63ExceptionGeneral;
import com.ejie.ab79.clientesWS.ab63WebService.Ab63WebService;
import com.ejie.ab79.clientesWS.ab63WebService.DatosEstablecimientoWS;
import com.ejie.ab79.clientesWS.ab63WebService.IOException_Exception;
import com.ejie.ab79.clientesWS.ab63WebService.IdentificadorDocumento;
import com.ejie.ab79.clientesWS.ab63WebService.PifException_Exception;
import com.ejie.ab79.clientesWS.ab63WebService.WebServiceImpl;
import com.ejie.ab79.comun.UtilsConfig;

@Service(value = "ab63WebServiceServiceImpl")
public class ab63WebServiceServiceImpl implements ab63WebServiceService {

	@Resource
	ReloadableResourceBundleMessageSource appMessageSource;

	public static final Logger logger = LoggerFactory.getLogger(ab63WebServiceServiceImpl.class);

	private Ab63WebService service;

	private WebServiceImpl getab63WebServiceServiceImplServicePort() {
		return this.getab63WebServiceServiceImplServicePort(true);
	}

	/**
	 * 
	 * @param getPort
	 *            boolean
	 * 
	 * @return proxy WebServiceImpl
	 */
	private WebServiceImpl getab63WebServiceServiceImplServicePort(boolean getPort) {
		try {

			if (this.service == null) {

				// Creamos el servicio
				logger.info("getab63WebServiceServiceImplServicePort - Crear llamada al Servicio Web:");
				logger.info("Ab63WebService_URL:" + UtilsConfig.getAppProperties().getProperty("Ab63WebService_URL"));
				logger.info("Ab63WebService_QNAME:" + UtilsConfig.getAppProperties().getProperty("Ab63WebService_QNAME"));
				logger.info("Ab63WebService_WS:" + UtilsConfig.getAppProperties().getProperty("Ab63WebService_WS"));

				service = new Ab63WebService(new URL(UtilsConfig.getAppProperties().getProperty("Ab63WebService_URL")),
						new QName(UtilsConfig.getAppProperties().getProperty("Ab63WebService_QNAME"),
								UtilsConfig.getAppProperties().getProperty("Ab63WebService_WS")));

				logger.info(
						"getab63WebServiceServiceImplServicePort - Creación de la llamada al Servicio Web finalizada");

			}
			if (getPort) {

				logger.info("getab63WebServiceServiceImplServicePort - Antes de obtener el proxy");

				WebServiceImpl proxy = this.service.getAb63WebServicePort();

				logger.info("getab63WebServiceServiceImplServicePort - Proxy obtenido");

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

				logger.info("getab63WebServiceServiceImplServicePort - Contexto obtenido");

				ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, UtilsConfig.getAppProperties().getProperty("Ab63WebService_URL"));

				logger.info("getab63WebServiceServiceImplServicePort - Contexto cargado");

				return proxy;
			}
		} catch (Exception e) {

			logger.error("getab63WebServiceServiceImplServicePort. Excepcion:" + e.getMessage());

			this.service = null;
		}
		return null;
	}

	public String actualizarEstablecimiento(String folderId, DatosEstablecimientoWS datosEstablecimientoWS,
			String usuario) throws Ab63Exception {
		try {
			return getab63WebServiceServiceImplServicePort().actualizarEstablecimiento(folderId, datosEstablecimientoWS,
					usuario);
		} catch (Exception exception) {

			logger.error("getab63WebServiceServiceImplServicePort.actualizarEstablecimiento", exception);

			String sCodigoError = "error.conectarAB63";

			Ab63ExceptionGeneral ab63ExceptionGeneral = new Ab63ExceptionGeneral();
			ab63ExceptionGeneral.setCodigo(sCodigoError);
			ab63ExceptionGeneral
					.setMensajeCastellano(appMessageSource.getMessage(sCodigoError, null, new Locale("es")));
			ab63ExceptionGeneral.setMensajeEuskera(appMessageSource.getMessage(sCodigoError, null, new Locale("eu")));

			throw new Ab63Exception(exception.getMessage(), ab63ExceptionGeneral);

		} finally {
			logger.info("getab63WebServiceServiceImplServicePort.actualizarEstablecimiento - Final");
		}
	}

	@Override
	public IdentificadorDocumento generarExcelEstablecimiento(String numeroRegistro)
			throws Ab61Exception, Ab63Exception, IOException_Exception, PifException_Exception {
		try {
			return getab63WebServiceServiceImplServicePort().generarExcelEstablecimiento(numeroRegistro);
		} catch (Exception exception) {

			logger.error("getab63WebServiceServiceImplServicePort.generarExcelEstablecimiento", exception);

			String sCodigoError = "error.conectarAB63";

			Ab63ExceptionGeneral ab63ExceptionGeneral = new Ab63ExceptionGeneral();
			ab63ExceptionGeneral.setCodigo(sCodigoError);
			ab63ExceptionGeneral
					.setMensajeCastellano(appMessageSource.getMessage(sCodigoError, null, new Locale("es")));
			ab63ExceptionGeneral.setMensajeEuskera(appMessageSource.getMessage(sCodigoError, null, new Locale("eu")));

			throw new Ab63Exception(exception.getMessage(), ab63ExceptionGeneral);

		} finally {
			logger.info("getab63WebServiceServiceImplServicePort.generarExcelEstablecimiento - Final");
		}
	}

}
