package database.migration.etape2.facture;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import tool.regEx.DataNormalizer;
import database.dataset.Facture;
import database.dataset.Taxe;
import database.migration.SqlFactory;

public class ReadFacture {
	private Facture facture;
	private Taxe taxe;

	public ReadFacture(Facture facture, Taxe taxe) {
		super();
		this.facture = facture;
		this.taxe = taxe;
	}

	public static List<ReadFacture> from(Connection connection) throws SQLException{
		System.out.println("import facture");
		List<ReadFacture> readFacture = new ArrayList<ReadFacture>();
		int taxe_id = 1;
		
		Statement stm = connection.createStatement();
		ResultSet rs = stm.executeQuery("SELECT * FROM facture");;
		while(rs.next()){
			int ID = rs.getInt("ID"),
			    ID_statut = rs.getInt("ID_statut"),
			    ID_client = rs.getInt("ID_client"),
			    ID_fermeture = rs.getInt("ID_fermeture");
			String commentaire = DataNormalizer.normalize(rs.getString("commentaire"));
			BigDecimal taux_TPS = rs.getBigDecimal("taux_TPS"),
					   taux_TVQ = rs.getBigDecimal("taux_TVQ");
			/*verification jamais utilisé*/
			Taxe taxe = new Taxe(taxe_id++, 
					             taux_TPS, 
					             taux_TVQ, 
					             BigDecimal.valueOf(0.00), 
					             new Date(0));
			Facture facture = new Facture(ID, 
					                      ID_client, 
					                      ID_fermeture, 
					                      commentaire, 
					                      taxe.getTaxe_p_id(), 
					                      new Date(0), 
					                      new Time(0), 
					                      BigDecimal.valueOf(0.00));
			
			readFacture.add(new ReadFacture(facture, taxe));
		}
		
		return readFacture;
	}
	
	public static int sameTaxe(Taxe t , Set<Taxe> st){
		for (Taxe tt : st)
			if (t.getTaxe_tps() == tt.getTaxe_tps() &&
			    t.getTaxe_tvq() == tt.getTaxe_tvq())
				return tt.getTaxe_p_id();
		return 0;
	}
	
	public static NormalisedFacture normaliseTaxe(List<ReadFacture> rflst){
		System.out.println("normalise les taxes");
		Set<Taxe> normalTaxe = new HashSet<Taxe>();
		Set<Facture> facture = new HashSet<Facture>();
		
		for (ReadFacture rf : rflst){
			int txID = sameTaxe(rf.taxe, normalTaxe);
			if (txID==0)
				normalTaxe.add(rf.taxe);
			else{
				rf.facture.setTaxe_fk_id(txID);
			}
			
			facture.add(rf.facture);
		}
		
		return new NormalisedFacture(facture, normalTaxe);
	}
	
	/**
	 * @param args
	 * @throws IOException 
	 * @throws SQLException 
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException, SQLException, IOException {
		Connection connection = SqlFactory.connect(SqlFactory.loadProperties("hotel_orford_import.xml"));
		
		List<ReadFacture> r = from(connection);
		for (ReadFacture rf : r){
			System.out.println(rf.facture.toString());
			System.out.println(rf.taxe.toString());
		}
		
		System.out.println("bonne journée");
	}

}
