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

public class ViewTelephoneCrud implements Crud<view_telephone> {
	private Connection connection;
	private Statement statement;

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

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

	@Override
	public view_telephone readById(int id) throws SQLException {
		String query = "SELECT * FROM view_telephone WHERE telephone_p_id='"+id+"'";
		ResultSet rs = statement.executeQuery(query);
		rs.next();
		
		//Construire le dataset � partir du resultset
		return new view_telephone(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("telephone_p_id"), 
								  rs.getString("telephone_numero"), 
								  rs.getInt("telephone_type_p_id"), 
								  rs.getString("telephone_type_nom"));
	}

	@Override
	public List<view_telephone> readAll() throws SQLException {
		List<view_telephone> lst = new ArrayList<>();
		
		String query = "SELECT * FROM view_telephone LIMIT 0, 100";
		ResultSet rs = statement.executeQuery(query);
		
		while(rs.next()){
		    //Construire le dataset � partir du resultset
			view_telephone tmp = new view_telephone(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("telephone_p_id"), 
					  rs.getString("telephone_numero"), 
					  rs.getInt("telephone_type_p_id"), 
					  rs.getString("telephone_type_nom"));
			lst.add(tmp);
		}
		
		return lst;
	}

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

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