package Models.User;

import Controllers.Dialog.ShowDialog;
import Controllers.Traitement.ParametreSystem;
import Models.H2JDBC;
import java.sql.ResultSet;
import java.sql.SQLException;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.apache.log4j.Logger;

/**
 * @author Maher Ben Tili
 */
public class ListUserDB {
    
    H2JDBC h2jdbc = new H2JDBC();
    ShowDialog SD = new ShowDialog();
    Logger logger = Logger.getLogger(ListUserDB.class.getName());
    
    public String nbrUsers()
    {
        String req = "SELECT  COUNT(idprofile) FROM  profile ,  authentification WHERE  idprofile =  idauhentification ;";
        String sommme = "0";
        try {
            ResultSet Resultat = h2jdbc.selectFromDB(req);
            Resultat.next();
            sommme = Resultat.getString(1);
        } catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ListUserDB  : nbrUsers :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }    
        return sommme;
    }
    
    
    
    public ObservableList<ListUsers> rechercheData(String ch){
        
        ObservableList<ListUsers> data=FXCollections.observableArrayList();
        String req = "SELECT  idprofile ,  type_compte, login ,  nom ,  prenom ,  num_tele "
                + "   FROM  profile ,  auhentification"
                + "   WHERE  idprofile =  idauhentification "
                + "   AND idprofile = "+ch+" ;";
        
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                int idprofile = resultat.getInt( "idprofile" );
                String type_compte=resultat.getString("type_compte");
                String login=resultat.getString("login");
                String nom=resultat.getString("nom");
                String prenom=resultat.getString("prenom");
                int tele=resultat.getInt("num_tele");
                data.add(new ListUsers(idprofile, type_compte, login, nom, prenom, tele));
            }
        } catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ListUserDB  : rechercheData :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
       return data ;
    }
    
    
    
    public ObservableList<ListUsers> getdata()
    {
        ObservableList<ListUsers> data=FXCollections.observableArrayList();
        String req = "SELECT  idprofile ,  type_compte, login ,  nom ,  prenom ,  num_tele FROM  profile ,  authentificationWHERE  idprofile =  idauhentification ;";
        try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                int idprofile = resultat.getInt( "idprofile" );
                String type_compte=resultat.getString("type_compte");
                String login=resultat.getString("login");
                String nom=resultat.getString("nom");
                String prenom=resultat.getString("prenom");
                int tele=resultat.getInt("num_tele");
                data.add(new ListUsers(idprofile, type_compte, login, nom, prenom, tele));
            }
        }catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ListUserDB  : getdata :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
       return data ;
    }
    
}
