package view.commande;

import controller.commande.Commande_Controller;
import controller.commande.Commande_Pdf_Controller;
import controller.history.Application_Controller;
import net.miginfocom.swing.MigLayout;
import org.sig.fw.bddclink.web.BddcBrowserAccess;
import org.sig.fw.db.bddc.dao.Rdc_DBAO;
import org.sig.fw.db.corrida.dao.Commande_Content_DBAO;
import org.sig.fw.db.corrida.dao.Commande_DBAO;
import org.sig.fw.db.corrida.dao.Facture_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.Commande_Content;
import org.sig.fw.layout.component.SButton;
import org.sig.fw.layout.component.SLabel;
import org.sig.fw.layout.contener.SInternalFrame;
import org.sig.fw.layout.pane.SOptionPane;
import org.sig.fw.layout.pane.SPanel;
import org.sig.fw.layout.pane.SScrollPane;
import org.sig.fw.sig.model.Account;
import org.sig.fw.utility.date.SDate;
import org.sig.fw.utility.number.NumberTools;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class Commande_View extends SInternalFrame 
{
	private static final long serialVersionUID = 2845037525247223332L;
	
	private Commande_Controller ctrl_commande;

	private SLabel jl_head;
	private SLabel jl_dossier_number;
	private SLabel jl_client;
	private SLabel jl_tranche_number;
	private SLabel jl_responsible;
	private SLabel jl_reference;
	private SLabel jl_dossier_name;
	private SLabel jl_signature_date;
	private SLabel jl_factured_date;
	private SLabel jl_payment_mode;
	private SLabel jl_is_auto_liquidation;
	private SPanel jp_commande_content;
	private SLabel jl_total_ht;
	private SLabel jl_total_tva;
	private SLabel jl_total_net;
	private SLabel jl_direct_payment;

	public Commande_View(Commande_Controller ctrl_commande) 
	{
		super("Commande " + ctrl_commande.getModel().getSociety().getName());
		jp_main_content.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBounds(100, 100, 980, 640);
		this.ctrl_commande = ctrl_commande;
		jp_main_content.setLayout(new MigLayout("gap 0px 0px, insets 1px 1px", "[][][grow][][][10px:10px:10px][]", "[][][][grow][][][]"));
		
		SButton btnNewButton_1 = new SButton("");
		btnNewButton_1.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) 
			{
				new Commande_Pdf_Controller(ctrl_commande.getModel(), new Commande_Content_DBAO("cc").getAllWithCommande(ctrl_commande.getModel()));
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(Commande_View.class.getResource("/icons/pdf.png")));
		jp_main_content.add(btnNewButton_1, "cell 0 0,alignx right");
		
		jl_head = new SLabel("", SLabel.TYPE_TITLE_BOLD);
		jp_main_content.add(jl_head, "cell 1 0 3 1,alignx center");
		
		SButton btnNewButton = new SButton("", "Modifier la Commande");
		btnNewButton.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				if(((Commande) new Commande_DBAO("com").getWithId(ctrl_commande.getModel().getDbId(), true)).getState().getDbId() != State_DBAO.SOLDE)
				{
					if(new Facture_DBAO("fact").getWithCommande(ctrl_commande.getModel()).size() == 0)
					{
						Account account = new Commande_DBAO("com").getLock(ctrl_commande.getModel());
						if(account == null || account.equals(ctrl_commande.getApplicationController().getConnectedAccount()))
						{
							if(ctrl_commande.getApplicationController().openView(Application_Controller.ADD_COMMANDE_VIEW, ctrl_commande.getModel(), null))
							{
								dispose();
							}
						}
						else
						{
							SOptionPane.showErrorMessage("Ouverture du formulaire de Commande", "La Commande est vérouillée par " + account.getCompleteName());
						}
					}
					else
					{
						SOptionPane.showErrorMessage("Ouverture du formulaire de Commande", "La Commande n'est pas modifiable puisqu'elle possède des factures");
					}
				}
				else
				{
					SOptionPane.showErrorMessage("Ouverture du formulaire de Commande", "La Commande n'est pas modifiable puisque SOLDE");
				}
			}
		});
		
		SButton button_3 = new SButton("", "Ajouter une facture");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(ctrl_commande.getApplicationController().openView(Application_Controller.ADD_FACTURE_VIEW, (Commande) ctrl_commande.getModel(), null)) {
					dispose();
				}
			}
		});
		button_3.setIcon(new ImageIcon(Commande_View.class.getResource("/icons/facture.png")));
		jp_main_content.add(button_3, "cell 4 0,alignx right,growy");
		btnNewButton.setIcon(new ImageIcon(Commande_View.class.getResource("/icons/modify.png")));
		jp_main_content.add(btnNewButton, "cell 6 0");
		
		SPanel panel = new SPanel();
		panel.setBorder(new TitledBorder(null, "Donn\u00E9es BDDC", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		jp_main_content.add(panel, "cell 0 1 2 1,grow");
		panel.setLayout(new MigLayout("gap 5px 0px, insets 1px 1px", "[][grow][]", "[][][][]"));
		
		SLabel label = new SLabel("N° Dossier", SLabel.TYPE_COMMUN);
		panel.add(label, "cell 0 0");
		
		jl_dossier_number = new SLabel("", SLabel.TYPE_COMMUN);
		panel.add(jl_dossier_number, "cell 1 0");
		
		SButton button = new SButton("", "");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				new BddcBrowserAccess().goToDossierView(ctrl_commande.getModel().getDossier().getDbId());
			}
		});
		button.setIcon(new ImageIcon(Commande_View.class.getResource("/org/sig/fw/bddclink/icon/sondefor_little.png")));
		panel.add(button, "cell 2 0,growx");
		
		SLabel label_1 = new SLabel("Client", SLabel.TYPE_COMMUN);
		panel.add(label_1, "cell 0 1");
		
		jl_client = new SLabel("", SLabel.TYPE_COMMUN);
		panel.add(jl_client, "cell 1 1");
		
		SButton button_1 = new SButton("", "");
		button_1.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				ctrl_commande.getApplicationController().openView(Application_Controller.CLIENT, ctrl_commande.getModel().getCc().getCorridaClient(), null);
			}
		});
		button_1.setIcon(new ImageIcon(Commande_View.class.getResource("/icons/logo_corrida_20x20.png")));
		panel.add(button_1, "cell 2 1,alignx right");
		
		SLabel label_2 = new SLabel("Tranche", SLabel.TYPE_COMMUN);
		panel.add(label_2, "cell 0 2");
		
		jl_tranche_number = new SLabel("", SLabel.TYPE_COMMUN);
		panel.add(jl_tranche_number, "cell 1 2");
		
		SButton button_2 = new SButton("", "");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				new BddcBrowserAccess().goToTrancheView(new Rdc_DBAO("rdc").getRdcId(ctrl_commande.getModel().getDossier()), ctrl_commande.getModel().getConstructionSite().getDbId());
			}
		});
		button_2.setIcon(new ImageIcon(Commande_View.class.getResource("/org/sig/fw/bddclink/icon/sondefor_little.png")));
		panel.add(button_2, "cell 2 2,growx");
		
		SLabel label_3 = new SLabel("Responsable", SLabel.TYPE_COMMUN);
		panel.add(label_3, "cell 0 3");
		
		jl_responsible = new SLabel("", SLabel.TYPE_COMMUN);
		panel.add(jl_responsible, "cell 1 3 2 1");
		
		SPanel panel_1 = new SPanel();
		panel_1.setBorder(new TitledBorder(null, "Donn\u00E9es de la Commande", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		jp_main_content.add(panel_1, "cell 2 1 5 1,grow");
		panel_1.setLayout(new MigLayout("gap 10px 3px, insets 1px 1px", "[][grow]", "[grow][][][][][]"));
		
		SLabel lblNewLabel = new SLabel("Référence", SLabel.TYPE_COMMUN);
		panel_1.add(lblNewLabel, "cell 0 0");
		
		jl_reference = new SLabel("", SLabel.TYPE_COMMUN);
		panel_1.add(jl_reference, "cell 1 0");
		
		SLabel label_4 = new SLabel("Date de Signature", 0);
		panel_1.add(label_4, "cell 0 1");
		
		jl_signature_date = new SLabel("", 0);
		panel_1.add(jl_signature_date, "cell 1 1");
		
		SLabel label_5 = new SLabel("Date de Facturation", 0);
		panel_1.add(label_5, "cell 0 2");
		
		jl_factured_date = new SLabel("", 0);
		panel_1.add(jl_factured_date, "cell 1 2");
		
		SLabel label_6 = new SLabel("Mode de R\u00E9glement", 0);
		panel_1.add(label_6, "cell 0 3");
		
		jl_payment_mode = new SLabel("", 0);
		panel_1.add(jl_payment_mode, "cell 1 3");
		
		jl_is_auto_liquidation = new SLabel("", 0);
		panel_1.add(jl_is_auto_liquidation, "cell 0 4 2 1");
		
		jl_direct_payment = new SLabel("", 0);
		jl_direct_payment.setText("");
		panel_1.add(jl_direct_payment, "cell 0 5 2 1");
		
		SPanel panel_2 = new SPanel();
		panel_2.setBorder(new TitledBorder(null, "Nom", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		jp_main_content.add(panel_2, "cell 0 2 7 1,grow");
		panel_2.setLayout(new MigLayout("gap 1px 1px, insets 1px 1px", "[grow]", "[]"));
		
		jl_dossier_name = new SLabel("", SLabel.TYPE_COMMUN_BOLD);
		panel_2.add(jl_dossier_name, "cell 0 0,alignx center");
		
		SPanel panel_3 = new SPanel();
		panel_3.setBorder(new TitledBorder(null, "Contenu", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		jp_main_content.add(panel_3, "cell 0 3 7 1,grow");
		panel_3.setLayout(new MigLayout("gap 1px 1px, insets 1px 1px", "[grow]", "[grow]"));
		
		SScrollPane scrollPane = new SScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		panel_3.add(scrollPane, "cell 0 0,grow");
		
		jp_commande_content = new SPanel();
		scrollPane.setViewportView(jp_commande_content);
		jp_commande_content.setLayout(new MigLayout("gap 0px 0px, insets 0px 0px", "[grow]", "[]"));
		
		SLabel label_7 = new SLabel("Total HT", SLabel.TYPE_COMMUN);
		jp_main_content.add(label_7, "cell 3 4,alignx left");
		
		jl_total_ht = new SLabel("", SLabel.TYPE_COMMUN);
		jp_main_content.add(jl_total_ht, "cell 4 4 2 1,alignx right");
		
		SLabel label_8 = new SLabel("Total TVA", SLabel.TYPE_COMMUN);
		jp_main_content.add(label_8, "cell 3 5,alignx left");
		
		jl_total_tva = new SLabel("", SLabel.TYPE_COMMUN);
		jp_main_content.add(jl_total_tva, "cell 4 5,alignx right");
		
		SLabel label_9 = new SLabel("Total NET   ", SLabel.TYPE_COMMUN);
		jp_main_content.add(label_9, "cell 3 6,alignx left");
		
		jl_total_net = new SLabel("", SLabel.TYPE_COMMUN);
		jp_main_content.add(jl_total_net, "cell 4 6 2 1,alignx right");

		SwingUtilities.invokeLater(new Runnable()
		{
			@Override
			public void run() 
			{
				insertData();
			}
		});
	}

	public void insertData()
	{
		Commande com = ctrl_commande.getModel();
		
		jl_head.setText("Commande N° " + com.getCommandeNumber());
		
		jl_dossier_number.setText(com.getDossier().getReferenceNumber());
		jl_client.setText(com.getCc().getBddcClient().getCompany());
		jl_tranche_number.setText(Integer.toString(com.getTrancheNumber()));
		jl_responsible.setText(com.getConstructionSite().getResponsible().getName());
		
		jl_reference.setText("<html>" + com.getCommandeReference().getShowText() + "</html>");
		
		jl_dossier_name.setText(com.getDossier().getLocalisation() + " / " + com.getDossier().getName());
		
		jl_signature_date.setText(com.getSignatureDate().getFormattedDate(SDate.FORMAT_FR));
		jl_factured_date.setText(com.getFacturedDate().getFormattedDate(SDate.FORMAT_FR));
		jl_payment_mode.setText(com.getPaymentMode().getShortValue());
		jl_is_auto_liquidation.setText(com.getAutoLiquidation() ? "Auto-liquidation" : "");
		jl_direct_payment.setText(com.getDirectPayment() ? "Paiement Direct" : "");
		
		jp_commande_content.removeAll();
		jp_commande_content.repaint();
		Commande_Content_View ccvhead = new Commande_Content_View(null);
		jp_commande_content.add(ccvhead, "cell 0 0, growx");
		List<Commande_Content> commande_contents = new Commande_Content_DBAO("cc").getAllWithCommande(com);
		for(int i = 0; i < commande_contents.size(); i++)
		{
			Commande_Content_View ccv = new Commande_Content_View(commande_contents.get(i));
			jp_commande_content.add(ccv, "cell 0 " + (i+1) + ", growx");
		}
		jp_commande_content.repaint();
		jp_commande_content.revalidate();
		
		jl_total_ht.setText(NumberTools.getNumberFormat(2).format(com.getAmountHt()) + " €");
		if(com.getAutoLiquidation())
			jl_total_tva.setText("Auto Liquidation");
		else
			jl_total_tva.setText(NumberTools.getNumberFormat(2).format(com.getAmountNet() - com.getAmountHt()) + " €");
		jl_total_net.setText(NumberTools.getNumberFormat(2).format(com.getAmountNet()) + " €");
		
	}
}
