/* ----------------------------------------
 * Origin  : Akkodis
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * Version : V2.12
 * DM-ID : Redmine#11103 : 25/03/2025 : LBE : Radio spectro interface added
 * ----------------------------------------
 */
 
package fr.tlse.irap.pt.client.services;

import java.net.URL;

import javax.xml.namespace.QName;
import jakarta.xml.ws.Service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cdpp_irap.impex.v0.MethodsAMDA;
import fr.tlse.irap.fieldLine.IFieldLine;
import fr.tlse.irap.fit.IFit;
import fr.tlse.irap.flare.IFlare;
import fr.tlse.irap.map.ICarringtonMap;
import fr.tlse.irap.map.IEclipticMap;
import fr.tlse.irap.map.IJMap;
import fr.tlse.irap.pos.IPosition;
import fr.tlse.irap.pt.client.services.constant.CONFIG;
import fr.tlse.irap.shock.IShock;
import fr.tlse.irap.spectro.ISpectro;
import fr.tlse.irap.tool.ITool;
import fr.tlse.irap.vplot.IVPlot;

public class ServiceFactory {

	private static final Logger log = LoggerFactory.getLogger(ServiceFactory.class);

	private final static String AMDA_URL = CONFIG.AMDA_SERVER_URL + "/" + CONFIG.AMDA_SERVICE + ".wsdl";
	private final static String POSITION_URL = CONFIG.SERVER_URL + "/" + CONFIG.POSITION_SERVICE + "?wsdl";
	private final static String VPLOT_URL = CONFIG.SERVER_URL + "/" + CONFIG.VPLOT_SERVICE + "?wsdl";
	private final static String SEP_URL = CONFIG.SERVER_URL + "/" + CONFIG.SPECTRO_SERVICE + "?wsdl";
	private final static String CARRINGTONMAP_URL = CONFIG.SERVER_URL + "/" + CONFIG.CARRINGTONMAP_SERVICE + "?wsdl";
	private final static String ECLIPTICMAP_URL = CONFIG.SERVER_URL + "/" + CONFIG.ECLIPTICMAP_SERVICE + "?wsdl";
	private final static String FLARE_URL = CONFIG.SERVER_URL + "/" + CONFIG.FLARE_SERVICE + "?wsdl";
	private final static String JMAP_URL = CONFIG.SERVER_URL + "/" + CONFIG.JMAP_SERVICE + "?wsdl";
	private final static String FIT_URL = CONFIG.SERVER_URL + "/" + CONFIG.FIT_SERVICE + "?wsdl";
	private final static String TOOL_URL = CONFIG.SERVER_URL + "/" + CONFIG.TOOL_SERVICE + "?wsdl";
	private final static String FIELDLINE_URL = CONFIG.SERVER_URL + "/" + CONFIG.FIELDLINE_SERVICE + "?wsdl";
	private final static String SHOCK_URL = CONFIG.SERVER_URL + "/" + CONFIG.SHOCK_SERVICE + "?wsdl";
	
	private static MethodsAMDA amdaService = null;
	private static IPosition positionService = null;
	private static IVPlot vPlotService = null;
	private static ISpectro spectroService = null;
	private static ICarringtonMap carringtonMapService = null;
	private static IEclipticMap eclipticMapService = null;
	private static IFlare flareService = null;
	private static IJMap jMapService = null;
	private static IFit fitService = null;
	private static ITool toolService = null;
	private static IFieldLine fieldLineService = null;
	private static IShock shockService = null;
	
	public ServiceFactory() {
	}

	public static MethodsAMDA getAmdaService() {
		if (amdaService == null) {
			log.info("Call getAmdaService, URL " + AMDA_URL);
			URL url = null;
			try {
				url = new URL(AMDA_URL);
				QName qname = new QName(CONFIG.AMDA_NAME_SPACE_URI, CONFIG.AMDA_SERVICE_IMPL);
				Service service = Service.create(url, qname);
				amdaService = service.getPort(MethodsAMDA.class);

			} catch (final Exception e) {
				e.printStackTrace();
				log.error("Web service error getAmdaService");
				// Propagating error message to the calling thread (RuntimeException)
				throw new RuntimeException(e.getMessage());
			}
		}
		return amdaService;
	}

	public static IPosition getPositionService() {
		if (positionService == null) {
			log.info("Call getPosition web service, URL " + POSITION_URL);
			URL url = null;
			try {
				url = new URL(POSITION_URL);
				QName qname = new QName(CONFIG.NAME_SPACE_URI, CONFIG.POSITION_SERVICE_IMPL);
				Service service = Service.create(url, qname);
				positionService = service.getPort(IPosition.class);

			} catch (final Exception e) {
				e.printStackTrace();
				log.error("Web service error getPositionService");
				// Propagating error message to the calling thread (RuntimeException)
				throw new RuntimeException(e.getMessage());
			}
		}
		return positionService;
	}

	public static ICarringtonMap getCarringtonMapService() {
		if (carringtonMapService == null) {
			log.info("Call getCarringtonMap web service, URL " + CARRINGTONMAP_URL);
			URL url;
			try {
				url = new URL(CARRINGTONMAP_URL);
				QName qname = new QName(CONFIG.NAME_SPACE_URI, CONFIG.CARRIGTONMAP_SERVICE_IMPL);
				Service service = Service.create(url, qname);
				carringtonMapService = service.getPort(ICarringtonMap.class);
			} catch (final Exception e) {
				e.printStackTrace();
				log.warn("Carrington map not found");
			}
		}
		return carringtonMapService;
	}

	public static IEclipticMap getEclipticMapService() {
		if (eclipticMapService == null) {
			log.info("Call getEclipticMap web service, URL " + ECLIPTICMAP_URL);
			URL url;
			try {
				url = new URL(ECLIPTICMAP_URL);
				QName qname = new QName(CONFIG.NAME_SPACE_URI, CONFIG.ECLIPTICMAP_SERVICE_IMPL);
				Service service = Service.create(url, qname);
				eclipticMapService = service.getPort(IEclipticMap.class);
			} catch (final Exception e) {
				e.printStackTrace();
				log.warn("Ecliptic map not found");
			}
		}
		return eclipticMapService;
	}

	public static IJMap getJMapService() {
		if (jMapService == null) {
			log.info("Call getJMap web service, URL " + JMAP_URL);
			URL url;
			try {
				url = new URL(JMAP_URL);
				QName qname = new QName(CONFIG.NAME_SPACE_URI, CONFIG.JMAP_SERVICE_IMPL);
				Service service = Service.create(url, qname);
				jMapService = service.getPort(IJMap.class);
			} catch (final Exception e) {
				e.printStackTrace();
				log.warn("J-Map not found");
			}
		}
		return jMapService;
	}

	public static IFit getFitService() {
		if (fitService == null) {
			log.info("Call getFit web service, URL " + FIT_URL);
			URL url;
			try {
				url = new URL(FIT_URL);
				QName qname = new QName(CONFIG.NAME_SPACE_URI, CONFIG.FIT_SERVICE_IMPL);
				Service service = Service.create(url, qname);
				fitService = service.getPort(IFit.class);
			} catch (final Exception e) {
				e.printStackTrace();
				log.warn("Fit not found");
			}
		}
		return fitService;
	}

	public static IFlare getFlareService() {
		if (flareService == null) {
			log.info("Call getFlare web service, URL " + FLARE_URL);

			URL url = null;
			try {
				url = new URL(FLARE_URL);
				QName qname = new QName(CONFIG.NAME_SPACE_URI, CONFIG.FLARE_SERVICE_IMPL);
				Service service = Service.create(url, qname);
				flareService = service.getPort(IFlare.class);
			} catch (final Exception e) {
				e.printStackTrace();
				log.warn("Flare not found");
			}
		}	
		return flareService;
	}

	public static IVPlot getVPlotService() {
		if (vPlotService == null) {
			log.info("Call getVPlot web service, URL " + VPLOT_URL);

			URL url = null;
			try {
				url = new URL(VPLOT_URL);
				QName qname = new QName(CONFIG.NAME_SPACE_URI, CONFIG.VPLOT_SERVICE_IMPL);
				Service service = Service.create(url, qname);
				vPlotService = service.getPort(IVPlot.class);
			} catch (final Exception e) {
				e.printStackTrace();
				log.warn("VPlot not found");
			}
		}	
		return vPlotService;
	}
	public static ISpectro getSpectroService() {
		if (spectroService == null) {
			log.info("Call getSpectro web service, URL " + SEP_URL);

			URL url = null;
			try {
				url = new URL(SEP_URL);
				QName qname = new QName(CONFIG.NAME_SPACE_URI, CONFIG.SPECTRO_SERVICE_IMPL);
				Service service = Service.create(url, qname);
				spectroService = service.getPort(ISpectro.class);
			} catch (final Exception e) {
				e.printStackTrace();
				log.warn("sep not found");
			}
		}	
		return spectroService;
	}

	public static ITool getToolService() {
		if (toolService == null) {
			log.info("Call getTool web service, URL " + TOOL_URL);

			URL url = null;
			try {
				url = new URL(TOOL_URL);
				QName qname = new QName(CONFIG.NAME_SPACE_URI, CONFIG.TOOL_SERVICE_IMPL);
				Service service = Service.create(url, qname);
				toolService = service.getPort(ITool.class);
			} catch (final Exception e) {
				e.printStackTrace();
				log.warn("Tool not found");
			}
		}	
		return toolService;
	}

	public static IFieldLine getFieldLineService() {
		if (fieldLineService == null) {
			log.info("Call getFieldLineService web service, URL " + FIELDLINE_URL);

			URL url = null;
			try {
				url = new URL(FIELDLINE_URL);
				QName qname = new QName(CONFIG.NAME_SPACE_URI, CONFIG.FIELDLINE_SERVICE_IMPL);
				Service service = Service.create(url, qname);
				fieldLineService = service.getPort(IFieldLine.class);
			} catch (final Exception e) {
				e.printStackTrace();
				log.warn("FieldLine not found");
			}
		}	
		return fieldLineService;
	}

	public static IShock getShockService() {
		if (shockService == null) {
			log.info("Call getShock web service, URL " + SHOCK_URL);

			URL url = null;
			try {
				url = new URL(SHOCK_URL);
				QName qname = new QName(CONFIG.NAME_SPACE_URI, CONFIG.SHOCK_SERVICE_IMPL);
				Service service = Service.create(url, qname);
				shockService = service.getPort(IShock.class);
			} catch (final Exception e) {
				e.printStackTrace();
				log.warn("Shock not found");
			}
		}	
		return shockService;
	}

}