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

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

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

	private DangerEstimationFrame owner;

	private JPanel pnlTable, pnlButton;

	private JTable tblPersons;

	private PersonsTableModel personsTableModel;

	private JButton btnOK;

	public PersonSelectionFrame(DangerEstimationFrame owner) {
		this.owner = owner;
		initGUI();
	}

	/**
	 * 
	 */
	private void initGUI() {
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		setTitle("Ohrožené osoby");
		setLayout(new BorderLayout());

		pnlTable = new JPanel();
		personsTableModel = new PersonsTableModel();
		tblPersons = new JTable(personsTableModel);
		JScrollPane scroll = new JScrollPane(tblPersons);
		scroll.setViewportView(tblPersons);
		scroll.setPreferredSize(new Dimension(150, 85));
		pnlTable.add(scroll);

		pnlButton = new JPanel(new FlowLayout(FlowLayout.CENTER));
		btnOK = new JButton("OK");
		btnOK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				PersonSelectionFrame.this.btnOKPerformed();
			}
		});
		pnlButton.add(btnOK);

		this.add(pnlTable, BorderLayout.CENTER);
		this.add(pnlButton, BorderLayout.SOUTH);

		pack();
	}

	/**
	 * 
	 */
	protected void btnOKPerformed() {
		String text = "";
		for (int i = 0, j = 0; i < personsTableModel.getPersons()
				.getPersonsCount(); i++) {
			if (personsTableModel.getPersons().isPersonSelected(i)) {
				if (j > 0) {
					text += ", ";
				}
				text += personsTableModel.getPersons().getPerson(i);
				j++;
			}
		}
		owner.setPersons(text);
		// owner.btnSavePerformed();

		setVisible(false);
	}

	private class PersonsTableModel extends AbstractTableModel {

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

		private Persons persons;

		public PersonsTableModel() {
			persons = new Persons();
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see javax.swing.table.TableModel#getColumnCount()
		 */
		@Override
		public int getColumnCount() {
			return 2;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see javax.swing.table.TableModel#getRowCount()
		 */
		@Override
		public int getRowCount() {
			return persons.getPersonsCount();
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see javax.swing.table.TableModel#getValueAt(int, int)
		 */
		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			switch (columnIndex) {
			case 0:
				return persons.isPersonSelected(rowIndex);
			case 1:
				return persons.getPerson(rowIndex);
			default:
				return null;
			}
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object,
		 *      int, int)
		 */
		@Override
		public void setValueAt(Object value, int rowIndex, int columnIndex) {
			switch (columnIndex) {
			case 0:
				persons.setPersonSelected(rowIndex, (Boolean) value);
				break;
			default:
				super.setValueAt(value, rowIndex, columnIndex);
			}
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
		 */
		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return columnIndex == 0;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see javax.swing.table.AbstractTableModel#getColumnName(int)
		 */
		@Override
		public String getColumnName(int column) {
			return "";
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
		 */
		@Override
		public Class<?> getColumnClass(int columnIndex) {
			switch (columnIndex) {
			case 0:
				return Boolean.class;
			case 1:
				return super.getColumnClass(columnIndex);
			default:
				return null;
			}

		}

		public Persons getPersons() {
			return persons;
		}
	}

	protected class Persons {
		private String[] persons = new String[] { "dopravci", "seřizovač",
				"údržbář", "obsluha", "třetí osoba" };

		private boolean[] personSelected = new boolean[persons.length];

		public String getPerson(int index) {
			if (index >= 0 && index < getPersonsCount()) {
				return persons[index];
			} else {
				return "";
			}
		}

		public int getPersonsCount() {
			return persons.length;
		}

		public void setPersonSelected(int personIndex, boolean selected) {
			if (personIndex >= 0 && personIndex < getPersonsCount()) {
				personSelected[personIndex] = selected;
			}
		}

		public boolean isPersonSelected(int personIndex) {
			if (personIndex >= 0 && personIndex < getPersonsCount()) {
				return personSelected[personIndex];
			} else {
				return false;
			}
		}
	}
}
