/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/javafx/FXMLController.java to edit this template
 */
package Controllers.TraiteClt;

import Controllers.Traitement.ParametreSystem;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.text.Text;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.TextFlow;
import javax.xml.XMLConstants;

/**
 * FXML Controller class
 *
 * @author Administrateur
 */
public class TraiteCltPrintController implements Initializable {
    
    @FXML private AnchorPane AnchorPaneTraiteClt;
    
    @FXML private Text TextCompanyName;
    @FXML private Text TextCompanyPlace;
    @FXML private Text TextCompanySIREN;
    @FXML private Text TextCompanyAddress;
    @FXML private TextFlow TextFlowCompanyAddress;
    
    @FXML private Text TextNomCompletClt;
    @FXML private Text TextCIN;
    
    @FXML private Text TextBank;
    @FXML private TextFlow TextFlowBank;
    
    @FXML private Text TextAmountLetter;
    @FXML private Text TextAmountFigures;
    @FXML private Text TextDateDeadline;
    
	
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        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();

            //C:\Users\Administrateur\Documents\NetBeansProjects\PowerERP\conf\print.xml
            Document doc = db.parse(new File("./conf/print.xml"));

            doc.getDocumentElement().normalize();

            Element elementTraiteClt  = (Element) doc.getElementsByTagName("TraiteClt").item(0);
     
            //AnchorPane
            Element elementAnchorPane = (Element)elementTraiteClt.getElementsByTagName("AnchorPaneTraiteClt").item(0);
            AnchorPaneTraiteClt.setPrefHeight(Double.parseDouble(elementAnchorPane.getElementsByTagName("prefHeight").item(0).getTextContent()));
            AnchorPaneTraiteClt.setPrefWidth(Double.parseDouble(elementAnchorPane.getElementsByTagName("prefWidth").item(0).getTextContent()));
              
            
            //CompanyName
            Element elementCompanyName = (Element)elementTraiteClt.getElementsByTagName("CompanyName").item(0);
            TextCompanyName.setVisible(Boolean.valueOf(elementCompanyName.getElementsByTagName("Visible").item(0).getTextContent()));
            TextCompanyName.setLayoutX(Double.parseDouble(elementCompanyName.getElementsByTagName("LayoutX").item(0).getTextContent()));
            TextCompanyName.setLayoutY(Double.parseDouble(elementCompanyName.getElementsByTagName("LayoutY").item(0).getTextContent()));
            
            
            //CompanyPlace
            Element elementCompanyPlace = (Element)elementTraiteClt.getElementsByTagName("CompanyPlace").item(0);
            TextCompanyPlace.setVisible(Boolean.valueOf(elementCompanyPlace.getElementsByTagName("Visible").item(0).getTextContent()));
            TextCompanyPlace.setLayoutX(Double.parseDouble(elementCompanyPlace.getElementsByTagName("LayoutX").item(0).getTextContent()));
            TextCompanyPlace.setLayoutY(Double.parseDouble(elementCompanyPlace.getElementsByTagName("LayoutY").item(0).getTextContent()));
           
            //CompanyAddress
            Element elementCompanyAddress = (Element)elementTraiteClt.getElementsByTagName("CompanyAddress").item(0);
            TextFlowCompanyAddress.setVisible(Boolean.valueOf(elementCompanyAddress.getElementsByTagName("Visible").item(0).getTextContent()));
            TextFlowCompanyAddress.setLayoutX(Double.parseDouble(elementCompanyAddress.getElementsByTagName("LayoutX").item(0).getTextContent()));
            TextFlowCompanyAddress.setLayoutY(Double.parseDouble(elementCompanyAddress.getElementsByTagName("LayoutY").item(0).getTextContent()));
            TextFlowCompanyAddress.setPrefHeight(Double.parseDouble(elementCompanyAddress.getElementsByTagName("prefHeight").item(0).getTextContent()));
            TextFlowCompanyAddress.setPrefWidth(Double.parseDouble(elementCompanyAddress.getElementsByTagName("prefWidth").item(0).getTextContent()));
            
            //CompanySIREN
            Element elementCompanySIREN = (Element)elementTraiteClt.getElementsByTagName("CompanySIREN").item(0);
            TextCompanySIREN.setVisible(Boolean.valueOf(elementCompanySIREN.getElementsByTagName("Visible").item(0).getTextContent()));
            TextCompanySIREN.setLayoutX(Double.parseDouble(elementCompanySIREN.getElementsByTagName("LayoutX").item(0).getTextContent()));
            TextCompanySIREN.setLayoutY(Double.parseDouble(elementCompanySIREN.getElementsByTagName("LayoutY").item(0).getTextContent()));
            
            //NomCompletClt
            Element elementNomCompletClt = (Element)elementTraiteClt.getElementsByTagName("NomCompletClt").item(0);
            TextNomCompletClt.setVisible(Boolean.valueOf(elementNomCompletClt.getElementsByTagName("Visible").item(0).getTextContent()));
            TextNomCompletClt.setLayoutX(Double.parseDouble(elementNomCompletClt.getElementsByTagName("LayoutX").item(0).getTextContent()));
            TextNomCompletClt.setLayoutY(Double.parseDouble(elementNomCompletClt.getElementsByTagName("LayoutY").item(0).getTextContent()));
            
            //CIN
            Element elementCIN = (Element)elementTraiteClt.getElementsByTagName("CIN").item(0);
            TextCIN.setVisible(Boolean.valueOf(elementCIN.getElementsByTagName("Visible").item(0).getTextContent()));
            TextCIN.setLayoutX(Double.parseDouble(elementCIN.getElementsByTagName("LayoutX").item(0).getTextContent()));
            TextCIN.setLayoutY(Double.parseDouble(elementCIN.getElementsByTagName("LayoutY").item(0).getTextContent()));
            
            //Bank
            Element elementBank = (Element)elementTraiteClt.getElementsByTagName("Bank").item(0);
            TextFlowBank.setVisible(Boolean.valueOf(elementBank.getElementsByTagName("Visible").item(0).getTextContent()));
            TextFlowBank.setLayoutX(Double.parseDouble(elementBank.getElementsByTagName("LayoutX").item(0).getTextContent()));
            TextFlowBank.setLayoutY(Double.parseDouble(elementBank.getElementsByTagName("LayoutY").item(0).getTextContent()));
            TextFlowBank.setPrefHeight(Double.parseDouble(elementBank.getElementsByTagName("prefHeight").item(0).getTextContent()));
            TextFlowBank.prefWidth(Double.parseDouble(elementBank.getElementsByTagName("prefWidth").item(0).getTextContent()));
            
            //AmountLetter
            Element elementAmountLetter = (Element)elementTraiteClt.getElementsByTagName("AmountLetter").item(0);
            TextAmountLetter.setVisible(Boolean.valueOf(elementAmountLetter.getElementsByTagName("Visible").item(0).getTextContent()));
            TextAmountLetter.setLayoutX(Double.parseDouble(elementAmountLetter.getElementsByTagName("LayoutX").item(0).getTextContent()));
            TextAmountLetter.setLayoutY(Double.parseDouble(elementAmountLetter.getElementsByTagName("LayoutY").item(0).getTextContent()));
            
            //AmountFigures
            Element elementAmountFigures = (Element)elementTraiteClt.getElementsByTagName("AmountFigures").item(0);
            TextAmountFigures.setVisible(Boolean.valueOf(elementAmountFigures.getElementsByTagName("Visible").item(0).getTextContent()));
            TextAmountFigures.setLayoutX(Double.parseDouble(elementAmountFigures.getElementsByTagName("LayoutX").item(0).getTextContent()));
            TextAmountFigures.setLayoutY(Double.parseDouble(elementAmountFigures.getElementsByTagName("LayoutY").item(0).getTextContent()));
            
            //DateDeadline
            Element elementDateDeadline = (Element)elementTraiteClt.getElementsByTagName("DateDeadline").item(0);
            TextDateDeadline.setVisible(Boolean.valueOf(elementDateDeadline.getElementsByTagName("Visible").item(0).getTextContent()));
            TextDateDeadline.setLayoutX(Double.parseDouble(elementDateDeadline.getElementsByTagName("LayoutX").item(0).getTextContent()));
            TextDateDeadline.setLayoutY(Double.parseDouble(elementDateDeadline.getElementsByTagName("LayoutY").item(0).getTextContent()));
            

        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }
        
        TextCompanyName.setText(ParametreSystem.CompanyName);
        
        TextCompanyPlace.setText(ParametreSystem.CompanyPlace);
        
        TextCompanySIREN.setText(ParametreSystem.CompanySIREN);
        
        TextCompanyAddress.setText(ParametreSystem.CompanyAddress);
        
        TextBank.setText(ParametreSystem.Bank);
        
    }    
    
    public void SetData(String AmountLetter, 
                        String AmountFigures,
                        String DateDeadline,
                        String NomCompletClt,
                        String CIN) 
    {
        TextNomCompletClt.setText(NomCompletClt);
        TextCIN.setText(CIN);
        TextAmountLetter.setText(AmountLetter);
        TextAmountFigures.setText(AmountFigures);
        TextDateDeadline.setText(DateDeadline);   
    }  
    
}
