/**
 * 
 */
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 java.math.BigDecimal;

import javax.swing.JButton;
import javax.swing.JComboBox;
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;


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

	/**
	 * 
	 */
	public static class paiement{
		public String montant;
		public String methodePaiement;
		
		public paiement(String montant, String methodePaiement){
			this.montant = montant;
			this.methodePaiement = methodePaiement;
		}
	};
	
	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,
	               methodepaiementLabel;
	private JComboBox<String> comboModePaiement;
	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());
        methodepaiementLabel = new JLabel(lang.getPopupFacturePaiementMethodepaiement());
        
		montantPresentChiffre = new JMoneyField(16);
		montantPresentChiffre.setEnabled(false);
        montantTotalChiffre = new JMoneyField(16);
        montantTotalChiffre.setEnabled(false);
        montantRestantChiffre = new JMoneyField(16);
        montantRestantChiffre.setEnabled(false);
        
        comboModePaiement = new JComboBox<String>();
        
        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, 5, 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, 5, 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, 5, 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, 5, 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, 5, 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, 5, 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, 5, 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, 5, 5);
		content.add(moneyfield,constraint);
		
		constraint.anchor = GridBagConstraints.NORTH;
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 0;
		constraint.gridy = 4;
		constraint.insets  = new Insets(0, 5, 5, 5);
		content.add(methodepaiementLabel,constraint);
		
		constraint.anchor = GridBagConstraints.NORTHEAST;
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1;
		constraint.weighty = 1;
		constraint.gridx = 1;
		constraint.gridy = 4;
		constraint.insets  = new Insets(0, 5, 5, 5);
		content.add(comboModePaiement,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 paiement getMoneyAmmount(BigDecimal montantPresent, BigDecimal montantRestant, BigDecimal montantTotal, String[] ModePaiement){
		comboModePaiement.removeAllItems();
		for(int i = 0; i < ModePaiement.length; i++)
			comboModePaiement.addItem(ModePaiement[i]);
		
		montantPresentChiffre.setValue(montantPresent);
        montantTotalChiffre.setValue(montantTotal);
        montantRestantChiffre.setValue(montantRestant);
        moneyfield.setText(JMoneyField.ZERO_DOLLAR);
		
		int message = JOptionPane.showOptionDialog(null, scroll, "Ajout",JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, new String[]{ok.getText(),cancel.getText()}, ok.getText());

		if(message == 0 && moneyfield.isValid())
		    return new paiement(moneyfield.getText(), (String)comboModePaiement.getSelectedItem());
		else
	    	return new paiement(JMoneyField.ZERO_DOLLAR,"0");
	}
	
	/**
	 * 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), new String[]{"1","2"}));
            }
        });
		a.add(b);
		a.pack();
		a.setVisible(true);
	}
}