package controller.relance;

import org.sig.fw.db.corrida.dao.Definition_DBAO;
import org.sig.fw.db.corrida.dao.Facture_DBAO;
import org.sig.fw.db.corrida.dao.History_DBAO;
import org.sig.fw.db.corrida.dao.Relance_DBAO;
import org.sig.fw.db.corrida.dao.Relance_History_DBAO;
import org.sig.fw.db.corrida.dao.State_DBAO;
import org.sig.fw.db.corrida.model.Commande;
import org.sig.fw.db.corrida.model.Definition;
import org.sig.fw.db.corrida.model.Facture;
import org.sig.fw.db.corrida.model.History;
import org.sig.fw.db.corrida.model.Relance;
import org.sig.fw.db.corrida.model.Relance_History;
import org.sig.fw.layout.pane.SOptionPane;
import org.sig.fw.utility.date.SDate;

import controller.Controller_Add_Dialog;
import controller.history.Application_Controller;
import view.relance.Add_Relance_View;

public class Add_Relance_Controller extends Controller_Add_Dialog
{
	private Relance old_relance = null;
	private Relance relance = null;
	
	public Add_Relance_Controller(Application_Controller ctrl_application, Relance relance)
	{
		super(ctrl_application);
		
		this.model = new Relance_History(relance);
		((Relance_History)this.model).setAccount(ctrl_application.getConnectedAccount());
		((Relance_History)this.model).setActionDate(new SDate());
		
		this.old_relance = relance;
		
		this.vw_add_dialog = new Add_Relance_View(this);
		this.vw_add_dialog.setVisible(true);
	}
	
	public void setNewRelance(Relance relance)
	{
		this.relance = relance;
	}

	@Override
	public void save()
	{
		Relance_History rh = (Relance_History) model;
		
		if(rh.getDbId() == -1)
		{
			rh.setDbId(new Relance_History_DBAO("rh").insert(rh));
			
			History history = new History(-1, History_DBAO.RELANCE, rh, ctrl_application.getConnectedAccount(), new SDate(), "Ajout d'une Relance.");
			new History_DBAO("h").insert(history);
		}
		else
		{
			new Relance_History_DBAO("rh").update(rh);
			
			History history = new History(-1, History_DBAO.RELANCE, rh, ctrl_application.getConnectedAccount(), new SDate(), "Modification d'une Relance.");
			new History_DBAO("h").insert(history);
		}
		/* SI Pas de nouvelle relance et validation Facture */
		if(relance == null && old_relance.getType().getId() == 1)
		{
			/* On met à jour la date de validation Facture */
			if(old_relance.getObject() instanceof Facture)
			{
				Facture f = (Facture) old_relance.getObject();
				f.setFactureValidation(new SDate(rh.getActionDate()));
				new Facture_DBAO("fact").update(f);
				if(SOptionPane.YES_OPTION == SOptionPane.showYesNoMessage("Facture", "Voulez-vous passer la facture à l'état Validée ? (Etat actuel : "+f.getState().getLongValue()+")")) {
					new Facture_DBAO("fact").updateState(f, State_DBAO.VALIDEE);
				}
			}
			/* On ajoute une Relance pour le paiement de la Facture */
			if(((Relance_History)model).getObject() instanceof Facture)
			{
				Relance r = new Relance(
						-1,
						new Definition(-1, Definition_DBAO.RELANCE_TYPE, 2, ""),
						new Definition(-1, Definition_DBAO.RELANCE_MODE, 1, ""),
						((Relance_History)model).getObject(),
						new SDate(),
						((Facture)((Relance_History)model).getObject()).getEcheance(),
						((Relance_History)model).getCc());
				new Relance_DBAO("rel", -1).insert(r);
			}
		}
		
		new Relance_DBAO("rel", -1).delete(old_relance);

		if(relance != null)
		{
			new Relance_DBAO("rel", -1).insert(relance);
		}
		
		int dossier_id = -1;
		int construction_site_id = -1;
		
		if(rh.getObject() instanceof Commande)
		{
			dossier_id = ((Commande)rh.getObject()).getDossier().getDbId();
			construction_site_id = ((Commande)rh.getObject()).getConstructionSite().getDbId();
		}
		else if(rh.getObject() instanceof Facture)
		{
			dossier_id = ((Facture)rh.getObject()).getCommande().getDossier().getDbId();
			construction_site_id = ((Facture)rh.getObject()).getCommande().getConstructionSite().getDbId();
		}
		
		this.ctrl_application.activeUpdate("ADD_RELANCE", dossier_id, construction_site_id);
	}
}
