/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers.Categorie;

import Controllers.Traitement.MyWindow;
import Models.Categorie.Categorie;
import Models.Categorie.CategorieDB;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 * @author PC-Maher
 */
public class AjouterCategorieController implements Initializable {

    public Node nodeFxml ;
    
    @FXML public AnchorPane AnchorPrincipal ;
    
    @FXML public TextField NomCategorie ;
    @FXML private Text erreurCategorie ;
    @FXML public ToggleGroup unite ;
    @FXML private RadioButton piece ;
    @FXML private RadioButton  kilo;
    @FXML private RadioButton  metre;
    @FXML private RadioButton  litre;
    @FXML public  Button ButtonAjouterCategory ;
    
    Categorie categorie = new Categorie();
    CategorieDB dbcategorie = new CategorieDB();
    
    
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        piece.setUserData("0");
        kilo.setUserData("1");
        metre.setUserData("2");
        litre.setUserData("3");
    }    
    
    
    public  AjouterCategorieController(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/Views/Categorie/AjouterCategorie.fxml"));
            fxmlLoader.setController(this);
            nodeFxml = (Node) fxmlLoader.load();
        } catch (IOException ex) {
            Logger.getLogger(AjouterCategorieController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
    public String AjouterCategorieTraitement(){
        if(NomCategorie.getText().length()==0){         //catégorie est vide
            erreurCategorie.setText("catégorie vide");
            NomCategorie.setStyle("-fx-border-color:#f20606;");
        }
        else if(dbcategorie.verifyCategorie(NomCategorie.getText())){
            erreurCategorie.setText("catégorie existe déjà");
            NomCategorie.setStyle("-fx-border-color:#f20606;");
        }
        else{
            erreurCategorie.setText("");
            NomCategorie.setStyle("-fx-border-color: transparent;");
            categorie.setNomcategorie(NomCategorie.getText());
            categorie.setUnitemesure(Integer.parseInt(unite.getSelectedToggle().getUserData().toString()));
            if(dbcategorie.addCategorie(categorie)){
                AnchorPrincipal.setVisible(false);
                return NomCategorie.getText();
            }
        }
        return "";
    }
    
    
    @FXML
    private void AnnulerCategorieButtonAction(ActionEvent event) throws IOException {
       AnchorPrincipal.setVisible(false);
    }
    
    
    public void Show()
    {
        StackPane Sp = new StackPane();
            
        Scene scene = new Scene(Sp);
        
        Sp.setPrefSize((double)MyWindow.PrincipalContentWidth, (double)MyWindow.PrincipalContentHeight);
        
        Stage stage = MyWindow.myStage;
        
        Sp.getChildren().add(MyWindow.myParent);
        Sp.getChildren().add(nodeFxml);
            
        stage.setScene(scene);
        stage.show();
        
    }
    
}
