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

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

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 * @author Tomáš Neubauer
 */
public class DangerCalculatorPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4750827546637595078L;

	private String dangerDescription;

	private JLabel lblDescription, lblDangerValue;

	private JComboBox cmbHealth, cmbDuration, cmbPrevention, cmbProbability;

	public DangerCalculatorPanel(String description) {
		dangerDescription = description;
		initGUI();
	}

	/**
	 * 
	 */
	private void initGUI() {
		GridBagLayout gbl = new GridBagLayout();
		setLayout(gbl);

		gbl.columnWidths = new int[] { 120, 200, 250, 80 };
		GridBagConstraints c = new GridBagConstraints();

		c.anchor = GridBagConstraints.NORTHWEST;
		c.fill = GridBagConstraints.BOTH;
		c.insets = new Insets(0, 6, 0, 6);

		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 4;
		lblDescription = new JLabel(
				"<html><body style=\"text-align: center;\">"
						+ dangerDescription + "</body></html>");
		lblDescription.setFont(new Font(lblDescription.getFont().getName(),
				Font.BOLD, 14));
		this.add(lblDescription, c);

		c.gridx = 1;
		c.gridy = 0;
		c.gridheight = 1;
		this.add(new JLabel("Závažnost možné škody na zdraví:"), c);

		c.gridx = 1;
		c.gridy = 1;
		c.gridheight = 1;
		this.add(new JLabel("Četnost a doba trvání ohrožení:"), c);

		c.gridx = 1;
		c.gridy = 2;
		c.gridheight = 1;
		this.add(new JLabel("Možnost vyvarování se nebezpečí:"), c);

		c.gridx = 1;
		c.gridy = 3;
		c.gridheight = 1;
		this.add(new JLabel("Pst. výskytu nebezpečné události:"), c);

		// ---
		c.gridx = 2;
		c.gridy = 0;
		c.gridheight = 1;
		cmbHealth = new JComboBox();
		cmbHealth.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DangerCalculatorPanel.this.compute();
			}
		});
		cmbHealth.addItem("S0 - žádné nebezpečí");
		cmbHealth.addItem("S1 - lehké poškození (přechodné následky)");
		cmbHealth.addItem("S2 - těžké zranění (trvalé následky)");
		cmbHealth.addItem("S3 - smrt");
		this.add(cmbHealth, c);

		c.gridx = 2;
		c.gridy = 1;
		c.gridheight = 1;
		cmbDuration = new JComboBox();
		cmbDuration.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DangerCalculatorPanel.this.compute();
			}
		});
		cmbDuration.addItem("A1 - zřídka až častěji");
		cmbDuration.addItem("A2 - často až trvale");
		this.add(cmbDuration, c);

		c.gridx = 2;
		c.gridy = 2;
		c.gridheight = 1;
		cmbPrevention = new JComboBox();
		cmbPrevention.addItem("E1 - možné");
		cmbPrevention.addItem("E2 - možné za určitých okolností");
		cmbPrevention.addItem("E3 - sotva možné");
		cmbPrevention.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DangerCalculatorPanel.this.compute();
			}
		});
		this.add(cmbPrevention, c);

		c.gridx = 2;
		c.gridy = 3;
		c.gridheight = 1;
		cmbProbability = new JComboBox();
		cmbProbability.addItem("W1 - malá");
		cmbProbability.addItem("W2 - střední");
		cmbProbability.addItem("W3 - velká");
		cmbProbability.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DangerCalculatorPanel.this.compute();
			}
		});
		this.add(cmbProbability, c);

		c.gridx = 3;
		c.gridy = 0;
		c.gridheight = 1;
		this.add(new JLabel("Velikost rizika"));

		c.gridx = 3;
		c.gridy = 1;
		c.gridheight = 3;
		lblDangerValue = new JLabel("0");
		lblDangerValue.setFont(new Font(lblDangerValue.getFont().getName(),
				Font.BOLD, 28));
		lblDangerValue.setHorizontalTextPosition(SwingConstants.CENTER);
		this.add(lblDangerValue, c);
	}

	/**
	 * 
	 */
	protected void compute() {
		int s = cmbHealth.getSelectedIndex() - 1;
		int result = 0;
		if (s >= 0) {
			int a = cmbDuration.getSelectedIndex();
			int e = cmbPrevention.getSelectedIndex();
			int w = cmbProbability.getSelectedIndex();
			result = 6 * s + 3 * a + w + e - 1;
		}

		if (lblDangerValue != null) {
			lblDangerValue.setText(String.valueOf(result >= 0 ? result : 0));
		}
	}

	public void setS(int s) {
		cmbHealth.setSelectedIndex(s);
	}

	public int getS() {
		return cmbHealth.getSelectedIndex();
	}

	public void setA(int a) {
		cmbDuration.setSelectedIndex(a);
	}

	public int getA() {
		return cmbDuration.getSelectedIndex();
	}

	public void setE(int e) {
		cmbPrevention.setSelectedIndex(e);
	}

	public int getE() {
		return cmbPrevention.getSelectedIndex();
	}

	public void setW(int w) {
		cmbProbability.setSelectedIndex(w);
	}

	public int getW() {
		return cmbProbability.getSelectedIndex();
	}

	public String getCurrentS() {
		return cmbHealth.getSelectedItem().toString();
	}

	public String getCurrentA() {
		return cmbDuration.getSelectedItem().toString();
	}

	public String getCurrentE() {
		return cmbPrevention.getSelectedItem().toString();
	}

	public String getCurrentW() {
		return cmbProbability.getSelectedItem().toString();
	}

	public String getCurrentDangerLevel() {
		return lblDangerValue.getText();
	}
}
