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

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerVariant;
import cz.neubauer.tomas.hazardanalysis.model.stages.Phase;
import cz.neubauer.tomas.hazardanalysis.model.tablemodels.PhaseDangerVariantTableModel;
import cz.neubauer.tomas.hazardanalysis.viewcontroller.windowmanipulator.WindowManipulator;

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

	private StagePanel owner;

	private JPanel pnlForm, pnlTable;

	private JTextField txtPhaseName;

	private JTable tableVariants;

	private PhaseDangerVariantTableModel phaseDangerVariantTableModel;

	private ArrayList<DangerVariant> criticalDangerVariants;

	private Phase phase;

	public PhaseDangerVariantFrame(StagePanel owner, Phase phase,
			ArrayList<DangerVariant> criticalDangerVariants) {
		this.owner = owner;
		this.phase = phase;
		this.criticalDangerVariants = criticalDangerVariants;
		initGUI();
	}

	/**
	 * 
	 */
	private void initGUI() {
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setTitle("Přiřazení nebezpečí fázi");

		initPanelTable();
		this.add(pnlTable, BorderLayout.CENTER);

		initPanelForm();
		this.add(pnlForm, BorderLayout.NORTH);

		addWindowListener(new WindowListener() {

			@Override
			public void windowOpened(WindowEvent e) {
				// nop
			}

			@Override
			public void windowIconified(WindowEvent e) {
				// nop
			}

			@Override
			public void windowDeiconified(WindowEvent e) {
				// nop
			}

			@Override
			public void windowDeactivated(WindowEvent e) {
				// nop
			}

			@Override
			public void windowClosing(WindowEvent e) {
				// nop
			}

			@Override
			public void windowClosed(WindowEvent e) {
				phase.setName(txtPhaseName.getText());
				owner.refresh();
			}

			@Override
			public void windowActivated(WindowEvent e) {
				// nop
			}
		});

		pack();
		WindowManipulator.centerWindow(this);
		setVisible(true);
	}

	/**
	 * 
	 */
	private void initPanelForm() {
		pnlForm = new JPanel(new GridLayout(2, 1));

		pnlForm.add(new JLabel("Název fáze"));

		txtPhaseName = new JTextField(phase.getName(), 10);
		pnlForm.add(txtPhaseName);
	}

	/**
	 * 
	 */
	private void initPanelTable() {
		pnlTable = new JPanel();
		phaseDangerVariantTableModel = new PhaseDangerVariantTableModel(phase,
				criticalDangerVariants);

		tableVariants = new JTable(phaseDangerVariantTableModel);

		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) {
				PhaseDangerVariantFrame.this.tableClicked();
			}
		});
		JScrollPane spn = new JScrollPane(tableVariants);
		spn.setViewportView(tableVariants);
		spn.setPreferredSize(new Dimension(453, 200));

		pnlTable.add(spn, BorderLayout.CENTER);
	}

	/**
	 * 
	 */
	protected void tableClicked() {
		int selectedRowIndex = tableVariants.getSelectedRow();
		int selectedColumnIndex = tableVariants.getSelectedColumn();
		if (selectedColumnIndex == 1 && selectedRowIndex >= 0) {
			DangerVariant selectedVariant = criticalDangerVariants
					.get(selectedRowIndex);
			PhaseDangerVariantTableModel phaseDangerVariantTableModel = (PhaseDangerVariantTableModel) tableVariants
					.getModel();

			if ((Boolean) (phaseDangerVariantTableModel).getValueAt(
					selectedRowIndex, selectedColumnIndex)) {
				// je vybrano ... varianta patri do faze
				phase.getVariants().remove(selectedVariant);
			} else {
				// neni vybrana
				phase.addDangerVariant(selectedVariant);
			}

			phaseDangerVariantTableModel.fireTableDataChanged();

		}
	}
}
