package view.facture;

import controller.facture.Add_Facture_Content_Controller;
import org.sig.fw.db.corrida.dao.Facture_Content_DBAO;
import org.sig.fw.layout.component.STextField;
import org.sig.fw.layout.component.SToggleButton;
import org.sig.fw.layout.pane.SPanel;
import org.sig.fw.utility.number.NumberTools;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.HashMap;

public class Add_Facture_Content_View extends SPanel 
{
	private static final long serialVersionUID = -5060610488242682467L;
	
	public static int CELL_WIDTH = 120;
	public static int TOGGLE_WIDTH = 40;
	
	private Add_Facture_Content_Controller ctrl_facture_content;
	
	private STextField jtf_designation;
	private STextField jtf_montant_marche;
	private STextField jtf_montant_cumule;
	private STextField jtf_montant_precedent;
	
	private STextField jtf_avancement_cumule;
	private STextField jtf_avancement_precedent;
	private STextField jtf_avancement_present;
	
	private SToggleButton jtg_percent_unite;

	private STextField jtf_montant_ht;
	
	private double av_cumule = 0;


	public Add_Facture_Content_View(Add_Facture_Content_Controller ctrl_facture_content) 
	{
		super();
		addComponentListener(new ComponentAdapter()
		{
			@Override
			public void componentResized(ComponentEvent e) 
			{
				computeSize();
			}
		});
		this.ctrl_facture_content = ctrl_facture_content;
		setLayout(null);
		
		
		jtf_designation = new STextField("");
		jtf_designation.setEditable(false);
		jtf_designation.setFocusable(false);
		jtf_designation.setHorizontalAlignment(SwingConstants.LEFT);
		add(jtf_designation);
		
		jtf_montant_marche = new STextField("");
		jtf_montant_marche.setEditable(false);
		jtf_montant_marche.setFocusable(false);
		jtf_montant_marche.setHorizontalAlignment(SwingConstants.RIGHT);
		add(jtf_montant_marche);
		
		jtf_montant_cumule = new STextField("");
		jtf_montant_cumule.setEditable(false);
		jtf_montant_cumule.setFocusable(false);
		jtf_montant_cumule.setHorizontalAlignment(SwingConstants.RIGHT);
		add(jtf_montant_cumule);
		
		jtf_montant_precedent = new STextField("");
		jtf_montant_precedent.setEditable(false);
		jtf_montant_precedent.setFocusable(false);
		jtf_montant_precedent.setHorizontalAlignment(SwingConstants.RIGHT);
		add(jtf_montant_precedent);
		
		
		jtf_avancement_cumule = new STextField("");
		jtf_avancement_cumule.setEditable(false);
		jtf_avancement_cumule.setFocusable(false);
		jtf_avancement_cumule.setHorizontalAlignment(SwingConstants.RIGHT);
		add(jtf_avancement_cumule);
		
		jtf_avancement_precedent = new STextField("");
		jtf_avancement_precedent.setEditable(false);
		jtf_avancement_precedent.setFocusable(false);
		jtf_avancement_precedent.setHorizontalAlignment(SwingConstants.RIGHT);
		add(jtf_avancement_precedent);
		
		jtf_avancement_present = new STextField("");
		jtf_avancement_present.addFocusListener(new FocusAdapter() 
		{
			@Override
			public void focusLost(FocusEvent arg0)
			{
				calcPresent();
				/*
				String unit_price = deleteSpace(jtf_unit_price.getText());				
				if(NumberTools.checkStringToDouble(unit_price))
				{
					if(ctrl_commande_content.getModel().getUnitPrice() != Double.parseDouble(unit_price))
						ctrl_commande_content.setIsSave(false);
					if(Double.parseDouble(unit_price) == 0)
						jtf_unit_price.setText("");
					else
						jtf_unit_price.setText(NumberTools.getNumberFormat(2).format(Double.parseDouble(unit_price)));
					calcAmount();
					ctrl_commande_content.getModel().setUnitPrice(Double.parseDouble(unit_price));
				}
				else
				{
					if(ctrl_commande_content.getModel().getUnitPrice() != 0)
						ctrl_commande_content.setIsSave(false);
					jtf_unit_price.setText("");
					ctrl_commande_content.getModel().setUnitPrice(0);
				}
				ctrl_commande_content.updateTotal();*/
			}
		});
		jtf_avancement_present.setHorizontalAlignment(SwingConstants.RIGHT);
		add(jtf_avancement_present);
		
		jtg_percent_unite = new SToggleButton("%", false)
		{
			private static final long serialVersionUID = -8573465418270642485L;

			@Override
			public void setSelected()
			{
				jtg_percent_unite.setText("U");
				calcPresent();
			}
			
			@Override
			public void setUnselected()
			{
				jtg_percent_unite.setText("%");
				calcPresent();
			}
		};
		add(jtg_percent_unite);
		
		jtf_montant_ht = new STextField("");
		jtf_montant_ht.addFocusListener(new FocusAdapter() 
		{
			@Override
			public void focusLost(FocusEvent arg0)
			{
				String amount = deleteSpace(jtf_montant_ht.getText());
				if(NumberTools.checkStringToDouble(amount))
				{
					double value = Double.parseDouble(amount) / ctrl_facture_content.getModel().getCommandeContent().getAmountHt();
					if(jtg_percent_unite.isSelected())
						jtf_avancement_present.setText(NumberTools.getNumberFormat(8).format(value*ctrl_facture_content.getModel().getCommandeContent().getQuantity()));
					else
						jtf_avancement_present.setText(NumberTools.getNumberFormat(8).format(value*100));
					calcPresent();
				}
				else
				{
					calcPresent();
				}
			}
		});
		jtf_montant_ht.setHorizontalAlignment(SwingConstants.RIGHT);
		add(jtf_montant_ht);
		
		computeSize();
		
		jtf_designation.setText(this.ctrl_facture_content.getModel().getCommandeContent().getReference().getLongText());
		
		if(jtf_designation.getText().isEmpty())
		{
			jtg_percent_unite.setText("");
			jtg_percent_unite.setEnabled(false);
			jtf_avancement_present.setEditable(false);
			jtf_avancement_present.setFocusable(false);
			jtf_montant_ht.setEnabled(false);
			jtf_montant_ht.setFocusable(false);
		}
		else
		{
			if(ctrl_facture_content.getModel().getCommandeContent().getReference().getTypeId() > 0)
			{
				configureTextField(jtf_designation);
				jtf_designation.setText(ctrl_facture_content.getModel().getCommandeContent().getReference().getLongText());
				configureTextField(jtf_montant_cumule);
				configureTextField(jtf_avancement_cumule);
				configureTextField(jtf_montant_precedent);
				configureTextField(jtf_avancement_precedent);
				configureTextField(jtf_avancement_present);
				configureTextField(jtf_montant_ht);
				jtf_montant_ht.setEditable(false);
				configureTextField(jtf_montant_marche);
				jtg_percent_unite.setText("");
				jtg_percent_unite.setEnabled(false);	
			}
			else if(ctrl_facture_content.getModel().getCommandeContent().getAmountHt() != 0)
			{
				jtf_montant_marche.setText(NumberTools.getNumberFormat(2).format(ctrl_facture_content.getModel().getCommandeContent().getAmountHt()));
				
				HashMap<String, Double> cumule = new Facture_Content_DBAO("fc").getAvancement(ctrl_facture_content.getModel(), 0);

				if(cumule.isEmpty() && ctrl_facture_content.getModel().getDbId() == -1)
				{
					jtf_montant_cumule.setText("0,00");
					jtf_avancement_cumule.setText("0,00000000");
					jtf_montant_precedent.setText("0,00");
					jtf_avancement_precedent.setText("0,00000000");
					
					jtf_avancement_present.setText("0,00000000");
					jtf_montant_ht.setText("0,00");
				}
				else
				{
					double present = 0.0;
					double montant_ht = 0.0;
					if(cumule.get("percent") != null)
					{
						present = ctrl_facture_content.getModel().getPresentPercent();
						montant_ht = present*ctrl_facture_content.getModel().getCommandeContent().getAmountHt()/100;
						av_cumule = cumule.get("percent");
						jtg_percent_unite.setSelected(false);
						jtg_percent_unite.setEnabled(false);
						double amount_cumule = (av_cumule+present)*ctrl_facture_content.getModel().getCommandeContent().getAmountHt()/100;
						jtf_montant_cumule.setText(NumberTools.getNumberFormat(2).format(amount_cumule));
						ctrl_facture_content.getModel().setAmountCumule(amount_cumule);
					}
					else if(cumule.get("unit") != null)
					{
						present = ctrl_facture_content.getModel().getPresentUnit();
						montant_ht = present*ctrl_facture_content.getModel().getCommandeContent().getUnitPrice();
						av_cumule = cumule.get("unit");
						jtg_percent_unite.setSelected(true);
						jtg_percent_unite.setEnabled(false);
						double amount_cumule = (av_cumule+present)*ctrl_facture_content.getModel().getCommandeContent().getUnitPrice();
						jtf_montant_cumule.setText(NumberTools.getNumberFormat(2).format(amount_cumule));
						ctrl_facture_content.getModel().setAmountCumule(amount_cumule);
					}
					else
					{
						if(ctrl_facture_content.getModel().getPresentPercent() != 0)
						{
							present = ctrl_facture_content.getModel().getPresentPercent();
							montant_ht = present*ctrl_facture_content.getModel().getCommandeContent().getAmountHt()/100;
							double amount_cumule = (av_cumule+present)*ctrl_facture_content.getModel().getCommandeContent().getAmountHt()/100;
							jtf_montant_cumule.setText(NumberTools.getNumberFormat(2).format(amount_cumule));
							ctrl_facture_content.getModel().setAmountCumule(amount_cumule);
						}
						else
						{
							present = ctrl_facture_content.getModel().getPresentUnit();
							montant_ht = present*ctrl_facture_content.getModel().getCommandeContent().getUnitPrice();
							double amount_cumule = (av_cumule+present)*ctrl_facture_content.getModel().getCommandeContent().getUnitPrice();
							jtf_montant_cumule.setText(NumberTools.getNumberFormat(2).format(amount_cumule));
							ctrl_facture_content.getModel().setAmountCumule(amount_cumule);
							jtg_percent_unite.setSelected(true);
							jtg_percent_unite.setText("U");
						}
					}
					jtf_avancement_cumule.setText(NumberTools.getNumberFormat(8).format(av_cumule+present));
					ctrl_facture_content.getModel().setAvCumule(av_cumule+present);
					
					HashMap<String, Double> precedent = new Facture_Content_DBAO("fc").getAvancement(ctrl_facture_content.getModel(), 1);
					double av_precedent = 0;
					if(precedent.get("percent") != null)
					{
						av_precedent = precedent.get("percent");
						double amount_precedent = av_precedent*ctrl_facture_content.getModel().getCommandeContent().getAmountHt()/100;
						jtf_montant_precedent.setText(NumberTools.getNumberFormat(2).format(amount_precedent));
						ctrl_facture_content.getModel().setAmountPrecedent(amount_precedent);
					}
					else if(precedent.get("unit") != null)
					{
						av_precedent = precedent.get("unit");
						double amount_precedent = av_precedent*ctrl_facture_content.getModel().getCommandeContent().getUnitPrice();
						jtf_montant_precedent.setText(NumberTools.getNumberFormat(2).format(amount_precedent));
						ctrl_facture_content.getModel().setAmountPrecedent(amount_precedent);
					}
					else
					{
						jtf_montant_precedent.setText(NumberTools.getNumberFormat(2).format(0.0));
						ctrl_facture_content.getModel().setAmountPrecedent(0.0);
					}
					jtf_avancement_precedent.setText(NumberTools.getNumberFormat(8).format(av_precedent));
					ctrl_facture_content.getModel().setAvPrecedent(av_precedent);
					
					jtf_avancement_present.setText("0,00000000");
					jtg_percent_unite.setText(jtg_percent_unite.isSelected() ? "U" : "%");
					
					jtf_avancement_present.setText(NumberTools.getNumberFormat(8).format(present));
					jtf_montant_ht.setText(NumberTools.getNumberFormat(2).format(montant_ht));
				}
			}
			else
			{
				jtg_percent_unite.setText("");
				jtg_percent_unite.setEnabled(false);
				jtf_avancement_present.setEditable(false);
				jtf_avancement_present.setFocusable(false);
				jtf_montant_ht.setEditable(false);
				jtf_montant_ht.setFocusable(false);
			}
		}
	}
	
	public void computeSize()
	{
		int height = 31;
		int x = 0;
		int y = 0;

		int width = getWidth() - 7*CELL_WIDTH - TOGGLE_WIDTH;
		jtf_designation.setBounds(x, y, width, height);
		x += width;
		
		jtf_montant_marche.setBounds(x, y, CELL_WIDTH, height);
		x += CELL_WIDTH;
		jtf_montant_cumule.setBounds(x, y, CELL_WIDTH, height);
		x += CELL_WIDTH;
		jtf_montant_precedent.setBounds(x, y, CELL_WIDTH, height);
		x += CELL_WIDTH;
		
		jtf_avancement_cumule.setBounds(x, y, CELL_WIDTH, height);
		x += CELL_WIDTH;
		jtf_avancement_precedent.setBounds(x, y, CELL_WIDTH, height);
		x += CELL_WIDTH;
		jtf_avancement_present.setBounds(x, y, CELL_WIDTH, height);
		x += CELL_WIDTH;
		jtg_percent_unite.setBounds(x, y, TOGGLE_WIDTH, height);
		x += TOGGLE_WIDTH;
		
		jtf_montant_ht.setBounds(x, y, CELL_WIDTH, height);
	}
	
	private void configureTextField(STextField stf)
	{
		stf.setBackground(Color.LIGHT_GRAY);
		stf.setFocusable(false);
		stf.setText("");
	}
	
	private String deleteSpace(String value)
	{
		return value.replaceAll(",", "\\.").replaceAll("\\s", "").replaceAll("[\u00a0\u202f]", "");
	}
	
	public void setAmount(double total_marche, double total_cumule, double total_precedent, double total_group)
	{
		jtf_montant_marche.setText(NumberTools.getNumberFormat(2).format(total_marche));
		jtf_montant_cumule.setText(NumberTools.getNumberFormat(2).format(total_cumule));
		jtf_montant_precedent.setText(NumberTools.getNumberFormat(2).format(total_precedent));
		jtf_montant_ht.setText(NumberTools.getNumberFormat(2).format(total_group));
		
		ctrl_facture_content.getModel().setAmountCumule(total_cumule);
		ctrl_facture_content.getModel().setAmountPrecedent(total_precedent);
		ctrl_facture_content.getModel().setAmountHt(total_group);
	}
	
	public void calcPresent()
	{
		String value = deleteSpace(jtf_avancement_present.getText());
		if(NumberTools.checkStringToDouble(value))
		{
			double present = Double.parseDouble(value);
			jtf_avancement_cumule.setText(NumberTools.getNumberFormat(8).format(av_cumule+present));
			ctrl_facture_content.getModel().setAvCumule(av_cumule+present);
			//U
			if(jtg_percent_unite.isSelected())
			{
				if(av_cumule+present > ctrl_facture_content.getModel().getCommandeContent().getQuantity())
				{
					jtf_avancement_present.setBackground(Color.RED);
					jtf_avancement_present.setForeground(Color.WHITE);
					jtf_avancement_cumule.setBackground(Color.RED);
					jtf_avancement_cumule.setForeground(Color.WHITE);
					jtf_montant_ht.setForeground(Color.WHITE);
					jtf_montant_ht.setBackground(Color.RED);
					jtf_montant_cumule.setForeground(Color.WHITE);
					jtf_montant_cumule.setBackground(Color.RED);
				}
				else
				{
					jtf_avancement_present.setBackground(Color.WHITE);
					jtf_avancement_present.setForeground(Color.BLACK);
					jtf_avancement_cumule.setBackground(Color.WHITE);
					jtf_avancement_cumule.setForeground(Color.BLACK);
					jtf_montant_ht.setForeground(Color.BLACK);
					jtf_montant_ht.setBackground(Color.WHITE);
					jtf_montant_cumule.setForeground(Color.BLACK);
					jtf_montant_cumule.setBackground(Color.WHITE);
				}
				double montant_ht = ctrl_facture_content.getModel().getCommandeContent().getUnitPrice()*present;
				double montant_cumule = ctrl_facture_content.getModel().getCommandeContent().getUnitPrice()*(av_cumule+present);
				jtf_montant_ht.setText(NumberTools.getNumberFormat(2).format(montant_ht));
				jtf_montant_cumule.setText(NumberTools.getNumberFormat(2).format(montant_cumule));
				ctrl_facture_content.getModel().setAmountCumule(montant_cumule);
				
				if(ctrl_facture_content.getModel().getPresentUnit() != present)
				{
					ctrl_facture_content.getModel().setPresentPercent(0);
					ctrl_facture_content.getModel().setPresentUnit(present);
					ctrl_facture_content.setIsSave(false);
				}
			}
			else
			{
				if(av_cumule+present > 100)
				{
					jtf_avancement_present.setBackground(Color.RED);
					jtf_avancement_present.setForeground(Color.WHITE);
					jtf_avancement_cumule.setBackground(Color.RED);
					jtf_avancement_cumule.setForeground(Color.WHITE);
					jtf_montant_ht.setForeground(Color.WHITE);
					jtf_montant_ht.setBackground(Color.RED);
					jtf_montant_cumule.setForeground(Color.WHITE);
					jtf_montant_cumule.setBackground(Color.RED);
				}
				else
				{
					jtf_avancement_present.setBackground(Color.WHITE);
					jtf_avancement_present.setForeground(Color.BLACK);
					jtf_avancement_cumule.setBackground(Color.WHITE);
					jtf_avancement_cumule.setForeground(Color.BLACK);
					jtf_montant_ht.setForeground(Color.BLACK);
					jtf_montant_ht.setBackground(Color.WHITE);
					jtf_montant_cumule.setForeground(Color.BLACK);
					jtf_montant_cumule.setBackground(Color.WHITE);
				}
				double montant_ht = ctrl_facture_content.getModel().getCommandeContent().getAmountHt()*present/100;
				double montant_cumule = ctrl_facture_content.getModel().getCommandeContent().getAmountHt()*(av_cumule+present)/100;
				jtf_montant_ht.setText(NumberTools.getNumberFormat(2).format(montant_ht));
				jtf_montant_cumule.setText(NumberTools.getNumberFormat(2).format(montant_cumule));
				ctrl_facture_content.getModel().setAmountCumule(montant_cumule);
				
				if(ctrl_facture_content.getModel().getPresentPercent() != present)
				{
					ctrl_facture_content.getModel().setPresentPercent(present);
					ctrl_facture_content.getModel().setPresentUnit(0);
					ctrl_facture_content.setIsSave(false);
				}
			}
			jtf_avancement_present.setText(NumberTools.getNumberFormat(8).format(present));
		}
		else
		{
			jtf_avancement_present.setText("0,00000000");
			calcPresent();
		}
		ctrl_facture_content.updateTotal();
	}
}
