/**
 * 
 */
package cz.neubauer.tomas.hazardanalysis.viewcontroller.administration;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerType;
import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerVariant;
import cz.neubauer.tomas.hazardanalysis.viewcontroller.windowmanipulator.WindowManipulator;

/**
 * @author Tomáš Neubauer
 */
public class AdminDangerVariantFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6248731818805186060L;

	private AdminFrame owner;

	private ArrayList<DangerType> types;

	private DangerVariant dangerVariant;

	private JComboBox cmbTypes;

	private JPanel pnlTextFields;

	private JTextField txtDescription;

	private JButton btnOK;

	private JButton btnCancel;

	private JPanel pnlButtons;

	public AdminDangerVariantFrame(AdminFrame owner, DangerVariant dangerVariant) {
		this.owner = owner;
		this.dangerVariant = dangerVariant;
		types = dangerVariant.getDangerType().getDangerGroup().getTypes();
		initGUI();
		WindowManipulator.centerWindow(this);
		setVisible(true);
	}

	/**
	 * 
	 */
	private void initGUI() {
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setTitle("Varianta typu nebezpečí");

		GridLayout gl = new GridLayout(4, 1);
		pnlTextFields = new JPanel(gl);

		pnlTextFields.add(new JLabel("Popis"));
		txtDescription = new JTextField(dangerVariant.getDescription(), 50);
		pnlTextFields.add(txtDescription);

		pnlTextFields.add(new JLabel("Typ nebezpečí"));
		cmbTypes = new JComboBox(new Vector<DangerType>(types));
		cmbTypes.setSelectedItem(dangerVariant.getDangerType());
		pnlTextFields.add(cmbTypes);

		this.add(pnlTextFields, BorderLayout.CENTER);

		pnlButtons = new JPanel(new FlowLayout(FlowLayout.CENTER));

		btnOK = new JButton("OK");
		btnOK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				AdminDangerVariantFrame.this.btnOKPerformed();
			}
		});
		pnlButtons.add(btnOK);

		btnCancel = new JButton("Zrušit");
		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				AdminDangerVariantFrame.this.btnCancelPerformed();
			}
		});
		pnlButtons.add(btnCancel);

		this.add(pnlButtons, BorderLayout.SOUTH);
		pack();
	}

	/**
	 * 
	 */
	protected void btnOKPerformed() {
		DangerType dt = (DangerType) cmbTypes.getSelectedItem();
		dangerVariant.setDescription(txtDescription.getText());
		dangerVariant.setDangerType(dt);
		dangerVariant.saveToDB(owner.getConnection());
		owner.refreshTree();
		dispose();
	}

	/**
	 * 
	 */
	protected void btnCancelPerformed() {
		dispose();
	}
}
