package Controllers;

import Controllers.Traitement.MyWindow;
import Models.User.User;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Accordion;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
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;



public class PrincipalController implements Initializable{
    
    @FXML public AnchorPane window ;
    @FXML public AnchorPane Content ;
    @FXML public Pane TitrePane;       
    @FXML public Label UsernomprenomBlad;
    @FXML public Label LabelDefaultLocal;
    
    @FXML public ImageView ImageViewLogoPrincipal;
    @FXML public ImageView ImageViewProfile;
    
    @FXML public Accordion accord;
    @FXML public TitledPane TitledPaneAccueil;
    @FXML public TitledPane TitledPaneAchat;
    @FXML public TitledPane TitledPaneVente;
    @FXML public TitledPane TitledPaneStock;
    @FXML public TitledPane TitledPaneDiver;
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
        setImageLogo();
        
        UsernomprenomBlad.setText(User.nom+" "+User.prenom);
        
        LabelDefaultLocal.setText(User.UserLocal);
        
        ImageViewProfile.setOnMouseClicked(new EventHandler<MouseEvent>() {
            public void handle(MouseEvent me) {
                try {
                    window.getChildren().clear();
                    window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/User/Profile.fxml")));
                } catch (IOException ex) {
                    Logger.getLogger(PrincipalController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        
        
        /**********************Accueil************************/
        try {    
            window.getChildren().clear();
            window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Home/Dashboard.fxml")));
        } catch (IOException ex) {
            Logger.getLogger(PrincipalController.class.getName()).log(Level.SEVERE, null, ex);
        }
        accord.setExpandedPane(TitledPaneAccueil);
        
        
        
        /********************get Width and Height Content **************************/
        
        Content.widthProperty().addListener(new ChangeListener<Number>() {
            @Override 
            public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneWidth, Number newSceneWidth) {
                MyWindow.PrincipalContentWidth = newSceneWidth;
            }
        });
        
        Content.heightProperty().addListener(new ChangeListener<Number>() {
            @Override 
            public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneHeight, Number newSceneHeight) {
                MyWindow.PrincipalContentHeight = newSceneHeight;
            }
        });
        
    }

    
    public void DefaultSelectedTitledPane(Integer number_titled_Pane){
        if(number_titled_Pane == 1){
            accord.setExpandedPane(TitledPaneAccueil);
        }else if(number_titled_Pane == 2){
            accord.setExpandedPane(TitledPaneAchat);
        }else if(number_titled_Pane == 3){
            accord.setExpandedPane(TitledPaneVente);
        }else if(number_titled_Pane == 4){
            accord.setExpandedPane(TitledPaneStock);
        }else if(number_titled_Pane == 5){
            accord.setExpandedPane(TitledPaneDiver);
        }
    }
    
    /*********************************Accueil****************************************/
    @FXML
    private void DashbordButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Home/Dashboard.fxml")));
    }
    
    @FXML
    private void AideButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Home/Aide.fxml")));
    }
    
    @FXML
    private void ContactButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Home/Contact.fxml")));
    }
    
    @FXML
    private void LogicielButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Home/Logiciel.fxml")));
    }
    
    @FXML
    private void MentionsButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Home/Mentions.fxml")));
    }
    
    /*********************************Pour l'Achat************************************/
    @FXML
    private void FournisseurButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Fournisseur/FournisseurGestion.fxml")));
    }
    
    @FXML
    private void BonReceptionButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/BonReceptionFrs/BonReceptionGestionFrs.fxml")));
    }
    
    @FXML
    private void FactureFrsButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/FactureFrs/FactureFrsGestion.fxml")));
    }
    
    @FXML
    private void ReglementButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Reglement/ReglementGestion.fxml")));
    }
    /*********************************Pour la Vente***********************************/
    
    @FXML
    private void VenteRapideCltButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/VenteRapideClt/VenteRapideClt.fxml")));
    }
    
    @FXML
    private void PasserCommandeCltButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/CommandeClt/CommandeCltGestion.fxml")));
    }
    
    @FXML
    private void BonLivraisonCltButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/BonLivraisonClt/BonLivraisonCltGestion.fxml")));
    }
    
    @FXML
    private void FactureCltButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/FactureClt/FactureCltGestion.fxml")));
    }
    
    @FXML
    private void MenuClientButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Client/MenuClient.fxml")));
    }
    
    @FXML
    private void DevisCltButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Devis/DevisCltGestion.fxml")));
    }
    
    @FXML
    private void TraiteCltButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/TraiteClt/TraiteCltGestion.fxml")));
    }
    
    
            
    /*********************************Pour Produit**********************************/
    @FXML
    private void AjouterProduitButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Produit/AjouterProduit.fxml")));
    }
    
    
    @FXML
    private void GestionProduitButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Produit/GestionProduit.fxml")));
    }
    
    
    
    
    /*********************************Pour l'utilisateur***********************************/
    @FXML
    private void AddUserButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/User/AddUser.fxml")));
       
    }
    
    @FXML
    private void GestionUtilisateurButtonAction(ActionEvent event) throws IOException {
        window.getChildren().clear();
        window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/User/GestionUtilisateur.fxml")));
        
    }
    
    
    
    /********************************* Stock ***********************************/
    
   @FXML
    private void BondeSortieButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Stock/BondeSortie.fxml")));
    }
    
    @FXML
    private void BondeEntrerButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Stock/BondeEntrer.fxml")));
    }
    
    @FXML
    private void EtatStockButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Stock/EtatStockCategorie.fxml")));
    }
    
    /********************************* Diver ***********************************/
    
    @FXML
    private void ChequeCltButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/ChequeClt/ChequeCltGestion.fxml")));
    }
    
    @FXML
    private void CaisseEntreButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Caisse/CaisseEntre.fxml")));
    }
    
    @FXML
    private void CaisseSortieButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Caisse/CaisseSortie.fxml")));
    }
    
    
    @FXML
    private void FraisButtonAction(ActionEvent event) throws IOException {
       window.getChildren().clear();
       window.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Caisse/FraisGestion.fxml")));
    }
    
    
    private void setImageLogo()
    {
        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\logo.xml
            Document doc = db.parse(new File("./conf/logo.xml"));

            doc.getDocumentElement().normalize();

            Element elementCNX  = (Element) doc.getElementsByTagName("Principal").item(0);
            
            String pathLogo = elementCNX.getElementsByTagName("PathImage").item(0).getTextContent();
            FileInputStream fis = new FileInputStream(pathLogo);
            ImageViewLogoPrincipal.setImage(new Image(fis));
            
            ImageViewLogoPrincipal.setFitWidth(Double.parseDouble(elementCNX.getElementsByTagName("FitWidth").item(0).getTextContent()));
            ImageViewLogoPrincipal.setFitHeight(Double.parseDouble(elementCNX.getElementsByTagName("FitHeight").item(0).getTextContent()));
            ImageViewLogoPrincipal.setLayoutX(Double.parseDouble(elementCNX.getElementsByTagName("LayoutX").item(0).getTextContent()));
            ImageViewLogoPrincipal.setLayoutY(Double.parseDouble(elementCNX.getElementsByTagName("LayoutY").item(0).getTextContent()));
        } 
        catch (ParserConfigurationException | SAXException | IOException e) {
            System.out.println("error AuthentificationController/setImageLogo "+e.getMessage());
        }
    }
}
