package com.arnaudpc;
import java.sql.*;

/**
  * This class details all methods useful to get data from the data base 
  */ 

public class Requests {
 
   private static final long serialVersionUID = 1L;

   private String url = "jdbc:mysql://192.168.2.5/FromagesVins";
   private String user = "user";
   private String passwd = "85N8C34MfFNVbcvS";
   private String driver = "com.mysql.jdbc.Driver";
  
   private Utils U;
   private String wineList[];
   private String cheeseList[];
                 

   public Requests() {
      Utils U = new Utils();
      wineList = new String[500];
      cheeseList = new String[500];         
   }
   		
   /**
     * This method returns the number of cheeses available in the data base
     * <p>     
     * @return an int     
     */      

   public int numberOfCheeses()  {
      Connection con = null;
      Statement st = null;         
      ResultSet rs = null;
      cheeseList[0] = "Data base is empty";
      int i = 0;      
      try { 
         Class.forName(driver).newInstance();
         con = DriverManager.getConnection(url, user, passwd);
         st = con.createStatement();          		
         rs = st.executeQuery("SELECT nom FROM fromages");
         while(rs.next()) { 
            cheeseList[i] = Utils.stripSlashes(rs.getString(1));
            i++;
         } 
      } 
      catch (Exception e) { 
         System.err.println("listCheeses() Exception: " + e.getMessage());    
      }   	 
      finally { 
         try { 
            if(rs != null) rs.close();
            if(st != null) st.close();                
            if(con != null) con.close(); 
         } 
         catch (SQLException e) {
            System.err.println("listCheeses() SQLException: " + e.getMessage());
         }
      }   
      System.gc();
      return(i);
   }
   
   /**
     * This method returns the number of wines according to a chosen cheese
     * and a given wine color (red or white) 
     * <p>
     * @param cheese A chosen cheese
     * @param wineColor A given color for wine     
     * @return an int
     * 
     */         
   
   public int numberOfSuggestedWines(String cheese, String wineColor)  {
      Connection con = null;
      Statement st = null;
      ResultSet rs = null;
      Statement st2 = null;
      ResultSet rs2 = null;
      String relation = null;
      String table = null;
      int i = 0;
      int id = 0;
      switch (wineColor) {
         case "white":
            relation = "vb";
            table = "vinblancs";
            break;
         case "red":
            relation = "vr";
            table = "vinrouges";
            break;
         default:
            i = 0;
            break;
      }
      try { 
         Class.forName(driver).newInstance(); 
         con = DriverManager.getConnection(url, user, passwd);
         st = con.createStatement();
         System.out.println("SELECT r.id"+relation+" FROM "+relation+" r, fromages f WHERE f.nom ='"+ Utils.addSlashes(cheese)+"' AND f.id=r.idf");
         rs = st.executeQuery("SELECT r.id"+relation+" FROM "+relation+" r, fromages f WHERE f.nom ='"+ Utils.addSlashes(cheese)+"' AND f.id=r.idf");
         while(rs.next()) {
            id = rs.getInt(1);
            st2 = con.createStatement();
            System.out.println("SELECT nom FROM "+table+" WHERE id ='"+id+"'");          		
            rs2 = st2.executeQuery("SELECT nom FROM "+table+" WHERE id ='"+id+"'");            
            while(rs2.next()) {
               wineList[i] = rs2.getString(1);
               i++;
            }
         } 
      } 
      catch (Exception e) { 
         System.err.println("numberOfSuggestedWines() Exception: " + e.getMessage());
             
      }   	 
      finally { 
         try {    
            if(rs2 != null) rs.close(); 
            if(st2 != null) st.close();
            if(rs != null) rs.close();
            if(st != null) st.close();                  
            if(con != null) con.close(); 
         } 
         catch (SQLException e) {
            System.err.println("numberOfSuggestedWines() SQLException: " + e.getMessage());  
         }
      }
      System.gc();
      return(i);      
   }       
      
 /**
    * This method returns all cheeses available in the data base
    * <p>     
    * @return an array of Strings     
    */      
		
   public String[] listCheeses()  {
      String myList[];
      int numberOfChesses=0;
      try {
         numberOfChesses = this.numberOfCheeses();  
      }
      catch (Exception ex) { 
         System.out.println("listCheeses(), DataBase request, Exeception "+ ex.getMessage()); 
      }
      myList = new String[numberOfChesses];

      System.arraycopy(cheeseList, 0, myList, 0, numberOfChesses);
      return(myList);
   }
   
   /**
     * This method returns some wines according to a chosen cheese and a wine color (red or white) 
     * <p>
     * @param cheese A chosen cheese
     * @param wineColor A given color for wine     
     * @return an array of Strings
     * 
     */         

   public String[] suggestWines(String cheese, String wineColor)  {
      String myList[];
      int numberOfWines=0;
      try {
         numberOfWines = this.numberOfSuggestedWines(cheese, wineColor);  
      }
      catch (Exception ex) { 
         System.out.println("suggestWines(), DataBase request, Exeception "+ ex.getMessage());
      }
      System.out.println("numberOfWines "+ numberOfWines);   
      myList = new String[numberOfWines];
   
      for (int i = 0; i<numberOfWines; i++) {
         myList[i] = wineList[i];            
      }
      return(myList);      
   }
} 

