package database.migration.etape1.chambre_reserv_reparation;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;

import database.dataset.Chambre;
import database.dataset.Reservation;
import database.migration.SqlFactory;

public class ReadChambre {
	private Chambre _chambre;
	private Reservation _reservation;
	/*la vérification des duplication est pauvre car il n'ya pas de possible duplication ici*/
	public ReadChambre(Chambre _chambre, 
			           Reservation _reservation) {
		super();
		this._chambre = _chambre;
		this._reservation = _reservation;
	}

	public static Set<ReadChambre> from(Connection connection) throws SQLException{
		System.out.println("import chambre");
		Statement stm = connection.createStatement();
		ResultSet rs = stm.executeQuery("SELECT * FROM chambre");
		Set<ReadChambre> chambre_lst = new HashSet<ReadChambre>();
		
		Chambre _chambre;
		Reservation _reservation;
		
		int reservID = 1;  //get max from reservation , reservation found in vente where id_chambre != 0
		
		while (rs.next()){
			
			int id = rs.getInt("ID"),
			    etage = rs.getInt("etage");
			String lits = rs.getString("lits");
			boolean avant = rs.getBoolean("avant");
			String commentaire = rs.getString("commentaire");
			boolean indisponnibilite = rs.getBoolean("indisponible");
			Date date;
//			String dateStr = rs.getString("dateND");
//			if (dateStr.equals("0000-00-00"))
//				date = null;
//			else
				date = rs.getDate("dateND");
			
			_chambre = new Chambre(id, 
					               1, 
					               avant, 
					               etage, 
					               lits, 
					               id); 
			if (indisponnibilite){
				//System.out.println("reservation/reparation");
				_reservation = new Reservation(reservID++, 
						                       id, 
						                       date, 
						                       date, 
						                       commentaire, 
						                       10);
				chambre_lst.add(new ReadChambre(_chambre, _reservation));
			}
			else{
				_reservation = null;
				chambre_lst.add(new ReadChambre(_chambre, null));
			}
		}
		
		return chambre_lst;
	}
	
	public static Set<Reservation> normaliseReservation(Set<ReadChambre> lst){
		Set<Reservation> yield = new HashSet<Reservation>();
		
		for (ReadChambre rc : lst){
			if (rc._reservation!=null)
				if (!yield.contains(rc._reservation))
					yield.add(rc._reservation);
		}

		//System.out.println("nb reservation" + yield.size());
		return yield;
	}
	
	public static Set<Chambre> normaliseChambre(Set<ReadChambre> lst){
		Set<Chambre> yield = new HashSet<Chambre>();
		
		for (ReadChambre rc : lst){
			if (!yield.contains(rc._chambre))
				yield.add(rc._chambre);
		}

		//System.out.println("nb chambre" + yield.size());
		return yield;
	}
	
	/**
	 * @param args
	 * @throws SQLException 
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws SQLException, FileNotFoundException, IOException {
		Connection connection = SqlFactory.connect(SqlFactory.loadProperties("hotel_orford_import.xml"));
		
		for (ReadChambre a : from(connection)){
			System.out.println(a._chambre.toString());
			if (a._reservation!=null)
				System.out.println(a._reservation);
		}
		
		System.out.println("bonne journée");
	}

}
