package Controllers.Home;

import Controllers.Traitement.Adaptateur;
import Controllers.Traitement.ParametreSystem;
import Models.Home.HomeDB;
import com.gluonhq.charm.glisten.control.ProgressBar;
import java.net.URL;
import java.util.Map;
import java.util.ResourceBundle;
import javafx.animation.FadeTransition;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;
import javafx.util.Duration;

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

    @FXML private AnchorPane AnchorPaneDashboard ;
    
    @FXML private Text TextYearDashboard;
    @FXML private Text TextNbrClient;
    @FXML private Text TextNbrVente;
    @FXML private Text TextNbrCheque;
    
    @FXML public BarChart BarChartVente;
    @FXML private PieChart PieChartVente;
    
    @FXML private ProgressBar ProgressBarLoading;
    
    private Service<Void> ThreadDashboard;
    
    String Year = Adaptateur.getDefaultCurrentDate("yyyy"); 
    
    HomeDB Homedb = new HomeDB();
    
    XYChart.Series series1 = new XYChart.Series();
    ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
    
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
        TextYearDashboard.setText("L'année "+Year);
        
        FadeTransitionNode(AnchorPaneDashboard);
        
        ProgressBarLoading.setVisible(true);
        ThreadDashboard = new Service<Void>(){    
            @Override
            protected Task<Void> createTask(){
                return new Task<Void>(){
                    @Override
                    protected Void call() throws Exception{
                        series1 = Homedb.HomeChiffreAffaireByMois();
                        series1.setName(ParametreSystem.NomLocalPC); 
                        
                        pieChartData = Homedb.HomeVente(Year);
                        
                        NombreVCV();
                        
                        return null;
                    }
                };
            }
        };
        ThreadDashboard.setOnSucceeded(new EventHandler<WorkerStateEvent>(){
            @Override
            public void handle(WorkerStateEvent event){
               BarChartVente.getData().addAll(series1);
               PieChartVente.setData(pieChartData);
               ProgressBarLoading.setVisible(false);

            }
        });
        ThreadDashboard.start();
        
        
        
    }    
    
    public void FadeTransitionNode(Node AnchorPane){
        FadeTransition ft = new FadeTransition(Duration.millis(200), AnchorPane);
        ft.setFromValue(0);
        ft.setToValue(1.0);
        ft.play();
    }
    
    
    
    
    
    
    private void NombreVCV()
    {
        Map map = Homedb.HomeNbr();   
        TextNbrClient.setText(map.get("client").toString());
        TextNbrVente.setText(map.get("factureclt").toString());
        TextNbrCheque.setText(map.get("cheque").toString());
    }
    
    
}
