package Controllers.BonReceptionFrs;

import Controllers.Traitement.Adaptateur;
import Models.BonReceptionFrs.BonReceptionDB;
import Models.BonReceptionFrs.BonReceptionFrs;
import Models.BonReceptionFrs.BonReceptionProduitList;
import Models.Fournisseur.Fournisseur;
import Models.User.Profile;
import java.net.URL;
import java.util.ResourceBundle;
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.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;

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

    @FXML private AnchorPane PaneBonReceptionDetailFrs ;
    
    @FXML public AnchorPane PaneSucessFrs ;
    
    @FXML public AnchorPane AnchorPaneDetailInformation;
    
    @FXML private AnchorPane AnchorPaneLoading;
    
    @FXML private Text TextNumero;
    @FXML private Text TextDateReception;
    @FXML private Text TextHeurReception;
    @FXML private Text TextLocalReception;
    @FXML private Text TextTransporteur;
    @FXML private Text TextProfile;
    @FXML private Text TextNomFrs;
    @FXML private Text TextSpecialiteFrs;
    @FXML private Text TextFormesFrs;
    @FXML private Text TextAdresseFrs;
    @FXML private Text TextTele1Frs;
    @FXML private Text TextTele2Frs;
    @FXML private Text TextNetPayer;
    
    @FXML public TableView<BonReceptionProduitList> TableViewListeProduit ;
    @FXML public TableColumn<BonReceptionProduitList ,String> TabColReference;
    @FXML public TableColumn<BonReceptionProduitList ,String> TabColDesignation;
    @FXML public TableColumn<BonReceptionProduitList ,String> TabColQuantite ;
    @FXML public TableColumn<BonReceptionProduitList ,String> TabColPrixHT;
    @FXML public TableColumn<BonReceptionProduitList ,String> TabColTotalHT;
    @FXML public TableColumn<BonReceptionProduitList ,String> TabColTVA;
    @FXML public TableColumn<BonReceptionProduitList ,String> TabColTotalTTC;
    
    private Service<Void> ThreadBonReceptionDetailFrs;
    
    BonReceptionFrs BonReception;
    
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        TabColReference.setCellValueFactory(new PropertyValueFactory<BonReceptionProduitList, String>("reference"));
        TabColDesignation.setCellValueFactory(new PropertyValueFactory<BonReceptionProduitList, String>("designation"));

        TabColQuantite.setCellValueFactory(new PropertyValueFactory<BonReceptionProduitList, String>("quantite"));
        TabColQuantite.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColQuantite.getStyleClass().add("Center");

        TabColPrixHT.setCellValueFactory(new PropertyValueFactory<BonReceptionProduitList, String>("PrixUnitaireHorsTaxe"));
        TabColPrixHT.setStyle( "-fx-alignment: CENTER;");
        TabColPrixHT.getStyleClass().add("Center");

        TabColTotalHT.setCellValueFactory(new PropertyValueFactory<BonReceptionProduitList, String>("PrixUnitaireHorsTaxenNet"));
        TabColTotalHT.setStyle( "-fx-alignment: CENTER;");
        TabColTotalHT.getStyleClass().add("Center");

        TabColTVA.setCellValueFactory(new PropertyValueFactory<BonReceptionProduitList, String>("Tva"));
        TabColTVA.setStyle( "-fx-alignment: CENTER;");
        TabColTVA.getStyleClass().add("Center");

        TabColTotalTTC.setCellValueFactory(new PropertyValueFactory<BonReceptionProduitList, String>("Montant"));
        TabColTotalTTC.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColTotalTTC.getStyleClass().add("Center");
    }    
    
    
    public void setDataBonReceptionDetailFrs(final String IdBonReceptionFrs){
        ThreadBonReceptionDetailFrs = new Service<Void>(){
            @Override
            protected Task<Void> createTask(){
                return new Task<Void>(){
                    @Override
                    protected Void call() throws Exception{
                        
                        BonReception =  new BonReceptionDB().getBonReceptionFrs(IdBonReceptionFrs, null, null);  
                        
                        return null;
                    }
                };
            }
	};
        ThreadBonReceptionDetailFrs.setOnSucceeded(new EventHandler<WorkerStateEvent>(){
            @Override
            public void handle(WorkerStateEvent event){
                TextNumero.setText(BonReception.getNumero());
                TextDateReception.setText(BonReception.getDate());
                TextHeurReception.setText(BonReception.getHeur());
                TextLocalReception.setText(BonReception.getLocal());
                TextTransporteur.setText(BonReception.getTransporteur());
                
                Profile profile = BonReception.getProfile();
                TextProfile.setText(profile.getNom()+" "+profile.getPrenom());
                
                Fournisseur Frs = BonReception.getFournisseur();
                TextNomFrs.setText(Frs.getNom());
                TextSpecialiteFrs.setText(Frs.getSpecialite());
                TextFormesFrs.setText(Frs.getFormes());
                TextAdresseFrs.setText(Frs.getAdresse());
                TextTele1Frs.setText(Frs.getTele1());
                TextTele2Frs.setText(Frs.getTele2());
                ObservableList<BonReceptionProduitList> BRProduitList = BonReception.getListProduit();
                float NetPayer = 0;
                for(int i=0; i<BRProduitList.size(); i++){
                    String TTC = BRProduitList.get(i).getMontant();
                    NetPayer += Adaptateur.StringToFloat(TTC);
                }
                TextNetPayer.setText(Adaptateur.FloatToStringEspaceCurrency(NetPayer));
                
                TableViewListeProduit.setItems(BRProduitList);
                
                PaneBonReceptionDetailFrs.setVisible(true);
                AnchorPaneLoading.setVisible(false);
            }
        });
        ThreadBonReceptionDetailFrs.start();
    }
    
    public void setContainsDetailInformation()
    {
        AnchorPane.setTopAnchor(AnchorPaneDetailInformation, 15.0);
    }
}
