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_encaissement;
import database.dataview.view_facture;
import database.tools.DbConnection;

public class ViewFactureCrud implements Crud<view_facture> {
	private Connection connection;
	private Statement statement;

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

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

	@Override
	public view_facture readById(int id) throws SQLException {
		String query = "SELECT * FROM view_facture WHERE facture_p_id='"+id+"'";
		ResultSet rs = statement.executeQuery(query);
		rs.next();
		
		//Construire le dataset � partir du resultset
		return new view_facture(rs.getInt("client_p_id"), 
								rs.getString("client_nom"), 
								rs.getString("client_prenom"), 
								rs.getString("client_commentaire"), 
								rs.getInt("adresse_p_id"), 
								rs.getString("adresse_no_rue_app"), 
								rs.getString("adresse_codepostal"), 
								rs.getInt("ville_p_id"), 
								rs.getString("ville_nom"), 
								rs.getString("province"), 
								rs.getString("pays"), 
								rs.getInt("facture_p_id"), 
								rs.getDate("facture_jour"), 
								rs.getTime("facture_heure"), 
								rs.getBigDecimal("facture_depense"), 
								rs.getString("facture_commentaire"), 
								rs.getInt("fermeture_fk_id"), 
								rs.getInt("taxe_p_id"), 
								rs.getBigDecimal("taxe_tps"), 
								rs.getBigDecimal("taxe_tvq"), 
								rs.getBigDecimal("taxe_hebergement"));
	}

	@Override
	public List<view_facture> readAll() throws SQLException {
		List<view_facture> lst = new ArrayList<>();
		
		String query = "SELECT * FROM view_facture LIMIT 0, 100";
		ResultSet rs = statement.executeQuery(query);
		
		while(rs.next()){
		    //Construire le dataset � partir du resultset
			view_facture tmp = new view_facture(rs.getInt("client_p_id"), 
					rs.getString("client_nom"), 
					rs.getString("client_prenom"), 
					rs.getString("client_commentaire"), 
					rs.getInt("adresse_p_id"), 
					rs.getString("adresse_no_rue_app"), 
					rs.getString("adresse_codepostal"), 
					rs.getInt("ville_p_id"), 
					rs.getString("ville_nom"), 
					rs.getString("province"), 
					rs.getString("pays"), 
					rs.getInt("facture_p_id"), 
					rs.getDate("facture_jour"), 
					rs.getTime("facture_heure"), 
					rs.getBigDecimal("facture_depense"), 
					rs.getString("facture_commentaire"), 
					rs.getInt("fermeture_fk_id"), 
					rs.getInt("taxe_p_id"), 
					rs.getBigDecimal("taxe_tps"), 
					rs.getBigDecimal("taxe_tvq"), 
					rs.getBigDecimal("taxe_hebergement"));
			lst.add(tmp);
		}
		
		return lst;
	}

	public List<view_facture> readByFk_fermeture(int fermeture_fk_id) throws SQLException {
		List<view_facture> lst = new ArrayList<>();
		
		String query = "SELECT * FROM view_facture WHERE fermeture_fk_id="+fermeture_fk_id+" LIMIT 0, 100";
		ResultSet rs = statement.executeQuery(query);
		
		while(rs.next()){
		    //Construire le dataset � partir du resultset
			view_facture tmp = new view_facture(rs.getInt("client_p_id"), 
					rs.getString("client_nom"), 
					rs.getString("client_prenom"), 
					rs.getString("client_commentaire"), 
					rs.getInt("adresse_p_id"), 
					rs.getString("adresse_no_rue_app"), 
					rs.getString("adresse_codepostal"), 
					rs.getInt("ville_p_id"), 
					rs.getString("ville_nom"), 
					rs.getString("province"), 
					rs.getString("pays"), 
					rs.getInt("facture_p_id"), 
					rs.getDate("facture_jour"), 
					rs.getTime("facture_heure"), 
					rs.getBigDecimal("facture_depense"), 
					rs.getString("facture_commentaire"), 
					rs.getInt("fermeture_fk_id"), 
					rs.getInt("taxe_p_id"), 
					rs.getBigDecimal("taxe_tps"), 
					rs.getBigDecimal("taxe_tvq"), 
					rs.getBigDecimal("taxe_hebergement"));
			lst.add(tmp);
		}
		
		return lst;
	}
	
	@Override
	public boolean update(view_facture dataset) throws SQLException {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean destroy(view_facture 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();
		ViewFactureCrud crud = new ViewFactureCrud(connection);
		for (view_facture v : crud.readAll())
			System.out.println(v.toString());
	}
}
