package Models.Categorie;

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

/**
 * @author Maher Ben Tili
 */
public class CategorieDB {
    
    H2JDBC h2jdbc = new H2JDBC();
    ShowDialog SD = new ShowDialog();
    Logger logger = Logger.getLogger(CategorieDB.class.getName());
    
    public boolean addCategorie(Categorie categorie)
    {
        String req = "INSERT INTO categorie (nomcategorie, unitemesure) VALUES ('"+categorie.getNomcategorie()+"', '"+categorie.getUnitemesure()+"');";
        int nbr = 0;
        try {
            nbr = h2jdbc.InsertUpdateDeleteInTable(req);
        }catch(SQLException ex){
            System.err.println("addCategorie Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("CategorieDB  : addCategorie :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return (nbr>0);
    }
    
    
    
    public boolean verifyCategorie(String cat)
    {
        String sql="SELECT COUNT(*) FROM categorie WHERE nomcategorie LIKE '"+cat+"' ;";
        int nombreLignes = 0;   
        try {
            ResultSet resultat = h2jdbc.selectFromDB(sql);
            resultat.next();
            nombreLignes = resultat.getInt(1);
        } catch(SQLException ex){
            System.err.println("Erreur SQL ! \n"+ex.getMessage());
            System.out.println(sql);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("CategorieDB  : verifyCategorie :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return (nombreLignes>0);
    }
    
    
    public ArrayList<String> getListCategorie()
    {
        ArrayList<String> listcategorie = new  ArrayList<>();
        String req = "SELECT nomcategorie FROM categorie ;";
         try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                String nomcategorie=resultat.getString("nomcategorie");
                listcategorie.add(nomcategorie);
            }
        } catch(SQLException ex){
            System.err.println("getListCategorie Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("CategorieDB  : getListCategorie :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return listcategorie ;
    }
    
    public  ObservableList<CategorieListe> GetCategorie(String Local, String NomCategorie){
        
        ObservableList<CategorieListe> DataCategorieListe = FXCollections.observableArrayList();
        
        NomCategorie = NomCategorie.replaceAll("'","\\\\'");
        
        String req =  " SELECT P.nomcategorie_cate AS category, COUNT(S.produit_reference) AS quantity " +
                        " FROM stock S, produit P " +
                        " WHERE (S.local_nom = '"+Local+"') AND (P.reference = S.produit_reference) AND (P.nomcategorie_cate LIKE '%"+NomCategorie+"%') " +
                        " GROUP BY P.nomcategorie_cate " +
                        " ORDER BY quantity DESC; ";
         try {
            ResultSet resultat = h2jdbc.selectFromDB(req);
            while (resultat.next()){
                String category = resultat.getString("category");
                String quantity =  resultat.getString("quantity");
                DataCategorieListe.add(new CategorieListe(category, quantity));
            }
        } catch(SQLException ex){
            System.err.println("GetCategorie Erreur SQL ! \n"+ex.getMessage());
            System.out.println(req);
            if(ParametreSystem.ErrorShowLogger)
            {
                logger.error("CategorieDB  : GetCategorie :" + ex.getMessage());
            }
            if(ParametreSystem.ErrorShowDialog)
            {
                ShowDialog.setTitre("Erreur SQL !");
                ShowDialog.setDetail(ex.getMessage()+"\n"+ex.getSQLState());
                SD.ShowAletDialog();
            }
        }
        return DataCategorieListe;
    }
}
