package database.crud.denormalised;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import database.Crud;
import database.dataview.view_facture;
import database.dataview.view_groupe;
import database.dataview.view_reservation;
import database.tools.DbConnection;

public class ViewReservationCrud implements Crud<view_reservation> {
	private Connection connection;
	private Statement statement;

	/**
	 * @param connection
	 * @throws SQLException 
	 */
	public ViewReservationCrud(Connection connection) throws SQLException {
		super();
		this.connection = connection;
		this.statement = this.connection.createStatement();
	}

	@Override
	public int create(view_reservation dataset) throws SQLException {
		throw new UnsupportedOperationException();
	}

	@Override
	public view_reservation readById(int id) throws SQLException {
		String query = "SELECT * FROM view_reservation WHERE reservation_p_id='"+id+"'";
		ResultSet rs = statement.executeQuery(query);
		rs.next();
		
		//Construire le dataset � partir du resultset
		return new view_reservation(rs.getInt("reservation_p_id"), 
				                    rs.getDate("reservation_debut"), 
				                    rs.getDate("reservation_fin"), 
				                    rs.getInt("status_chambre_p_id"), 
				                    rs.getString("status_chambre_description"), 
				                    rs.getInt("chambre_p_id"), 
				                    rs.getInt("chambre_nombredelits"), 
				                    rs.getBoolean("chambre_devantlhotel"), 
				                    rs.getInt("chambre_nombreEtage"), 
				                    rs.getString("chambre_numero"), 
				                    rs.getString("chambre_description"), 
				                    rs.getString("reservation_commentaire"), 
				                    rs.getInt("groupe_p_id"), 
				                    rs.getString("groupe_nom"), 
				                    rs.getInt("client_p_id"), 
				                    rs.getString("client_nom"), 
				                    rs.getString("client_prenom")); 
	}

	@Override
	public List<view_reservation> readAll() throws SQLException {
		List<view_reservation> lst = new ArrayList<>();
		
		String query = "SELECT * FROM view_reservation LIMIT 0, 100";
		ResultSet rs = statement.executeQuery(query);
		
		while(rs.next()){
		    //Construire le dataset � partir du resultset
			view_reservation tmp = new view_reservation(rs.getInt("reservation_p_id"), 
                    rs.getDate("reservation_debut"), 
                    rs.getDate("reservation_fin"), 
                    rs.getInt("status_chambre_p_id"), 
                    rs.getString("status_chambre_description"), 
                    rs.getInt("chambre_p_id"), 
                    rs.getInt("chambre_nombredelits"), 
                    rs.getBoolean("chambre_devantlhotel"), 
                    rs.getInt("chambre_nombreEtage"), 
                    rs.getString("chambre_numero"), 
                    rs.getString("chambre_description"), 
                    rs.getString("reservation_commentaire"), 
                    rs.getInt("groupe_p_id"), 
                    rs.getString("groupe_nom"), 
                    rs.getInt("client_p_id"), 
                    rs.getString("client_nom"), 
                    rs.getString("client_prenom")); 
			lst.add(tmp);
		}
		
		return lst;
	}

	@Override
	public boolean update(view_reservation dataset) throws SQLException {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean destroy(view_reservation dataset) throws SQLException {
		throw new UnsupportedOperationException();
	}

	@Override
	public int getNewSequenceId() throws SQLException {
		throw new UnsupportedOperationException();
	}
	public static void main(String ... arg) throws ClassNotFoundException, IOException, SQLException{
		Connection connection = DbConnection.createConnection();
		ViewReservationCrud crud = new ViewReservationCrud(connection);
		for (view_reservation v : crud.readAll())
			System.out.println(v.toString());
	}
}
