/**
 * 
 */
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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

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

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

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

	private AdminFrame owner;

	private ArrayList<DangerGroup> groups;

	private DangerType dangerType;

	private JComboBox cmbGroups;

	private JPanel pnlTextFields;

	private JTextField txtCode;

	private JTextField txtDescription;

	private JTextArea txaSituationDescription;

	private JTextField txtColumn1;

	private JTextField txtColumn2;

	private JTextField txtColumn3;

	private JTextField txtColumn4;

	private JTextField txtColumn5;

	private JTextField txtColumn6;

	private JTextField txtColumn7;

	private JButton btnOK;

	private JButton btnCancel;

	private JPanel pnlButtons;

	public AdminDangerTypeFrame(AdminFrame owner, DangerType dangerType,
			ArrayList<DangerGroup> groups) {
		this.owner = owner;
		this.dangerType = dangerType;
		this.groups = groups;
		initGUI();
		setVisible(true);
	}

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

		pnlTextFields = new JPanel(new GridLayout(22, 1));

		pnlTextFields.add(new JLabel(" Číslo nebezpečí dle ČSN EN 1050:2001"));
		txtCode = new JTextField(dangerType.getCode(), 50);
		pnlTextFields.add(txtCode);

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

		pnlTextFields.add(new JLabel(" Popis nebezpečné situace"));
		txaSituationDescription = new JTextArea(dangerType
				.getSituationDescription());
		txaSituationDescription.setColumns(40);
		txaSituationDescription.setRows(1);
		JScrollPane scroll = new JScrollPane(txaSituationDescription);
		scroll.setViewportView(txaSituationDescription);
		pnlTextFields.add(scroll);

		pnlTextFields.add(new JLabel(" Část 2 - A1:1995"));
		txtColumn1 = new JTextField(dangerType.getColumn_cast2_a1_1995(), 50);
		pnlTextFields.add(txtColumn1);

		pnlTextFields.add(new JLabel(" Část 1 r. 1991"));
		txtColumn2 = new JTextField(dangerType.getColumn_cast1_r1991(), 50);
		pnlTextFields.add(txtColumn2);

		pnlTextFields.add(new JLabel(" Část 2 r. 1991"));
		txtColumn3 = new JTextField(dangerType.getColumn_cast2_r1991(), 50);
		pnlTextFields.add(txtColumn3);

		pnlTextFields.add(new JLabel(" ČSN EN ISO 12100-1:2003"));
		txtColumn4 = new JTextField(dangerType
				.getColumn_csn_en_iso_12100_1_2003(), 50);
		pnlTextFields.add(txtColumn4);

		pnlTextFields.add(new JLabel(" ČSN EN ISO 12100-2:2003"));
		txtColumn5 = new JTextField(dangerType
				.getColumn_csn_en_iso_12100_2_2003(), 50);
		pnlTextFields.add(txtColumn5);

		pnlTextFields.add(new JLabel(" Odpovídající normy typu B"));
		txtColumn6 = new JTextField(dangerType.getColumn_normy_typu_b(), 50);
		pnlTextFields.add(txtColumn6);

		pnlTextFields.add(new JLabel(" Přílušné články ČSN EN 12415:2001"));
		txtColumn7 = new JTextField(dangerType
				.getColumn_clanky_csn_en_12415_2001(), 50);
		pnlTextFields.add(txtColumn7);

		pnlTextFields.add(new JLabel(" Skupina nebezpečí"));
		cmbGroups = new JComboBox(new Vector<DangerGroup>(groups));
		cmbGroups.setSelectedItem(dangerType.getDangerGroup());
		pnlTextFields.add(cmbGroups);

		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) {
				AdminDangerTypeFrame.this.btnOKPerformed();
			}
		});
		pnlButtons.add(btnOK);

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

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

	/**
	 * 
	 */
	protected void btnOKPerformed() {
		DangerGroup dg = (DangerGroup) cmbGroups.getSelectedItem();
		dangerType.setCode(txtCode.getText());
		dangerType.setDescription(txtDescription.getText());
		dangerType.setColumn_cast2_a1_1995(txtColumn1.getText());
		dangerType.setColumn_cast1_r1991(txtColumn2.getText());
		dangerType.setColumn_cast2_r1991(txtColumn3.getText());
		dangerType.setColumn_csn_en_iso_12100_1_2003(txtColumn4.getText());
		dangerType.setColumn_csn_en_iso_12100_2_2003(txtColumn5.getText());
		dangerType.setColumn_normy_typu_b(txtColumn6.getText());
		dangerType.setColumn_clanky_csn_en_12415_2001(txtColumn7.getText());
		dangerType.setSituationDescription(txaSituationDescription.getText());
		dangerType.setDangerGroup(dg);
		dangerType.saveToDB(owner.getConnection());
		owner.refreshTree();
		dispose();
	}

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