package Controllers.Traitement;

import Models.User.User;
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;
/**
 * @author Maher
 */
public class ParametreSystem 
{   
    public static boolean ErrorShowLogger ;
    public static boolean ErrorShowDialog ;
    
    public static String CompanyName ;        
    public static String CompanyPlace ;
    public static String CompanyAddress ;
    public static String CompanySIREN ; //Système d’Identification du Répertoire des Entreprises
    public static String CompanyMF ; //Matricule Fiscale
    public static String CompanyIF ;  //Identité Fiscale
    public static String CompanyTele ;
    public static String CompanyFax ;
    public static String CompanyMail ;
    public static String CompanySite ;
    
    public static String NomLocalPC ;
    public static String Timbre ;
    
    public static String CurrencySign ;
    public static String CurrencyName ;
    public static String CurrencyThousandth ;
     
    public static String Bank ;
    public static String RIP ;
    public static String RIB ;
    
    public boolean SetJsonParametre(String strUrl)
    {
        try {
            URL url = new URL(strUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            if (connection.getResponseCode() == 200) 
            {
                String jsonResponse = fetchJsonData(connection, strUrl);
                JsonObject jsonObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
                // Vous pouvez maintenant utiliser les données JSON analysées, par exemple :
                User.idprofile = 2;
                User.nom = jsonObject.get("UserNom").getAsString();
                User.prenom = jsonObject.get("UserPrenom").getAsString();
                User.UserLocal = "Magasin I";
                User.SotckNegatif= true;
                
                CompanyName = jsonObject.get("CompanyName").getAsString();        
                CompanyPlace = jsonObject.get("CompanyPlace").getAsString();
                CompanyAddress = jsonObject.get("CompanyAddress").getAsString();
                CompanySIREN = jsonObject.get("CompanySIREN").getAsString();
                CompanyMF = jsonObject.get("CompanyMF").getAsString();
                CompanyIF = jsonObject.get("CompanyIF").getAsString();
                CompanyTele = jsonObject.get("CompanyTele").getAsString();
                CompanyFax = jsonObject.get("CompanyFax").getAsString();
                CompanyMail = jsonObject.get("CompanyMail").getAsString();
                CompanySite = jsonObject.get("CompanySite").getAsString();

                NomLocalPC = "Magasin I";
                Timbre = jsonObject.get("Timbre").getAsString();

                CurrencySign = jsonObject.get("CurrencySign").getAsString();
                CurrencyName = jsonObject.get("CurrencyName").getAsString();
                CurrencyThousandth = jsonObject.get("CurrencyThousandth").getAsString();

                Bank = jsonObject.get("Bank").getAsString();
                RIP = jsonObject.get("RIP").getAsString();
                RIB = jsonObject.get("RIB").getAsString();
                return true;
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return false;
    }
    
    private String fetchJsonData(HttpURLConnection connection, String urlString) throws Exception 
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder content = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            content.append(inputLine);
        }
        in.close();
        connection.disconnect();

        return content.toString();
    }
    
    public void SetParametre()
    {
        ParametreSystem.ErrorShowLogger = true;
        ParametreSystem.ErrorShowDialog = true;
        
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            // optional, but recommended
            // process XML securely, avoid attacks like XML External Entities (XXE)
            dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

            // parse XML file
            DocumentBuilder db = dbf.newDocumentBuilder();

            //PowerERP\conf\parametre.xml
            Document doc = db.parse(new File("./conf/parametre.xml"));

            doc.getDocumentElement().normalize();
            
            Element elementCompany  = (Element) doc.getElementsByTagName("Company").item(0);
            
            String company_name = elementCompany.getElementsByTagName("Name").item(0).getTextContent();
            ParametreSystem.CompanyName = company_name;
            
            String company_place = elementCompany.getElementsByTagName("Place").item(0).getTextContent();
            ParametreSystem.CompanyPlace = company_place;
            
            String company_address = elementCompany.getElementsByTagName("Address").item(0).getTextContent();
            ParametreSystem.CompanyAddress = company_address;
            
            String companySIREN = elementCompany.getElementsByTagName("SIREN").item(0).getTextContent();
            ParametreSystem.CompanySIREN = companySIREN; 
            
            String companyMF = elementCompany.getElementsByTagName("MF").item(0).getTextContent();
            ParametreSystem.CompanyMF =companyMF; 
            
            String companyIF = elementCompany.getElementsByTagName("IF").item(0).getTextContent();
            ParametreSystem.CompanyIF = companyIF; 
            
            String companyTele = elementCompany.getElementsByTagName("Tele").item(0).getTextContent();
            ParametreSystem.CompanyTele = companyTele;
            
            String companyFax = elementCompany.getElementsByTagName("Fax").item(0).getTextContent();
            ParametreSystem.CompanyFax = companyFax;
            
            String companyMail = elementCompany.getElementsByTagName("Mail").item(0).getTextContent();
            ParametreSystem.CompanyMail = companyMail;
            
            String companySite = elementCompany.getElementsByTagName("Site").item(0).getTextContent();
            ParametreSystem.CompanySite = companySite;
            
            /******************************/
            
            Element elementNomLocalPC  = (Element) doc.getElementsByTagName("NomLocalPC").item(0);
            ParametreSystem.NomLocalPC = elementNomLocalPC.getTextContent() ;
            
            Element elementTimbre  = (Element) doc.getElementsByTagName("Timbre").item(0);
            ParametreSystem.Timbre = elementTimbre.getTextContent() ;
            
            
            /********************************/
            
            Element elementCurrency = (Element) doc.getElementsByTagName("Currency").item(0);
            
            String currency_sign = elementCurrency.getElementsByTagName("Sign").item(0).getTextContent();
            ParametreSystem.CurrencySign = currency_sign;
            
            String currency_name = elementCurrency.getElementsByTagName("Name").item(0).getTextContent();
            ParametreSystem.CurrencyName = currency_name;
            
            String currency_thousandth = elementCurrency.getElementsByTagName("Thousandth").item(0).getTextContent();
            ParametreSystem.CurrencyThousandth = currency_thousandth;
            
            
            /*************************************/
            
            Element elementBanque = (Element) doc.getElementsByTagName("Banque").item(0);
            
            String bank = elementBanque.getElementsByTagName("Bank").item(0).getTextContent();
            ParametreSystem.Bank = bank;
            
            String rip = elementBanque.getElementsByTagName("RIP").item(0).getTextContent();
            ParametreSystem.RIP = rip;
            
            String rib = elementBanque.getElementsByTagName("RIB").item(0).getTextContent();
            ParametreSystem.RIB = rib;
        } 
        catch (ParserConfigurationException | SAXException | IOException e) {
            System.out.println("error Controllers\\Traitement\\ParametreSystem.java/SetParametre "+e.getMessage());
            System.out.println(e.getMessage());
        }
    }
}
