/*
 * 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.Reglement;

import Controllers.Traitement.Adaptateur;
import Models.FactureFrs.FactureFrs;
import Models.Reglement.ChequeFrs;
import Models.Reglement.Reglement;
import Models.Reglement.ReglementDB;
import Models.Reglement.TraiteFrs;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.util.Callback;

/**
 * FXML Controller class
 * @author Maher
 */
public class ReglementAjouter3Controller implements Initializable {

    @FXML public ProgressIndicator ProgressReglement;
    @FXML private AnchorPane PaneDetailTraite;
    private Service<Void> ThreadReglement;
    
    @FXML private Text TextCodeReglement;
    @FXML private Text TextTypePaiement;
    @FXML private Text TextModePaiement;
    @FXML private Text TextDateCreation;
    @FXML private Text TextTotalReglement;
    @FXML private Text TextProfile;
    
    @FXML private Text TextCodeFrs;
    @FXML private Text TextNomFrs;
    @FXML private Text TextTele1Frs;
    @FXML private Text TextTele2Frs;
    
    @FXML private GridPane GridPaneCheque;
    @FXML private GridPane GridPaneCarte;
    
    @FXML private Text TextCartNomBanque;
    @FXML private Text TextCartNumeroCart;
    @FXML private Text TextCartNumeroTransaction;
    @FXML private Text TextCartDateHeurTransaction;
    
    @FXML private Text TextChequeNomBanque;
    @FXML private Text TextChequeNumero;
    @FXML private Text TextChequeNomComple;
    @FXML private Text TextChequeDate;
    
    @FXML private Button ButtonImprimer;
    
    @FXML private TableView<ChequeFrs> TableViewListeCheque ;
    @FXML private TableColumn<ChequeFrs ,String> TabColDateCheque;
    @FXML private TableColumn<ChequeFrs ,String> TabColNumeroCheque;
    @FXML private TableColumn<ChequeFrs ,String> TabColBanqueCheque;
    @FXML private TableColumn<ChequeFrs ,String> TabColMontantCheque;
    
    @FXML private TableView<TraiteFrs> TableViewListeTraite ;
    @FXML private TableColumn<TraiteFrs ,String> TabColDateTraite;
    @FXML private TableColumn<TraiteFrs ,String> TabColMontantTraite;
    
    
    
    @FXML private TableView<FactureFrs> TableViewFactureFrs;
    @FXML private TableColumn<FactureFrs ,String> TabColNumeroFact;
    @FXML private TableColumn<FactureFrs ,String> TabColProfile;
    @FXML private TableColumn<FactureFrs ,String> TabColDate;
    @FXML private TableColumn<FactureFrs ,String> TabColHeur;
    @FXML private TableColumn<FactureFrs ,String> TabColLocalReception;
    @FXML private TableColumn<FactureFrs ,String> TabColNetPayer;
    
    Reglement reglement = new Reglement();
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        TabColMontantTraite.setStyle( "-fx-alignment: CENTER;");
        TabColMontantTraite.getStyleClass().add("Center");
        TabColMontantTraite.setCellValueFactory(new PropertyValueFactory<TraiteFrs, String>("Montant"));
        
        TabColDateTraite.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColDateTraite.getStyleClass().add("Center");
        TabColDateTraite.setCellValueFactory(new PropertyValueFactory<TraiteFrs, String>("DatePaiement"));
        
        
        TabColNumeroCheque.setStyle( "-fx-alignment: CENTER;");
        TabColNumeroCheque.getStyleClass().add("Center");
        TabColNumeroCheque.setCellValueFactory(new PropertyValueFactory<ChequeFrs, String>("NumeroChequeFrs"));
        
        TabColBanqueCheque.setStyle( "-fx-alignment: CENTER;");
        TabColBanqueCheque.getStyleClass().add("Center");
        TabColBanqueCheque.setCellValueFactory(new PropertyValueFactory<ChequeFrs, String>("BanqueChequeFrs"));
        
        TabColDateCheque.setStyle( "-fx-alignment: CENTER;");
        TabColDateCheque.getStyleClass().add("Center");
        TabColDateCheque.setCellValueFactory(new PropertyValueFactory<ChequeFrs, String>("DatePaiement"));
        
        TabColMontantCheque.setStyle( "-fx-alignment: CENTER;");
        TabColMontantCheque.getStyleClass().add("Center");
        TabColMontantCheque.setCellValueFactory(new PropertyValueFactory<ChequeFrs, String>("MontantChequeFrs"));
        
        TabColNumeroFact.setCellValueFactory(new PropertyValueFactory<FactureFrs, String>("Numero"));
        
        TabColProfile.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColProfile.getStyleClass().add("Center");
        TabColProfile.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<FactureFrs,String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<FactureFrs, String> data) {
                return new SimpleStringProperty(data.getValue().getProfile().getNom()+" "+data.getValue().getProfile().getPrenom());
            }
        });
    
        TabColDate.setCellValueFactory(new PropertyValueFactory<FactureFrs, String>("DateCreation"));
        TabColDate.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColDate.getStyleClass().add("Center");
        
        TabColHeur.setCellValueFactory(new PropertyValueFactory<FactureFrs, String>("HeurCreation"));
        TabColHeur.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColHeur.getStyleClass().add("Center");
        
        TabColLocalReception.setCellValueFactory(new PropertyValueFactory<FactureFrs, String>("Local"));
        TabColLocalReception.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColLocalReception.getStyleClass().add("Center");
        
        TabColNetPayer.setCellValueFactory(new PropertyValueFactory<FactureFrs, String>("NetAPayer"));
        TabColNetPayer.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColNetPayer.getStyleClass().add("Center");
    }    
    
    public void setDataReglement(Reglement regl){
        reglement = regl;
        LanchSaveReglement();
    }
    
    public void setReglement(){
        
        TextCodeReglement.setText(reglement.getNumero());
        TextTypePaiement.setText(reglement.getTypePaiementString());
        TextModePaiement.setText(reglement.getModePaiementString());
        TextDateCreation.setText(reglement.getDateTimeCreationString());
        TextTotalReglement.setText(Adaptateur.StringToStringEspaceCurrency(reglement.getMontant()));
        
        TextProfile.setText(reglement.getProfile().getPrenom()+" "+reglement.getProfile().getNom());
        
        TextCodeFrs.setText(reglement.getFournisseur().getCode());
        TextNomFrs.setText(reglement.getFournisseur().getNom());
        TextTele1Frs.setText(reglement.getFournisseur().getTele1());
        TextTele2Frs.setText(reglement.getFournisseur().getTele2());
        
        TableViewFactureFrs.setItems(reglement.getListFacture());
        // 0 Comptant 
        if(reglement.getTypePaiement().equals("0")){
            
            //0 espèce
            if(reglement.getModePaiement().equals("0")){
            
            //1 Chéque
            }else if(reglement.getModePaiement().equals("1")){
                GridPaneCheque.setVisible(true);
                TextChequeNomBanque.setText(reglement.getCheque().getBanqueChequeFrs());
                TextChequeNumero.setText(reglement.getCheque().getNumeroChequeFrs());
                TextChequeNomComple.setText(reglement.getCheque().getNomCompletChequeFrs());
                TextChequeDate.setText(reglement.getCheque().getDateCreation());
                
            //2 Carte électronique
            }else if(reglement.getModePaiement().equals("2")){
                GridPaneCarte.setVisible(true);
                TextCartNomBanque.setText(reglement.getCarte().getBanque());
                TextCartNumeroCart.setText(reglement.getCarte().getNumeroCarte());
                TextCartNumeroTransaction.setText(reglement.getCarte().getNumeroTransaction());
                TextCartDateHeurTransaction.setText(reglement.getCarte().getDateTransaction()+" "+reglement.getCarte().getHeurTransaction());
            }

            
        //1 Facilité
        }else if(reglement.getTypePaiement().equals("1")){
            
            //1 Chéque
            if(reglement.getModePaiement().equals("1")){
                TableViewListeCheque.setVisible(true);
                ObservableList<ChequeFrs>  NewListCheque =  FXCollections.observableArrayList();
                for(ChequeFrs Cheque : reglement.getListCheque()) {
                    String CodeChequeFrs = Cheque.getCodeChequeFrs();
                    String NumeroChequeFrs = Cheque.getNumeroChequeFrs();
                    String BanqueChequeFrs = Cheque.getBanqueChequeFrs();
                    String MontantChequeFrs = Adaptateur.StringDeleZero(Cheque.getMontantChequeFrs());
                    String NomCompletChequeFrs = Cheque.getNomCompletChequeFrs(); 
                    String DatePaiement = Adaptateur.NormalDateFormat(Cheque.getDatePaiement());
                    String DateCreation = Adaptateur.NormalDateTimeFormat(Cheque.getDateCreation());
                    NewListCheque.add(new ChequeFrs(CodeChequeFrs, NumeroChequeFrs, BanqueChequeFrs, MontantChequeFrs, NomCompletChequeFrs, DatePaiement, DateCreation));
                }
                TableViewListeCheque.setItems(NewListCheque);
                
                
            //3 Traite
            }else if(reglement.getModePaiement().equals("3")){
                TableViewListeTraite.setVisible(true);
                ButtonImprimer.setVisible(true);
                TableViewListeTraite.setItems(reglement.getListTraiteString());
            }
        }
    
    }
    
    public void LanchSaveReglement(){
        ThreadReglement = new Service<Void>(){
            @Override
            protected Task<Void> createTask(){
                return new Task<Void>(){
                    @Override
                    protected Void call() throws Exception{
                        ReglementDB reglementDB = new ReglementDB();
                        reglement = reglementDB.setReglementDB(reglement);
                        reglement = reglementDB.getReglementDB(reglement.getNumero());
                        return null;
                    }
                };
            }
        };
        ThreadReglement.setOnSucceeded(new EventHandler<WorkerStateEvent>(){
            @Override
            public void handle(WorkerStateEvent event){
                ProgressReglement.setVisible(false);
                setReglement();
                PaneDetailTraite.setVisible(true);
            }
        });
        ThreadReglement.start();        
    }
    
    
    
}
