
package Controllers.Caisse;

import Controllers.Traitement.contro;
import Models.Caisse.Frais;
import Models.Caisse.FraisDB;
import Models.User.User;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import javafx.animation.FadeTransition;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.util.Duration;

/**
 * FXML Controller class
 * @author Maher Ben Tili
 */
public class FraisAjouterController implements Initializable {

    Logger logger = Logger.getLogger(FraisAjouterController.class.getName());
    
    @FXML private AnchorPane PaneAjouterFrais ;
    @FXML private AnchorPane AnchorPaneFrais ;
    @FXML private StackPane StackPaneSucces;
    
    @FXML private ProgressIndicator ProgrssLoading;
    @FXML private Button ButtonAjouterFrais;
    
    @FXML private TextField TextFieldMontant;
    
    @FXML private RadioButton RadioEspace;
    
    @FXML private RadioButton RadioCheque;
    @FXML private GridPane GridPaneCheque;
    @FXML private TextField TextFieldNumeroCheque;
    @FXML private TextField TextFieldBanqueCheque;
    @FXML private DatePicker DatePickerDateCheque;
    
    @FXML private RadioButton RadioCarte;
    @FXML private GridPane GridPaneCarte;
    @FXML private TextField TextFieldNumeroTransation;
    @FXML private TextField TextFieldBanqueCarte;
    
            
    @FXML private TextField TextFieldObject;
    @FXML private TextField TextFieldPersonne;
    @FXML private TextArea  TextAreaContext ;
    
    @FXML private Text ErreurMontant;
    @FXML private Text ErreurNumeroCheque;
    @FXML private Text ErreurBanqueCheque;
    @FXML private Text ErreurDateCheque;
    
    @FXML private Text ErreurTransaction;
    @FXML private Text ErreurBanqueCart;
    @FXML private Text ErreurObject;
    
    private Service<Void> ThreadFrais;
    
    Frais frais = new Frais();
    FraisDB fraisDB = new FraisDB();
    contro ctl= new contro() ;
    
    
    
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        frais.setPaiement("0");
        this.TypePaiement();
    }
    
    @FXML
    private void AjouterFraisButtonAction(ActionEvent event) throws IOException {
        if(this.ContoleFrais()){
            ButtonAjouterFrais.setDisable(true);
            ButtonAjouterFrais.setText("");
            ThreadFrais = new Service<Void>(){
                @Override
                protected Task<Void> createTask(){
                    return new Task<Void>(){
                        @Override
                        protected Void call() throws Exception{
                            ProgrssLoading.setVisible(true);
                            frais = fraisDB.setFrais(frais);
                            return null;
                        }
                    };
                }
            };
            ThreadFrais.setOnSucceeded(new EventHandler<WorkerStateEvent>(){
                @Override
                public void handle(WorkerStateEvent event){
                    FadeTransition ft = new FadeTransition(Duration.millis(400), AnchorPaneFrais);
                    ft.setFromValue(1.0);
                    ft.setToValue(0.0);
                    ft.play();
                    ft.setOnFinished(new EventHandler<ActionEvent>() {
                        //message de succés    
                        @Override
                        public void handle(ActionEvent event) {
                            StackPaneSucces.setVisible(true);
                        }
                    });
                }
            });
            ThreadFrais.start(); 
        }
    }
    
    private Boolean ContoleFrais(){
        
        Boolean result = false;
        
        if( ctl.ContrReelNotNull(TextFieldMontant,ErreurMontant) && 
            ctl.ContrNull(TextFieldObject,ErreurObject) ){
            frais.setMontant(TextFieldMontant.getText());       
            frais.setObject(TextFieldObject.getText());
            frais.setPersonne(TextFieldPersonne.getText());
            
            frais.setDateCreation(LocalDateTime.now().toString());
            frais.setProfile(User.idprofile+"");
            frais.setLocalNom(User.UserLocal);
            
            frais.setContext(TextAreaContext.getText());
            
            if( frais.getPaiement().equals("1") && 
                ctl.ContrNull(TextFieldNumeroCheque, ErreurNumeroCheque) && 
                ctl.ContrNull(TextFieldBanqueCheque, ErreurBanqueCheque) &&  
                ctl.ContrNullDatePicker(DatePickerDateCheque, ErreurDateCheque)){
                frais.setNumero(TextFieldNumeroCheque.getText());
                frais.setBanque(TextFieldBanqueCheque.getText());
                frais.setDateCheque(DatePickerDateCheque.getValue().toString());
                result = true;
            }else if(frais.getPaiement().equals("2") &&
                    ctl.ContrNull(TextFieldNumeroTransation, ErreurTransaction) && 
                    ctl.ContrNull(TextFieldBanqueCarte, ErreurBanqueCart) ){
                frais.setBanque(TextFieldBanqueCarte.getText());
                frais.setNumero(TextFieldNumeroTransation.getText());
                result = true;
            }else if(frais.getPaiement().equals("0")){
                result = true;
            }
        }
        return result;
    }
    
    private void TypePaiement(){
        //0 :espèce   1:Chèque    2 :carte bancaire
        RadioEspace.setOnMouseClicked(new EventHandler<MouseEvent>() {
            public void handle(MouseEvent me) {
                GridPaneCheque.setVisible(false);
                GridPaneCarte.setVisible(false);
                frais.setPaiement("0");
            }
        });
        RadioCheque.setOnMouseClicked(new EventHandler<MouseEvent>() {
            public void handle(MouseEvent me) {
                GridPaneCheque.setVisible(true);
                GridPaneCarte.setVisible(false);
                frais.setPaiement("1");
            }
        });
        RadioCarte.setOnMouseClicked(new EventHandler<MouseEvent>() {
            public void handle(MouseEvent me) {
                GridPaneCheque.setVisible(false);
                GridPaneCarte.setVisible(true);
                frais.setPaiement("2");
            }
        });
    }
    
    //ajouter un nouvelle catégorie
    @FXML
    private void AfficherAjouterFraisButtonAction(ActionEvent event) throws IOException 
    {
        FadeTransition ft = new FadeTransition(Duration.millis(400), StackPaneSucces);
        ft.setFromValue(1.0);
        ft.setToValue(0.0);
        ft.setOnFinished(new EventHandler<ActionEvent>() {
            //message de succés    
            @Override
            public void handle(ActionEvent event) {
                try {
                    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/Views/Caisse/FraisAjouter.fxml"));
                    Parent NodeDetail = (Parent)fxmlLoader.load();
                    PaneAjouterFrais.getChildren().add(NodeDetail);
                } catch (IOException ex) {
                    logger.error("FraisAjouterController  : AfficherAjouterFraisButtonAction : FraisAjouter.fxml : " + ex.getMessage());
                }
                
            }
        });
        ft.play();
    }
    
}
