package controler.gestion;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;

import language.Language;
import view.vueGestionTaxe.VueGestionTaxe;

import commander.Commander;

import controler.Message;
import database.crud.TaxeCrud;
import database.dataset.Taxe;

public class ControlerGestionTaxe {
	public static final Message reveil = new Message(){
		public void trigger(Object caster) throws Exception {
			if (!(caster instanceof VueGestionTaxe))
				return;
			Connection connection = Commander.getInstance().getConnectionBD();
			VueGestionTaxe vue = (VueGestionTaxe)caster;
			vue.ClearInfo();
			TaxeCrud taxeCrud = new TaxeCrud(connection);
			Taxe taxe = taxeCrud.readLast();
			
			vue.setTextFieldHebergement(taxe.getTaxe_hebergement().toPlainString());
			vue.setTextFieldInfoHebergement(taxe.getTaxe_hebergement().toPlainString());
			vue.setTextFieldInfoTPS(taxe.getTaxe_tps().toPlainString());
			vue.setTextFieldTPS(taxe.getTaxe_tps().toPlainString());
			vue.setTextFieldInfoTVQ(taxe.getTaxe_tvq().toPlainString());
			vue.setTextFieldTVQ(taxe.getTaxe_tvq().toPlainString());
		}
	};
	
	public static final Message enregistrer = new Message(){
		public void trigger(Object caster) throws Exception {
			if (!(caster instanceof VueGestionTaxe))
				return;
			VueGestionTaxe vue = (VueGestionTaxe)caster;
			Connection connection = Commander.getInstance().getConnectionBD();
			Language lang = Commander.getInstance().getLanguage();
			Commander.getInstance().getVuePrincipale().setModifiedTab(lang.getPrincipaleGestion(), lang.getGestionTaxes(),false);	
			
			TaxeCrud taxeCrud = new TaxeCrud(connection);
			
			if(vue.getTextFieldTPSIsValid() && vue.getTextFieldTVQIsValid() && vue.getTextFieldHebergementIsValid()){
				BigDecimal taxe_tps         = new BigDecimal(vue.getTextFieldTPS());
				BigDecimal taxe_tvq         = new BigDecimal(vue.getTextFieldTVQ());
				BigDecimal taxe_hebergement = new BigDecimal(vue.getTextFieldHebergement());
				java.util.Calendar cal  = java.util.Calendar.getInstance();
				java.util.Date utilDate = cal.getTime();
				java.sql.Date sqlDate   = new Date(utilDate.getTime());
				
				connection.setAutoCommit(false);
				Taxe taxe = new Taxe(taxeCrud.getNewSequenceId(), taxe_tps, taxe_tvq, taxe_hebergement, sqlDate);
				System.out.print(taxeCrud.create(taxe));
				connection.commit();
				connection.setAutoCommit(true);

				taxe = taxeCrud.readLast();
				
				vue.setTextFieldHebergement(taxe.getTaxe_hebergement().toPlainString());
				vue.setTextFieldInfoHebergement(taxe.getTaxe_hebergement().toPlainString());
				vue.setTextFieldInfoTPS(taxe.getTaxe_tps().toPlainString());
				vue.setTextFieldTPS(taxe.getTaxe_tps().toPlainString());
				vue.setTextFieldInfoTVQ(taxe.getTaxe_tvq().toPlainString());
				vue.setTextFieldTVQ(taxe.getTaxe_tvq().toPlainString());
				
				Commander.getInstance().getVuePrincipale().setModifiedTab(lang.getPrincipaleGestion(), lang.getGestionTaxes(),false);
			}
			else
			{
				return;
			}
		}
	};

	public static final Message annuler = new Message(){
		public void trigger(Object caster) throws Exception {
			if (!(caster instanceof VueGestionTaxe))
				return;
			
			Connection connection = Commander.getInstance().getConnectionBD();
			VueGestionTaxe vue = (VueGestionTaxe)caster;
			vue.ClearInfo();
			TaxeCrud taxeCrud = new TaxeCrud(connection);
			Taxe taxe = taxeCrud.readLast();
			
			vue.setTextFieldHebergement(taxe.getTaxe_hebergement().toPlainString());
			vue.setTextFieldInfoHebergement(taxe.getTaxe_hebergement().toPlainString());
			vue.setTextFieldInfoTPS(taxe.getTaxe_tps().toPlainString());
			vue.setTextFieldTPS(taxe.getTaxe_tps().toPlainString());
			vue.setTextFieldInfoTVQ(taxe.getTaxe_tvq().toPlainString());
			vue.setTextFieldTVQ(taxe.getTaxe_tvq().toPlainString());
			
			Language lang = Commander.getInstance().getLanguage();
			Commander.getInstance().getVuePrincipale().setModifiedTab(lang.getPrincipaleGestion(), lang.getGestionTaxes(),false);
		}
	};
	
	public static final Message dataChanged = new Message(){
		public void trigger(Object caster) throws Exception {
			if (!(caster instanceof VueGestionTaxe))
				return;
			
			Language lang = Commander.getInstance().getLanguage();
			Commander.getInstance().getVuePrincipale().setModifiedTab(lang.getPrincipaleGestion(), lang.getGestionTaxes(),true);
		}
	};
}
