package database.migration.etape1.client;

import java.io.FileNotFoundException;
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 tool.regEx.Adresse;
import tool.regEx.DataNormalizer;
import database.dataset.Client;
import database.dataset.Groupe;
import database.dataset.Telephone;
import database.dataset.Ville;
import database.migration.SqlFactory;

public class ReadClient {
	private Client _client;
	private Groupe _groupe;
	private Adresse _adresse;
	private Ville _ville;
	private List<Telephone> _telephones;
	
	private static final int MAISON = 1,
			                 TRAVAIL = 2;
	
	public ReadClient(Client _client, Groupe _groupe, Adresse _adresse,
			Ville _ville, List<Telephone> _telephones) {
		super();
		this._client = _client;
		this._groupe = _groupe;
		this._adresse = _adresse;
		this._ville = _ville;
		this._telephones = _telephones;
	}
	
	public Client get_client() {
		return _client;
	}

	public Groupe get_groupe() {
		return _groupe;
	}

	public Adresse get_adresse() {
		return _adresse;
	}

	public Ville get_ville() {
		return _ville;
	}

	public List<Telephone> get_telephones() {
		return _telephones;
	}

	public void set_telephones(List<Telephone> _telephones) {
		this._telephones = _telephones;
	}

	public void set_client(Client _client) {
		this._client = _client;
	}

	public void set_groupe(Groupe _groupe) {
		this._groupe = _groupe;
	}

	public void set_adresse(Adresse _adresse) {
		this._adresse = _adresse;
	}

	public void set_ville(Ville _ville) {
		this._ville = _ville;
	}

	@Override
	public String toString() {
		return "ReadClient [_client=" + _client + ", _groupe=" + _groupe
				+ ", _adresse=" + _adresse + ", _ville=" + _ville + "]";
	}
	/**/
	public static List<ReadClient> loadFrom(Connection connection) throws SQLException{
		System.out.println("import client");
		Statement stm = connection.createStatement();
		List<ReadClient> clientList = new ArrayList<ReadClient>();
		int adresseID = 0,
		    villeID = 0,
		    clientID = 0,
		    groupeID = 0,
		    telephoneID = 1;
		
		ResultSet rs = stm.executeQuery("SELECT * FROM client");
		while(rs.next()){
			adresseID++;
			villeID++;
			clientID = rs.getInt("ID");
			groupeID = clientID;
			
			Ville _ville = new Ville(villeID,
									 rs.getString("ville"),
									 rs.getString("province"),
									 "CA?");
			
			Adresse _adresse = new Adresse(adresseID,
										   rs.getString("Adresse"),
										   villeID,
										   rs.getString("codePostal"));
			String cli_nom = rs.getString("nom"),
				   cli_prenom = rs.getString("prenom");
			if (cli_nom==null)
				cli_nom="";
			if (cli_prenom==null)
				cli_prenom="";
			if(cli_nom.length()>32)
				cli_nom = cli_nom.substring(0, 32);
			if(cli_prenom.length()>32)
				cli_prenom = cli_prenom.substring(0, 32);
			Client _client = new Client(clientID,
										cli_nom,
										cli_prenom,
										adresseID,
										rs.getString("commentaire"));
			
			Groupe _groupe = new Groupe(groupeID,
										clientID,
										(rs.getString("nomDuGroupe")!=null?rs.getString("nomDuGroupe").toUpperCase():""));
			
			List<Telephone> _telephone = new ArrayList<Telephone>();
			String noTelephone = rs.getString("telephoneMaison");
			if (noTelephone!=null)
				_telephone.add(new Telephone(telephoneID++,MAISON,noTelephone,clientID));
			noTelephone = rs.getString("telephoneTravail");
			if (noTelephone!=null)
				_telephone.add(new Telephone(telephoneID++,TRAVAIL,noTelephone,clientID));
			
			clientList.add(new ReadClient(_client, _groupe, _adresse, _ville, _telephone));
		}
		
		return clientList;
	}
	
	/*mutateur*/
	public void normalisationDesDonnees(){
		get_adresse().setAdresse_codepostal( DataNormalizer.normalize( get_adresse().getAdresse_codepostal()));
		get_adresse().setadresse_no_rue_app( DataNormalizer.normalize( get_adresse().getadresse_no_rue_app()));
		get_ville().setVille_nom(            DataNormalizer.normalize( get_ville().getVille_nom()));
		get_ville().setVille_pays(           DataNormalizer.normalize( get_ville().getVille_pays()));
		get_ville().setVille_province(       DataNormalizer.normalize( get_ville().getVille_province()));
		get_groupe().setGroupe_nom(          DataNormalizer.normalize( get_groupe().getGroupe_nom()));
		get_client().setClient_commentaire(  DataNormalizer.normalize( get_client().getClient_commentaire()));
		get_client().setClient_nom(          DataNormalizer.normalize( get_client().getClient_nom()));
		get_client().setClient_prenom(       DataNormalizer.normalize( get_client().getClient_prenom()));
		for (Telephone tel : get_telephones()){
			tel.setTelephone_numero(DataNormalizer.normalize(tel.getTelephone_numero()));
		}
	}
	
	public static void main(String ... args) throws FileNotFoundException, SQLException, IOException{
		System.out.println("connection");
		Connection connection = SqlFactory.connect(SqlFactory.loadProperties("hotel_orford_import.xml"));
		
		System.out.println("lecture massive");
		List<ReadClient> imported_client = ReadClient.loadFrom(connection);
		
		System.out.println("deconnection");
		connection.close();
		
		System.out.println("nombre de client lu : " + imported_client.size());
		
		System.out.println("preparation à la normalization des données");
		for (ReadClient rc : imported_client){
			rc.normalisationDesDonnees();
		}	
	}

}
