package Models.User;

import Controllers.Dialog.ShowDialog;
import Controllers.Traitement.Adaptateur;
import Controllers.Traitement.ParametreSystem;
import Models.H2JDBC;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.log4j.Logger;

/**
 * @author Maher Ben Tili
 */
public class ProfileDB {
    
    Logger logger = Logger.getLogger(ProfileDB.class.getName());
    H2JDBC h2jdbc = new H2JDBC();
    
    public Profile getProfileByConnexion(String login, String password)
    {
        Profile profile = new Profile();
        
        String Query = "SELECT * "
                + "FROM authentification A, profile P "
                + "WHERE (A.id_profile = P.idprofile) AND (A.login= '"+Adaptateur.addSlashes(login)+"') AND (A.password = '"+Adaptateur.addSlashes(password)+"') ;";
        
        try{
           ResultSet resultat = h2jdbc.selectFromDB(Query);
            //on place le curseur sur le dernier tuple
            resultat.last();
            //on récupère le numéro de la ligne
            int nombreLignes = resultat.getRow();
            if(nombreLignes>0)
            {
                Authentification authentification = new Authentification();
                
                authentification.setIdAuthentification(resultat.getString("idauhentification"));
                authentification.setLogin(resultat.getString("login"));
                authentification.setPassword(resultat.getString("password"));
                authentification.setTypeCompte(resultat.getString("type_compte"));
                authentification.setQuestionSecrete(resultat.getString("question_secrete"));
                authentification.setReponceQuestion(resultat.getString("reponce_question"));
                authentification.setEtatCompte(resultat.getBoolean("etat_compte"));
                authentification.setStockNegatif(resultat.getBoolean("stock_negatif"));
                authentification.setLocalNom(resultat.getString("local_nom"));
                
                profile.setAuthentification(authentification);
                profile.setIdprofile(resultat.getInt("idprofile"));
                profile.setNom(resultat.getString("nom"));
                profile.setPrenom(resultat.getString("prenom"));
                profile.setNaissance(resultat.getString("naissance"));
                profile.setSexe(resultat.getInt("sexe"));
                profile.setTele(resultat.getInt("num_tele"));
                profile.setMail(resultat.getString("mail"));
                profile.setAdresse(resultat.getString("adresse"));
                profile.setPays(resultat.getString("pays"));
            }
        }catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(Query);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("  :  :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                ShowDialog SD = new ShowDialog();
                SD.ShowAletDialog();
            }
        }
        return profile;
    }
    
    
    public boolean updateAuthentification(String idAuhentification, Authentification authentification)
    {
        String req = "UPDATE authentification SET  "
                + " password = '"+Adaptateur.addSlashes(authentification.getPassword())+"', "
                + " question_secrete = '"+Adaptateur.addSlashes(authentification.getQuestionSecrete())+"', "
                + " reponce_question = '"+Adaptateur.addSlashes(authentification.getReponceQuestion())+"' "
                + " WHERE idauhentification = "+idAuhentification+" ;";   
        int nbr = 0;
        try {
            nbr = h2jdbc.InsertUpdateDeleteInTable(req);
        }catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ProfileDB : updateAuthentification :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                ShowDialog SD = new ShowDialog();
                SD.ShowAletDialog();
            }
        }
        return (nbr>0);
    }
    public boolean updateProfile(int idprofile, Profile profile)
    {
        String req = "UPDATE profile SET mail =  '"+Adaptateur.addSlashes(profile.getMail())+"', nom = '"+Adaptateur.addSlashes(profile.getNom())+"', prenom = '"+Adaptateur.addSlashes(profile.getPrenom())+"', naissance = '"+profile.getNaissance()+"', sexe = '"+profile.getSexe()+"', num_tele = "+profile.getTele()+", adresse = '"+Adaptateur.addSlashes(profile.getAdresse())+"', pays = '"+Adaptateur.addSlashes(profile.getPays())+"' WHERE  (idprofile = "+idprofile+"); ";
        int nbr = 0;
        try {
            nbr = h2jdbc.InsertUpdateDeleteInTable(req);
        }catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ProfileDB  : updateProfile :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                ShowDialog SD = new ShowDialog();
                SD.ShowAletDialog();
            }
        }
        return (nbr>0);
    }
    
    
    
    
    public Profile getpro(int idprofile)
    {
        Profile myprofile = new Profile();
        
        String req = "SELECT * "
                + "FROM authentification A, profile P "
                + "WHERE (P.idprofile = A.id_profile) AND "
                + " (P.idprofile= "+idprofile+") AND "
                + " (A.local_nom = '"+ParametreSystem.NomLocalPC+"') ;";
        
        ResultSet resultat = h2jdbc.selectFromDB(req);
        try {
            while (resultat.next()) {
                Authentification authentification = new Authentification();
                authentification.setIdAuthentification(resultat.getString("idauhentification"));
                authentification.setLogin(resultat.getString("login"));
                authentification.setPassword(resultat.getString("password"));
                authentification.setTypeCompte(resultat.getString("type_compte"));
                authentification.setQuestionSecrete(resultat.getString("question_secrete"));
                authentification.setReponceQuestion(resultat.getString("reponce_question"));
                authentification.setEtatCompte(resultat.getBoolean("etat_compte"));
                authentification.setStockNegatif(resultat.getBoolean("stock_negatif"));
                authentification.setLocalNom(resultat.getString("local_nom"));
                
                myprofile.setAuthentification(authentification);
                myprofile.setIdprofile(resultat.getInt("idprofile")); 
                myprofile.setNom(resultat.getString("nom"));
                myprofile.setPrenom(resultat.getString("prenom"));
                myprofile.setNaissance(resultat.getString("naissance"));
                myprofile.setSexe(resultat.getInt("sexe"));
                myprofile.setTele(resultat.getInt("num_tele"));
                myprofile.setMail(resultat.getString("mail"));
                myprofile.setAdresse(resultat.getString("adresse"));
                myprofile.setPays(resultat.getString("pays"));
            }
        } catch (SQLException ex) {
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("ProfileDB  : getpro :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                ShowDialog SD = new ShowDialog();
                SD.ShowAletDialog();
            }
        }
        
        return myprofile;
    }
}
