package Controllers.DevisClt;

import Models.Produit.ListeProduit;
import Controllers.Traitement.Adaptateur;
import Models.Client.Client;
import Models.Client.ClientEntreprise;
import Models.Client.ClientPassager;
import Models.DevisClt.Devis;
import Models.DevisClt.DevisDB;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
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.print.PageRange;
import javafx.print.PrinterJob;
import javafx.scene.Node;
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 DevisDetailController implements Initializable {

    @FXML public AnchorPane AnchorPaneSucces;
    @FXML public AnchorPane AnchorPaneDevisDetail;
    @FXML public AnchorPane AnchorPaneLoadingDevisDetail;
    
    @FXML public Text TextSucces;
    @FXML public Text TextDescSucces;
    
    @FXML private Text DevisDateCreation;
    @FXML private Text DevisFinValidite;
    @FXML private Text DevisReglement;
    @FXML private Text DevisNetAPayer;
    @FXML private Text DevisTotalHorsTaxNet;
    @FXML private Text DevisCode;
    
    @FXML private Text DevisTypeClt;
    @FXML private Text DevisNom;
    @FXML private Text DevisPrenom;
    @FXML private Text DevisAdresse;
    @FXML private Text DevisTELE1;
    @FXML private Text DevisTELE2;
    
    @FXML private Text DevisTextPrenom ;
    @FXML private Text DevisTextMatricule ;
    
    @FXML private TableView<ListeProduit> TableViewListeProduitDevis ;
    @FXML private TableColumn<ListeProduit ,String> TabColDevisReference;
    @FXML private TableColumn<ListeProduit ,String> TabColDevisDesignaton;
    @FXML private TableColumn<ListeProduit ,String> TabColDevisQuantite ;
    @FXML private TableColumn<ListeProduit ,String> TabColDevisPrixHT;
    @FXML private TableColumn<ListeProduit ,String> TabColDevisRemise;
    @FXML private TableColumn<ListeProduit ,String> TabColDevisTotalHT;
    @FXML private TableColumn<ListeProduit ,String> TabColDevisTVA;
    @FXML private TableColumn<ListeProduit ,String> TabColDevisTotalTTC;
    
    Devis devis = new Devis();
    
    private Service<Void> ThreadDevisDetail;
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
 
        TabColDevisReference.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("reference"));
        TabColDevisDesignaton.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("designation"));

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

        TabColDevisPrixHT.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("PrixHT"));
        TabColDevisPrixHT.setStyle( "-fx-alignment: CENTER;");
        TabColDevisPrixHT.getStyleClass().add("Center");

        TabColDevisRemise.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("Remise"));
        TabColDevisRemise.setStyle( "-fx-alignment: CENTER;");
        TabColDevisRemise.getStyleClass().add("Center");

        TabColDevisTotalHT.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("TotalHT"));
        TabColDevisTotalHT.setStyle( "-fx-alignment: CENTER;");
        TabColDevisTotalHT.getStyleClass().add("Center");

        TabColDevisTVA.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("TVA"));
        TabColDevisTVA.setStyle( "-fx-alignment: CENTER;");
        TabColDevisTVA.getStyleClass().add("Center");

        TabColDevisTotalTTC.setCellValueFactory(new PropertyValueFactory<ListeProduit, String>("TotalTTC"));
        TabColDevisTotalTTC.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;");
        TabColDevisTotalTTC.getStyleClass().add("Center");
    }    
    
    public void SowDetailDevis(String codedevis)
    {
         ThreadDevisDetail = new Service<Void>(){
            @Override
            protected Task<Void> createTask(){
                return new Task<Void>(){
                    @Override
                    protected Void call() throws Exception{
                        
                        devis = new DevisDB().getDevis(codedevis);
                        
                        return null;
                    }
                };
            }
	};
        ThreadDevisDetail.setOnSucceeded(new EventHandler<WorkerStateEvent>(){
            @Override
            public void handle(WorkerStateEvent event){
                DevisDateCreation.setText(Adaptateur.NormalDateFormat(devis.getDateCreation()));
                DevisFinValidite.setText(devis.getValidite());
                DevisReglement.setText(devis.getTypeRegementString());
                DevisTotalHorsTaxNet.setText(Adaptateur.StringToStringEspace(devis.getTotalHorsTaxNet()));
                DevisCode.setText(codedevis);
                DevisTypeClt.setText(devis.getTypeClientString());
                
                if(devis.getTypeClient()==0)
                {
                    Client client = devis.getClient();
                    DevisNom.setText(client.getNom());
                    DevisTextPrenom.setVisible(true);
                    DevisTextMatricule.setVisible(false);
                    DevisPrenom.setText(client.getPrenom());
                    DevisAdresse.setText(client.getAdresse());
                    DevisTELE1.setText(client.getTelefixString());
                    DevisTELE2.setText(client.getTelemobileString());
                }else if(devis.getTypeClient()==1)
                {
                    ClientEntreprise entreprise = devis.getEntreprise();
                    DevisNom.setText(entreprise.getNom());
                    DevisTextPrenom.setVisible(false);
                    DevisTextMatricule.setVisible(true);
                    DevisPrenom.setText(devis.getMatricule());
                    DevisAdresse.setText(entreprise.getAdresse());
                    DevisTELE1.setText(entreprise.getTele1String());
                    DevisTELE2.setText(entreprise.getTele2String());
                }else if(devis.getTypeClient()==2)
                {
                    ClientPassager passager = devis.getPassager();
                    DevisNom.setText(passager.getNom());
                    DevisTextPrenom.setVisible(true);
                    DevisTextMatricule.setVisible(false);
                    DevisPrenom.setText(passager.getPrenom());
                    DevisAdresse.setText(passager.getAdresse());
                    DevisTELE1.setText(passager.getTelefixString());
                    DevisTELE2.setText(passager.getTelemobileString());
                }
                
                DevisNetAPayer.setText(Adaptateur.StringToStringEspaceCurrency(devis.getNetAPayer()));

                TableViewListeProduitDevis.setItems(devis.getListeproduit());
                AnchorPaneDevisDetail.setVisible(true);
                AnchorPaneLoadingDevisDetail.setVisible(false);
            }
        });
        ThreadDevisDetail.start();
    }
    
    @FXML
    private void DevisPrintButtonAction(ActionEvent event) throws IOException {
       PrinterJob jobPrint = PrinterJob.createPrinterJob(); 
       jobPrint.getJobSettings().setPageRanges(new PageRange(1, 1));
       
        if (jobPrint.showPrintDialog(null)) {
            
            FXMLLoader fxmlLoaderPrintDevis = new FXMLLoader(getClass().getResource("/Views/Devis/DevisCltPrint.fxml"));
            final Node ParentFxmlPrintDevis = (Node)fxmlLoaderPrintDevis.load();
            DevisCltPrintController PrintDevis = fxmlLoaderPrintDevis.getController();
                
            PrintDevis.SetDevis(devis);

            jobPrint.printPage(ParentFxmlPrintDevis);
            
            jobPrint.endJob();
        }
    }
    
}
