package Controllers.VenteRapideClt;

import Controllers.Traitement.Adaptateur;
import Controllers.Traitement.MyWindow;
import Models.Produit.ListeProduit;
import Models.User.Profile;
import Models.User.ProfileDB;
import Models.VenteRapideClt.VenteSansFacture;
import Models.VenteRapideClt.VenteSansFactureDB;
import java.io.IOException;
import org.apache.log4j.Logger;
import javafx.collections.ObservableList;
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.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * FXML Controller class
 * @author Maher Ben Tili
 */
public class VenteSansFactureCltDetailController{

    Logger logger = Logger.getLogger(VenteSansFactureCltDetailController.class.getName());
    
    @FXML private TableView<ListeProduit> TableViewListeProduit ;
    @FXML private TableColumn<ListeProduit ,String> TabColReference;
    @FXML private TableColumn<ListeProduit ,String> TabColDesignation;
    @FXML private TableColumn<ListeProduit ,String> TabColQuantite ;
    @FXML private TableColumn<ListeProduit ,String> TabColPrixHT;
    @FXML private TableColumn<ListeProduit ,String> TabColRemise;
    @FXML private TableColumn<ListeProduit ,String> TabColTotalHT;
    @FXML private TableColumn<ListeProduit ,String> TabColTVA;
    @FXML private TableColumn<ListeProduit ,String> TabColTotalTTC;
    
    @FXML public AnchorPane AnchorPrincipal ;    
    @FXML public AnchorPane AnchorSecondaire ;
    @FXML private ProgressIndicator ProgressVenteSansFact ;
    
    @FXML public Text TextIdVenteSansFacture;
    
    @FXML private Text TextVenteAnnuler;
    
    @FXML private Text TextLocale;
    @FXML private Text TextNomPrenomProfile;
    @FXML private Text TextDateCreation;
    @FXML private Text TextClient;
    
    @FXML private Text TextTypePaiement;
    @FXML private Text TextMontant;
    
    @FXML private GridPane GridPaneCheque;
    @FXML private Text TextNumeroCheque;
    @FXML private Text TextNomBanqueCheque ;
    @FXML private Text TextDateCheque;
    @FXML private Text TextNomComplet;
    
    @FXML private GridPane GridPaneCarte;
    @FXML private Text TextNumeroTransationCarte;
    @FXML private Text TextNomBanqueCarte;
    @FXML private Text TextNumeroCarte;
    
    private Service<Void> ThreadVenteSansFact;
    public Node nodeFxml;
    public String  IdVenteSansFacture;
    
    VenteSansFacture VenteSansFact = new VenteSansFacture();
    Profile profile  = new Profile();
    
    public  VenteSansFactureCltDetailController(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/Views/VenteRapideClt/VenteSansFactureCltDetail.fxml"));
            fxmlLoader.setController(this);
            nodeFxml = (Node) fxmlLoader.load();
        } catch (IOException ex) {
            logger.error("VenteSansFactureCltDetailController  : VenteSansFactureCltDetailController :" + ex.getMessage());
        }
    }
    
    public void SetIdVenteSansFacture(String id_bon_livraison){
        IdVenteSansFacture = id_bon_livraison;
        
        TableViewListeProduit.setEditable(true);
        TabColReference.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("reference"));
        TabColDesignation.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("designation"));
    
        TabColQuantite.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("quantite"));
        TabColQuantite.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColQuantite.getStyleClass().add("Center");
            
        TabColPrixHT.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("PrixHT"));
        TabColPrixHT.setStyle( "-fx-alignment: CENTER;");
        TabColPrixHT.getStyleClass().add("Center");
    
        TabColRemise.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("Remise"));
        TabColRemise.setStyle( "-fx-alignment: CENTER;");
        TabColRemise.getStyleClass().add("Center");
    
        TabColTotalHT.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("TotalHT"));
        TabColTotalHT.setStyle( "-fx-alignment: CENTER;");
        TabColTotalHT.getStyleClass().add("Center");
    
        TabColTVA.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("TVA"));
        TabColTVA.setStyle( "-fx-alignment: CENTER;");
        TabColTVA.getStyleClass().add("Center");
    
        TabColTotalTTC.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("TotalTTC"));
        TabColTotalTTC.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColTotalTTC.getStyleClass().add("Center");
    
        TabColReference.setCellFactory(TextFieldTableCell.<ListeProduit>forTableColumn());
        TabColQuantite.setCellFactory(TextFieldTableCell.<ListeProduit>forTableColumn());
        TabColRemise.setCellFactory(TextFieldTableCell.<ListeProduit>forTableColumn());
    
        LanchVenteSansFact();
    }
    
    public void LanchVenteSansFact(){
        ThreadVenteSansFact = new Service<Void>(){
            @Override
            protected Task<Void> createTask(){
                return new Task<Void>(){
                    @Override
                    protected Void call() throws Exception{
                        VenteSansFact = new VenteSansFactureDB().getVenteSansFacture(IdVenteSansFacture);
                        profile = new ProfileDB().getpro(VenteSansFact.getIdProfile());
                        return null;
                    }
                };
            }
	};
        ThreadVenteSansFact.setOnSucceeded(new EventHandler<WorkerStateEvent>(){
            @Override
            public void handle(WorkerStateEvent event){
                TextIdVenteSansFacture.setText(VenteSansFact.getIdVenteSansFacture());
                
                // 0:espèce | 1:chèque | 2:Carte bancaire
                if(VenteSansFact.getPaiement().equals("0")){
                    TextTypePaiement.setText("Espèce") ;
                    GridPaneCheque.setVisible(false);
                    GridPaneCarte.setVisible(false);
                }else if(VenteSansFact.getPaiement().equals("1")){
                    TextTypePaiement.setText("Chèque");
                    GridPaneCheque.setVisible(true);
                    GridPaneCarte.setVisible(false);
                    TextNumeroCheque.setText(VenteSansFact.getCheque().getNumeroChequeClt());
                    TextNomBanqueCheque.setText(VenteSansFact.getCheque().getBanqueChequeClt());
                    TextDateCheque.setText(Adaptateur.NormalDateFormat(VenteSansFact.getCheque().getDatePaiement()));
                    TextNomComplet.setText(VenteSansFact.getCheque().getNomCompletChequeClt());
                }else if(VenteSansFact.getPaiement().equals("2")){
                    TextTypePaiement.setText("Carte bancaire");
                    TextNumeroTransationCarte.setText(VenteSansFact.getCarteBancaire().getNumeroTransation());
                    TextNomBanqueCarte.setText(VenteSansFact.getCarteBancaire().getNomBanque());
                    TextNumeroCarte.setText(VenteSansFact.getCarteBancaire().getNumeroCarte());
                    GridPaneCheque.setVisible(false);
                    GridPaneCarte.setVisible(true);
                }
                
                if(VenteSansFact.getOperation() !=  null){
                    TextVenteAnnuler.setText("Vente Annuler Le: "+Adaptateur.NormalDateTimeFormat(VenteSansFact.getDateOperation()));
                }
                
                TextLocale.setText(VenteSansFact.getLocalNom());
                TextNomPrenomProfile.setText(profile.getNom()+" "+profile.getPrenom()) ;
                
                TextDateCreation.setText(VenteSansFact.getDateVente());
                
                TextClient.setText(VenteSansFact.getNomPrenomClient());
                
                TextMontant.setText(Adaptateur.StringToStringEspaceCurrency(VenteSansFact.getMontant()));
                
                ObservableList<ListeProduit> DataListeProduit = VenteSansFact.getListProduit();
                TableViewListeProduit.setItems(DataListeProduit);
                        
                ProgressVenteSansFact.setVisible(false);
                AnchorSecondaire.setVisible(true);
            }
        });
        ThreadVenteSansFact.start();
    }
    
    
    
    public void Show()
    {
        StackPane Sp = new StackPane();
            
        Scene scene = new Scene(Sp);
        
        Sp.setPrefSize((double)MyWindow.PrincipalContentWidth, (double)MyWindow.PrincipalContentHeight);
        
        Sp.getChildren().add(MyWindow.myParent);
        Sp.getChildren().add(nodeFxml);
        
        Stage stage = MyWindow.myStage;
        stage.setScene(scene);
        stage.show();
    }
    
    @FXML
    private void ExitButtonAction(ActionEvent event) throws IOException {
        AnchorPrincipal.setVisible(false);
    }
    
}
