package Controllers.DevisClt;

import Models.Produit.ListeProduit;
import Controllers.Traitement.MyWindow;
import Models.Client.Client;
import Models.Client.ClientEntreprise;
import Models.Client.ClientPassager;
import Models.DevisClt.Devis;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

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

    public Node nodeFxml ;
    
    Logger logger = Logger.getLogger(DevisDialogConfirmationController.class.getName());
    
    @FXML public AnchorPane AnchorPrincipal ;
    @FXML public Button DevisEnregister ;
    @FXML private Text DevisDateCreation;
    @FXML private Text DevisFinValidite;
    @FXML private Text DevisReglement;
    @FXML private Text DevisNetAPayer;
    @FXML private Text DevisTotalHorsTaxNet;
    @FXML private Text DevisTotalTVA;
    
    @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 public TableView<ListeProduit> TableViewListeProduitDevis ;
    @FXML public TableColumn<ListeProduit ,String> TabColDevisReference;
    @FXML public TableColumn<ListeProduit ,String> TabColDevisDesignaton;
    @FXML public TableColumn<ListeProduit ,String> TabColDevisQuantite ;
    @FXML public TableColumn<ListeProduit ,String> TabColDevisPrixHT;
    @FXML public TableColumn<ListeProduit ,String> TabColDevisRemise;
    @FXML public TableColumn<ListeProduit ,String> TabColDevisTotalHT;
    @FXML public TableColumn<ListeProduit ,String> TabColDevisTVA;
    @FXML public TableColumn<ListeProduit ,String> TabColDevisTotalTTC;
    
    @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");
        
        Image coffeeImage = new Image(getClass().getResourceAsStream("/Public/icon/iconsave.png"));
        ImageView buttonGraphic = new ImageView();
        buttonGraphic.setImage(coffeeImage);
        DevisEnregister.setGraphic(buttonGraphic);
    }
    
    public void Getdevis(Devis devis)
    {
        DevisDateCreation.setText(devis.getDateCreation());
        DevisFinValidite.setText(devis.getValidite());
        DevisReglement.setText(devis.getTypeRegement());
        
        DevisNetAPayer.setText(devis.getNetAPayer());
        DevisTotalHorsTaxNet.setText(devis.getTotalHorsTaxNet());
        DevisTotalTVA.setText(devis.getTotalTVA());
        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());
        }
             
        TableViewListeProduitDevis.setItems(devis.getListeproduit());
    }
    
    
    public  DevisDialogConfirmationController(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/Views/Devis/DevisDialogConfirmation.fxml"));
            fxmlLoader.setController(this);
            nodeFxml = (Node) fxmlLoader.load();
        } catch (IOException ex) {
            logger.error("DevisDialogConfirmationController  : DevisDialogConfirmation.fxml :" + ex.getMessage());
        }
    }
    
    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);
    }    
    
}
