/*
 * ClientMysql.java
 */
package bdd;

import com.test.beans.Pompier;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Dominique_2
 */
public class ClientMysql {

    private Connection theConnection;
    private Pompier unClient;

    /**
     * Constructeur
     */
    public ClientMysql() {
        theConnection = Connexion.getConnect("localhost", //s erveur
                "bdclient", // base de données
                "adminBDClient",// user
                "mdpBDClient"); // mot de passe ClientMysql() {
    }
    
    /**
     * Recherche de tous les clients
     * @return collection de clients
     */
    public ArrayList<Pompier> readAll() {
        ArrayList<Pompier> lesClients = new ArrayList<>();

        try {
            Statement stmt = theConnection.createStatement();
            ResultSet resultQ = null;
            resultQ = stmt.executeQuery("SELECT * FROM client");
            while (resultQ.next()) {
                unClient = new Pompier(resultQ.getInt("id"),
                        resultQ.getString("civilite"),
                        resultQ.getString("nom"),
                        resultQ.getString("prenom"),
                        resultQ.getString("mdp"),
                        resultQ.getInt("adrno"),
                        resultQ.getString("adrrue"),
                        resultQ.getString("adrville"),
                        resultQ.getString("adrcp"),
                        resultQ.getString("adrmail"),
                        resultQ.getString("tel"),
                        resultQ.getString("dateNais"));
                lesClients.add(unClient);
            }
            resultQ.close();
            stmt.close();
            //theConnection.close();
        } catch (SQLException ex) {
            System.out.println("SQLException : " + ex.getMessage());
            System.out.println("SQLState : " + ex.getSQLState());
            System.out.println("Code erreur : " + ex.getErrorCode());
        }
        
        return lesClients;
    }
    
    /**
     * Creation du client passé en paramètre dans la table client
     * Requête non préparée
     * @param c objet de type Pompier (sans identifiant)
     * @return int : id du client créé
     */
    public int create(Pompier c) {
        int id = -1;
        try {           
            Statement stmt = theConnection.createStatement();
            int status = stmt.executeUpdate(
                "INSERT INTO client (civilite, nom, prenom, mdp, adrno, adrrue, adrville, adrcp, adrmail, tel, dateNais) "
                + "VALUES ('" + c.getCivilite() + "', '" 
                                + c.getNom() + "', '"
                                + c.getPrenom() + "', '"
                                + c.getMdp() + "', " 
                                + c.getAdrNo() + ", '"
                                + c.getAdrRue() + "', '" 
                                + c.getAdrCP() + "', '" 
                                + c.getAdrVille() + "', '" 
                                + c.getAdrmail()+ "', '"
                                + c.getTel()+ "', '"
                                + c.getDateNais() + "');",
                Statement.RETURN_GENERATED_KEYS);
            
            // Recherche de l'identifiant du client créé            
            if (status > 0) {
                ResultSet result = stmt.getGeneratedKeys();
                if (result.first()) {
                    id = result.getInt(1);                    
                }
            }               
        } catch (SQLException ex) {
            System.out.println("SQLException : " + ex.getMessage());
            System.out.println("SQLState : " + ex.getSQLState());
            System.out.println("Code erreur : " + ex.getErrorCode());
        }        
        return id;
    }
    
    /**
     * Creation du client passé en paramètre dans la table client
     * Requête préparée
     * @param c objet de type Pompier (sans identifiant)
     * @return int : id du client créé
     */
    public int createRP(Pompier c) {
        int id = -1;
        try {           
            PreparedStatement stmt = null;
            String sql =  "INSERT INTO client (civilite, nom, prenom, mdp, adrno, adrrue, adrville, adrcp, adrmail, tel, dateNais) "
                + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
            stmt = theConnection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            stmt.setString(1, c.getCivilite());
            stmt.setString(2, c.getNom());
            stmt.setString(3, c.getPrenom());
            stmt.setString(4, c.getMdp());
            stmt.setInt(5, c.getAdrNo());
            stmt.setString(6, c.getAdrRue());
            stmt.setString(7, c.getAdrCP());
            stmt.setString(8, c.getAdrVille());
            stmt.setString(9, c.getAdrmail());
            stmt.setString(10, c.getTel());
            stmt.setString(11, c.getDateNais());
            System.out.println("Requête : " + stmt.toString());
            int status = stmt.executeUpdate();
               
            
            // Recherche de l'identifiant du client créé            
            if (status > 0) {
                ResultSet result = stmt.getGeneratedKeys();
                if (result.first()) {
                    id = result.getInt(1);                    
                }
            }               
        } catch (SQLException ex) {
            System.out.println("SQLException : " + ex.getMessage());
            System.out.println("SQLState : " + ex.getSQLState());
            System.out.println("Code erreur : " + ex.getErrorCode());
        }        
        return id;
    }
}
