import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class IHMCoursier extends JFrame {
	
	public static void main(String[] args) {
		
		IHMCoursier fenetre = new IHMCoursier("Collissimo no Densetsu");
		fenetre.pack();
		fenetre.setVisible(true);
	
	}
	
	private static final long serialVersionUID = 1L;
	private JTextField volColis = new JTextField("0.0");
	private JTextField volumeSac = new JTextField("0.0");
	private JComboBox<String> choix = new JComboBox<String>();
	private JCheckBox urgente = new JCheckBox("Urgente");
	private JLabel capacite = new JLabel("capacité sac: 100.0 ");
	private JLabel volumeLab = new JLabel("volume sac:");
	private JLabel volColLab = new JLabel("volume colis:");
	private JPanel infos = new JPanel();
	private JPanel sacPan = new JPanel();	
	private JPanel menu = new JPanel();
	private JPanel boutons = new JPanel();
	private JButton add = new JButton("Ajouter");
	private JButton quit = new JButton("Quitter");
	private JButton aff = new JButton("Affranchir");
	private JTextArea zoneAff = new JTextArea();
	private JScrollPane scrollZone = new JScrollPane(zoneAff);
	private Sac sac = new Sac(100);

	public IHMCoursier(String titre) {
		super(titre);		
		this.setPreferredSize(new Dimension(800,200));
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		choix.addItem("Lettre");
		choix.addItem("Colis");
		
		this.add.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if (choix.getSelectedItem().equals(new String("Lettre"))) {
					try {
						if (urgente.isSelected()) {
							zoneAff.append("Ajout : Lettre urgente.\n");
							sac.ajouter(new Lettre(true));
							volumeSac.setText(((Double)sac.getVolume()).toString());
						} else {
							zoneAff.append("Ajout : Lettre ordinaire.\n");
							sac.ajouter(new Lettre(false));
							volumeSac.setText(((Double)sac.getVolume()).toString());
						}
					} catch (SacPleinException exp) {
						zoneAff.append("Erreur : le sac est trop rempli.\n");
					}
				} else if (choix.getSelectedItem().equals(new String("Colis"))) {
					try {
						zoneAff.append(("Ajout : Colis (Volume : " + volColis.getText()) + ").\n");
						sac.ajouter(new Colis(Double.parseDouble(volColis.getText())));
						volumeSac.setText(((Double)sac.getVolume()).toString());
					} catch (SacPleinException exp) {
						zoneAff.append("Erreur : le sac est trop rempli.\n");
					} catch (NumberFormatException exp) {
						zoneAff.append("Erreur : saisie du volume du colis incorrecte.\n");
					}
				}
			}
		});
		
		this.aff.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				zoneAff.append("Affranchissement : " + sac.affranchir() + " euros au total\n");
			}
		});
		
		this.quit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		
		this.boutons.setLayout(new GridLayout(1,3));

		boutons.add(add);
		boutons.add(quit);
		boutons.add(aff);
		
		volumeSac.setEnabled(false);
		volColis.setPreferredSize(new Dimension(100,20));
		volumeSac.setPreferredSize(new Dimension(100,20));
		infos.setLayout(new FlowLayout());
		infos.add(urgente);
		infos.add(volColLab);
		infos.add(volColis);
		sacPan.setLayout(new FlowLayout());
		sacPan.add(capacite);
		sacPan.add(volumeLab);
		sacPan.add(volumeSac);
		menu.setLayout(new GridLayout(4,1));
		menu.add(choix);
		menu.add(infos);
		menu.add(sacPan);
		menu.add(boutons);
		this.getContentPane().setLayout(new GridLayout(1,2));
		this.getContentPane().add(menu);
		this.getContentPane().add(scrollZone);
		
		
	}
	
}
