package Controllers.Caisse;

import Controllers.Traitement.Adaptateur;
import Controllers.Traitement.MyWindow;
import Models.Caisse.Frais;
import Models.Caisse.FraisDB;
import Models.Reglement.Reglement;
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.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;

/**
 * FXML Controller class
 * @author Maher
 */
public class FraisDetailController {

    public Node nodeFxml ;
    
    @FXML public AnchorPane AnchorPrincipal ;
    
    @FXML public AnchorPane PaneDetailFrais;
    
    @FXML private Text TextMontant;
    @FXML private Text TextPaiement;
    @FXML private Text TextChequeNumero;
    @FXML private Text TextChequeBanque;
    @FXML private Text TextChequeDate;
    @FXML private Text TextCarteTransation;
    @FXML private Text TextCarteBanque;
    @FXML private Text TextPersonne;
    @FXML private Text TextDateCreation;
    @FXML private Text TextProfile;
    @FXML private Text TextObject;
    
    @FXML private GridPane GridPaneCheque;
    @FXML private GridPane GridPaneCarte;
    
    @FXML private TextArea  TextAreaContext ;
    
    @FXML private ProgressIndicator ProgressFrais;
    
    private Service<Void> ThreadFraisDetail;
    
    Frais frais = new Frais();
    
    public  FraisDetailController(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/Views/Caisse/FraisDetail.fxml"));
            fxmlLoader.setController(this);
            nodeFxml = (Node) fxmlLoader.load();
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
    }
    
    
    public void setFrais(Frais frais){
        TextMontant.setText(frais.getMontant());
        TextPaiement.setText(frais.getPaiementString());
        
        if(frais.getPaiement().equals("1")){
            GridPaneCheque.setVisible(true);
            TextChequeNumero.setText(frais.getNumero());
            TextChequeBanque.setText(frais.getBanque());
            TextChequeDate.setText(frais.getDateCheque());
        }
        
        if(frais.getPaiement().equals("2")){
            GridPaneCarte.setVisible(true);
            TextCarteTransation.setText(frais.getNumero());
            TextCarteBanque.setText(frais.getBanque());
        }
        
        TextPersonne.setText(frais.getPersonne());
        TextDateCreation.setText(frais.getDateCreation());
        TextProfile.setText(frais.getProfile());
        TextObject.setText(frais.getObject());
        if(frais.getContext() != null){
            if(!frais.getContext().equals("")){
                TextAreaContext.setText(frais.getContext());
            }
        }
        
    }
    
    
    public void LanchShowFrais(final String IdFrais){
        
        ThreadFraisDetail = new Service<Void>(){
            @Override
            protected Task<Void> createTask(){
                return new Task<Void>(){
                    @Override
                    protected Void call() throws Exception{
                        frais = new FraisDB().getFrais(IdFrais);
                        return null;
                    }
                };
            }
        };
        ThreadFraisDetail.setOnSucceeded(new EventHandler<WorkerStateEvent>(){
            @Override
            public void handle(WorkerStateEvent event){
                setFrais(frais);
                ProgressFrais.setVisible(false);
                PaneDetailFrais.setVisible(true);
            }
        });
        ThreadFraisDetail.start();   
    }
    
    
    
    
    @FXML
    private void ExitButtonAction(ActionEvent event) throws IOException {
        AnchorPrincipal.setVisible(false);
    }
    
    
    
    public void Show()
    {
        StackPane Sp = new StackPane();
            
        Scene scene = new Scene(Sp);
        
        Sp.setPrefSize((double)MyWindow.PrincipalContentWidth, (double)MyWindow.PrincipalContentHeight);
        
        Stage stage = MyWindow.myStage;
        
        Sp.getChildren().add(MyWindow.myParent);
        Sp.getChildren().add(nodeFxml);
            
        stage.setScene(scene);
        stage.show();
    }
    
}
