/**
 * 
 */
package view.vuePopup;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import language.Francais;
import language.Language;
import view.tool.moneyField.JMoneyField;

import com.ibm.icu.math.BigDecimal;

/**
 * @author alexis
 *
 */
public class PopupFacturePaiement extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8649204841458366492L;
	private JScrollPane scroll;
	private JPanel content;
	private GridBagConstraints constraint;
	private JMoneyField moneyfield,
						montantPresentChiffre,
		                montantTotalChiffre,
		                montantRestantChiffre;
	private JLabel montantPresentLabel,
	               montantTotalLabel,
	               montantRestantLabel,
	               montantAjouterLabel;
	private JButton ok,
	                cancel;
	private Language lang;
	
	public PopupFacturePaiement(Language lang) {
		initVariable(lang);
		initComponent();
		setMnemonic();
	}
	
	/**
	 * initialise variables
	 */
	private void initVariable(Language lang) {
		this.lang = lang;
		
		scroll = new JScrollPane();
		content = new JPanel(new GridBagLayout());
		constraint = new GridBagConstraints();
		
		moneyfield = new JMoneyField(16);
		
		montantPresentLabel = new JLabel(lang.getPopupFacturePaiementMontantPresent());
		montantAjouterLabel = new JLabel(lang.getPopupFacturePaiementMontantPayer());
        montantTotalLabel = new JLabel(lang.getPopupFacturePaiementMontantTotal());
        montantRestantLabel = new JLabel(lang.getPopupFacturePaiementMontantRestant());
        
		montantPresentChiffre = new JMoneyField(16);
		montantPresentChiffre.setEnabled(false);
        montantTotalChiffre = new JMoneyField(16);
        montantTotalChiffre.setEnabled(false);
        montantRestantChiffre = new JMoneyField(16);
        montantRestantChiffre.setEnabled(false);
        
        ok = new JButton(lang.getPopupFacturePaiementOk().replaceAll("%", ""));
        cancel = new JButton(lang.getPopupFacturePaiementCancel().replaceAll("%", ""));
        ok.addActionListener(this);
        cancel.addActionListener(this);
		
		scroll.setViewportView(content);
	}
	
	/**
	 * initialise the display
	 */
	private void initComponent(){
		constraint.anchor = GridBagConstraints.NORTH;
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 0;
		constraint.gridy = 0;
		constraint.insets = new Insets(0, 5, 10, 5);
		content.add(montantTotalLabel,constraint);
		
		constraint.anchor = GridBagConstraints.NORTHEAST;
		constraint.fill = GridBagConstraints.NONE;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 1;
		constraint.gridy = 0;
		constraint.insets = new Insets(0, 5, 10, 5);
		content.add(montantTotalChiffre,constraint);
		
		constraint.anchor = GridBagConstraints.NORTH;
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 0;
		constraint.gridy = 1;
		constraint.insets = new Insets(0, 5, 10, 5);
		content.add(montantPresentLabel,constraint);
		
		constraint.anchor = GridBagConstraints.NORTHEAST;
		constraint.fill = GridBagConstraints.NONE;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 1;
		constraint.gridy = 1;
		constraint.insets = new Insets(0, 5, 10, 5);
		content.add(montantPresentChiffre,constraint);
		
		constraint.anchor = GridBagConstraints.NORTH;
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 0;
		constraint.gridy = 2;
		constraint.insets = new Insets(0, 5, 10, 5);
		content.add(montantRestantLabel,constraint);
		
		constraint.anchor = GridBagConstraints.NORTHEAST;
		constraint.fill = GridBagConstraints.NONE;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 1;
		constraint.gridy = 2;
		constraint.insets = new Insets(0, 5, 10, 5);
		content.add(montantRestantChiffre,constraint);
		
		constraint.anchor = GridBagConstraints.NORTH;
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 0;
		constraint.gridy = 3;
		constraint.insets = new Insets(0, 5, 10, 5);
		content.add(montantAjouterLabel,constraint);
		
		constraint.anchor = GridBagConstraints.NORTHEAST;
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 1;
		constraint.gridy = 3;
		constraint.insets = new Insets(0, 5, 10, 5);
		content.add(moneyfield,constraint);
	}
	
	/**
	 * sets the mnemonics on the components
	 */
	private void setMnemonic(){
		ok.setMnemonic(lang.getPopupFacturePaiementOk().charAt(lang.getPopupFacturePaiementOk().indexOf("%")+1));
		cancel.setMnemonic(lang.getPopupFacturePaiementCancel().charAt(lang.getPopupFacturePaiementCancel().indexOf("%")+1));
	}
	
	/**
	 * @param montantPresent
	 * @param montantRestant
	 * @param montantTotal
	 * @return money ammount entered by user in popup
	 */
	public String getMoneyAmmount(BigDecimal montantPresent, BigDecimal montantRestant, BigDecimal montantTotal){
		montantPresentChiffre.setValue(montantPresent);
        montantTotalChiffre.setValue(montantTotal);
        montantRestantChiffre.setValue(montantRestant);
        moneyfield.setText(JMoneyField.ZERO_DOLLAR);
		
		JOptionPane.showOptionDialog(null, scroll, "Ajout",JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, new JButton[]{ok,cancel}, ok);
	    if(moneyfield.isValid())
	    	return moneyfield.getText();
	    else
	    	return JMoneyField.ZERO_DOLLAR;
	}
	
	/**
	 * hides the popup
	 */
	private void setVisibleFalse(){
		Window window = SwingUtilities.getWindowAncestor(ok);
        if(window != null) 
        	window.setVisible(false);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == ok)
		{
			if(moneyfield.isValid())
			{
				setVisibleFalse();
			}
			else
				JOptionPane.showMessageDialog(this, lang.getPopupFacturePaiementValidMontant());
		}
		else
		{
			setVisibleFalse();
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame a = new JFrame();
		JButton b = new JButton("click");
		b.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	PopupFacturePaiement popup = new PopupFacturePaiement(Francais.getInstance());
            	System.out.println(popup.getMoneyAmmount(new BigDecimal(0),new BigDecimal(10),new BigDecimal(12.03)));
            }
        });
		a.add(b);
		a.pack();
		a.setVisible(true);
	}
}