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

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

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

/**
 * @author Tomáš Neubauer
 */
public class CriticalDangerVariantFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2009393442557761987L;

	private JTable tableVariants;

	private DangerVariantTableModel dangerVariantTableModel;

	private JComboBox cmbTypes;

	private ArrayList<DangerType> types;

	public CriticalDangerVariantFrame(ArrayList<DangerType> types) {
		this.types = types;
		initGUI();
		WindowManipulator.centerWindow(this);
		refresh(0);
		setVisible(true);
	}

	/**
	 * 
	 */
	private void initGUI() {
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setTitle("Volba závažných nebezpečí");

		cmbTypes = new JComboBox();
		int dangerTypeCount = types.size();
		for (int i = 0; i < dangerTypeCount; i++) {
			cmbTypes.addItem(types.get(i).getDescription());
		}
		cmbTypes.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				CriticalDangerVariantFrame.this.cmbTypesChanged();
			}
		});
		this.add(cmbTypes, BorderLayout.NORTH);

		dangerVariantTableModel = new DangerVariantTableModel(
				new ArrayList<DangerVariant>(10));

		tableVariants = new JTable(dangerVariantTableModel);

		tableVariants.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tableVariants.getColumnModel().getColumn(0).setPreferredWidth(400);
		tableVariants.getColumnModel().getColumn(1).setPreferredWidth(50);

		tableVariants.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				CriticalDangerVariantFrame.this.tableClicked();
			}
		});
		JScrollPane spn = new JScrollPane(tableVariants);
		spn.setViewportView(tableVariants);
		spn.setPreferredSize(new Dimension(453, 200));

		this.add(spn, BorderLayout.CENTER);
		pack();
	}

	/**
	 * 
	 */
	protected void tableClicked() {
		int selectedRow = tableVariants.getSelectedRow();
		int selectedColumn = tableVariants.getSelectedColumn();
		if (selectedRow >= 0 && selectedColumn == 1) {
			DangerVariant currentDangerVariant = dangerVariantTableModel
					.getDangerVariants().get(selectedRow);
			currentDangerVariant
					.setCritical(!currentDangerVariant.isCritical());
			dangerVariantTableModel.fireTableDataChanged();
		}
	}

	/**
	 * 
	 */
	protected void cmbTypesChanged() {
		int selectedIndex = cmbTypes.getSelectedIndex();
		if (selectedIndex >= 0) {
			refresh(selectedIndex);
		}
	}

	/**
	 * 
	 */
	private void refresh(int index) {
		dangerVariantTableModel.setDangerVariants(types.get(index)
				.getVariants());
		dangerVariantTableModel.fireTableDataChanged();
		this.repaint();
		// this.tableVariants.setModel(dangerVariantTableModel);
	}
}
