package com.triz.util;

import com.triz.trizservice.service.TransactionService;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UtilGetParametre {

    public static String TRIZSTOCK_BD_URL = "trizstock.jdbc.url";
    public static String TRIZSTOCK_BD_USER = "trizstock.jdbc.user";
    public static String TRIZSTOCK_BD_PASSWORD = "trizstock.jdbc.password";
    public static String TRIZSTOCK_BD_DRIVER = "trizstock.jdbc.driver";
    public static String TRIZDISTRIBUTION_ID = "trizdistribution.id";
    public static String TRIZSTOCK_USE = "trizstock.use";
    public static String TRIZSTOCK_MAITRE = "trizstock.maitre";
    public static String TRIZSTOCK_MAITRE_PORT = "trizstock.maitre.port";
    public static String TRIZDISTRIBUTION_USE = "trizdistribution.use";
    public static String TRIZDISTRIBUTION_BD_URL = "jdbc.url";
    public static String TRIZDISTRIBUTION_BD_USER = "jdbc.user";
    public static String TRIZDISTRIBUTION_BD_PASSWORD = "jdbc.password";
    public static String TRIZDISTRIBUTION_BD_DRIVER = "jdbc.driver";
    public static String TRIZDISTRIBUTION_SERVEUR_ADRESSEIP = "trizdistribution.serveur.adresseip";
    public static String TRIZDISTRIBUTION_SERVEUR_PORT = "trizdistribution.serveur.port";
    public static String TRIZDISTRIBUTION_ETABLISSEMENT_NOM = "trizdistribution.etablissement.nom";
    public static String TRIZDISTRIBUTION_MOBILE_PORT = "trizdistribution.mobile.port";
    public static String TRIZDISTRIBUTION_MOBILE_PORT_MAG = "trizdistribution.mobile.portmag";
    public static String TRIZDISTRIBUTION_MULTI_ETABLISSEMENT_PORT = "trizdistribution.multiEtablissement.port";
    public static String TRIZDISTRIBUTION_DOSSIER_CHEMAIN = "trizdistribution.dossier.chemain";
    public static String TRIZDISTRIBUTION_DOSSIER_MOBILE_CHEMAIN = "trizdistribution.dossier.chemain.mobile";
    public static String TRIZDISTRIBUTION_CODE_ETABLISSEMENT = "trizdistribution.code.etablissement";
    public static String TRIZDISTRIBUTION_ADRESSE_WEB = "trizdistribution.adresse.web";  
    public static String TB_USE = "tb.use";
    
    private static Map<String, String> map = new HashMap<String, String>();
     
     //public static String idEtablissement;
    
    @Autowired
    private TransactionService service;
     
    @Autowired
    public UtilContext context;
    

    public UtilGetParametre() {
        getProperties();
    }

    public Map<String, String> getProperties() {
        if (!map.isEmpty()) {
            return map;
        }
        Properties propertiesFile = new Properties();
        URL r = this.getClass().getResource("/");
        try {

            System.err.println(r.toURI());
            InputStream inputStream = new FileInputStream(new File(r.toURI()) + File.separator + "database.properties");
            propertiesFile.load(new InputStreamReader(inputStream));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }

        System.out.println(TRIZDISTRIBUTION_BD_URL + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_BD_URL));
        System.out.println(TRIZDISTRIBUTION_BD_USER + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_BD_USER));
        System.out.println(TRIZDISTRIBUTION_BD_PASSWORD + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_BD_PASSWORD));
        System.out.println(TRIZDISTRIBUTION_BD_DRIVER + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_BD_DRIVER));
        System.out.println(TRIZDISTRIBUTION_SERVEUR_ADRESSEIP + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_SERVEUR_ADRESSEIP));
        System.out.println(TRIZDISTRIBUTION_SERVEUR_PORT + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_SERVEUR_PORT));
        System.out.println(TRIZDISTRIBUTION_ETABLISSEMENT_NOM + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_ETABLISSEMENT_NOM));
        System.out.println(TRIZDISTRIBUTION_MOBILE_PORT_MAG + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_MOBILE_PORT_MAG));
        System.out.println(TRIZDISTRIBUTION_MOBILE_PORT + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_MOBILE_PORT));
        System.out.println(TRIZDISTRIBUTION_MULTI_ETABLISSEMENT_PORT + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_MULTI_ETABLISSEMENT_PORT));
        System.out.println(TRIZDISTRIBUTION_DOSSIER_MOBILE_CHEMAIN + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_DOSSIER_MOBILE_CHEMAIN));
        System.out.println(TRIZDISTRIBUTION_DOSSIER_CHEMAIN + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_DOSSIER_CHEMAIN));
        System.out.println(TRIZDISTRIBUTION_CODE_ETABLISSEMENT + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_CODE_ETABLISSEMENT));
        System.out.println(TRIZDISTRIBUTION_ADRESSE_WEB + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_ADRESSE_WEB));
        System.out.println(TRIZDISTRIBUTION_ID + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_ID));
        System.out.println(TRIZDISTRIBUTION_USE + " :" + propertiesFile.getProperty(TRIZDISTRIBUTION_USE));
        System.out.println(TRIZSTOCK_BD_PASSWORD + " :" + propertiesFile.getProperty(TRIZSTOCK_BD_PASSWORD));
        System.out.println(TRIZSTOCK_BD_DRIVER + " :" + propertiesFile.getProperty(TRIZSTOCK_BD_DRIVER));
        System.out.println(TRIZSTOCK_BD_URL + " :" + propertiesFile.getProperty(TRIZSTOCK_BD_URL));
        System.out.println(TRIZSTOCK_BD_USER + " :" + propertiesFile.getProperty(TRIZSTOCK_BD_USER));
        System.out.println(TRIZSTOCK_USE + " :" + propertiesFile.getProperty(TRIZSTOCK_USE));
        System.out.println(TRIZSTOCK_MAITRE + " :" + propertiesFile.getProperty(TRIZSTOCK_MAITRE));
        System.out.println(TRIZSTOCK_MAITRE_PORT + " :" + propertiesFile.getProperty(TRIZSTOCK_MAITRE_PORT));

        map.put(TRIZDISTRIBUTION_BD_URL, propertiesFile.getProperty(TRIZDISTRIBUTION_BD_URL));
        map.put(TRIZDISTRIBUTION_BD_USER, propertiesFile.getProperty(TRIZDISTRIBUTION_BD_USER));
        map.put(TRIZDISTRIBUTION_BD_PASSWORD, propertiesFile.getProperty(TRIZDISTRIBUTION_BD_PASSWORD));
        map.put(TRIZDISTRIBUTION_BD_DRIVER, propertiesFile.getProperty(TRIZDISTRIBUTION_BD_DRIVER));
        map.put(TRIZDISTRIBUTION_SERVEUR_ADRESSEIP, propertiesFile.getProperty(TRIZDISTRIBUTION_SERVEUR_ADRESSEIP));
        map.put(TRIZDISTRIBUTION_SERVEUR_PORT, propertiesFile.getProperty(TRIZDISTRIBUTION_SERVEUR_PORT));
        map.put(TRIZDISTRIBUTION_ETABLISSEMENT_NOM, propertiesFile.getProperty(TRIZDISTRIBUTION_ETABLISSEMENT_NOM));
        map.put(TRIZDISTRIBUTION_MOBILE_PORT, propertiesFile.getProperty(TRIZDISTRIBUTION_MOBILE_PORT));
        map.put(TRIZDISTRIBUTION_MOBILE_PORT_MAG, propertiesFile.getProperty(TRIZDISTRIBUTION_MOBILE_PORT_MAG));
        map.put(TRIZDISTRIBUTION_MULTI_ETABLISSEMENT_PORT, propertiesFile.getProperty(TRIZDISTRIBUTION_MULTI_ETABLISSEMENT_PORT));
        map.put(TRIZDISTRIBUTION_DOSSIER_CHEMAIN, propertiesFile.getProperty(TRIZDISTRIBUTION_DOSSIER_CHEMAIN));
        map.put(TRIZDISTRIBUTION_DOSSIER_MOBILE_CHEMAIN, propertiesFile.getProperty(TRIZDISTRIBUTION_DOSSIER_MOBILE_CHEMAIN));
        map.put(TRIZDISTRIBUTION_CODE_ETABLISSEMENT, propertiesFile.getProperty(TRIZDISTRIBUTION_CODE_ETABLISSEMENT));
        map.put(TRIZDISTRIBUTION_ADRESSE_WEB, propertiesFile.getProperty(TRIZDISTRIBUTION_ADRESSE_WEB));
        map.put(TRIZDISTRIBUTION_ID, propertiesFile.getProperty(TRIZDISTRIBUTION_ID));
        map.put(TRIZDISTRIBUTION_USE, propertiesFile.getProperty(TRIZDISTRIBUTION_USE));
        map.put(TRIZSTOCK_BD_PASSWORD, propertiesFile.getProperty(TRIZSTOCK_BD_PASSWORD));
        map.put(TRIZSTOCK_BD_DRIVER, propertiesFile.getProperty(TRIZSTOCK_BD_DRIVER));
        map.put(TRIZSTOCK_BD_URL, propertiesFile.getProperty(TRIZSTOCK_BD_URL));
        map.put(TRIZSTOCK_BD_USER, propertiesFile.getProperty(TRIZSTOCK_BD_USER));
        map.put(TRIZSTOCK_USE, propertiesFile.getProperty(TRIZSTOCK_USE));
        map.put(TRIZSTOCK_MAITRE, propertiesFile.getProperty(TRIZSTOCK_MAITRE));
        map.put(TRIZSTOCK_MAITRE_PORT, propertiesFile.getProperty(TRIZSTOCK_MAITRE_PORT));
        map.put(TB_USE, propertiesFile.getProperty(TB_USE));

        return map;
    }

    public String getPropertyByKey(String key) {
        if (map.containsKey(key)) {
            return map.get(key);
        }
        return null;
    }

    public Connection getConnection() throws SQLException {

        UtilGetParametre ugp = new UtilGetParametre();
        String url = ugp.getPropertyByKey(UtilGetParametre.TRIZDISTRIBUTION_BD_URL);
        String userName = ugp.getPropertyByKey(UtilGetParametre.TRIZDISTRIBUTION_BD_USER);
        String passWord = ugp.getPropertyByKey(UtilGetParametre.TRIZDISTRIBUTION_BD_PASSWORD);

        return DriverManager.getConnection(url, userName, passWord);
    }

    public Connection getConnectionStock() throws SQLException {

        UtilGetParametre ugp = new UtilGetParametre();
        String url = ugp.getPropertyByKey(UtilGetParametre.TRIZSTOCK_BD_URL);
        String userName = ugp.getPropertyByKey(UtilGetParametre.TRIZSTOCK_BD_USER);
        String passWord = ugp.getPropertyByKey(UtilGetParametre.TRIZSTOCK_BD_PASSWORD);

        return DriverManager.getConnection(url, userName, passWord);
    }

    
    public static String getIdEtablissement() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        if(request!=null){
        HttpSession mySessin = request.getSession();
        
          return (String)mySessin.getAttribute("idEtablissementStock");
        }
        return null;
    }

    public static void setIdEtablissement(String idEtablissement) {
        
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        if(request!=null){
        HttpSession mySessin = request.getSession();
        mySessin.setAttribute("idEtablissementStock", idEtablissement);
       // UtilGetParametre.idEtablissement = idEtablissement;
        }
    }
}
