/**
 * 
 */
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 java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerVariant;
import cz.neubauer.tomas.hazardanalysis.model.stages.Phase;
import cz.neubauer.tomas.hazardanalysis.model.stages.Stage;
import cz.neubauer.tomas.hazardanalysis.model.tablemodels.StageTableModel;

/**
 * @author Tomáš Neubauer
 */
public class StagePanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 121459743732488051L;

	private CriticalDangerAnalysisFrame owner;

	private JPanel pnlTable, pnlButtons;

	private JButton btnAdd, btnDelete;

	private Stage stage;

	private JTable tblPhases;

	private ArrayList<DangerVariant> criticalVarians;

	private StageTableModel stageTableModel;

	/**
	 * @param stage
	 */
	public StagePanel(CriticalDangerAnalysisFrame owner, Stage stage,
			ArrayList<DangerVariant> criticalVarians) {
		setLayout(new BorderLayout());
		this.owner = owner;
		this.stage = stage;
		this.criticalVarians = criticalVarians;
		initGUI();
	}

	/**
	 * 
	 */
	private void initGUI() {
		initTablePanel();
		this.add(pnlTable, BorderLayout.CENTER);

		initBottonsPanel();
		this.add(pnlButtons, BorderLayout.SOUTH);
	}

	/**
	 * 
	 */
	private void initBottonsPanel() {
		pnlButtons = new JPanel(new FlowLayout(FlowLayout.CENTER));

		btnAdd = new JButton("Přidat fázi");
		btnAdd.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				StagePanel.this.btnAddPerformed();
			}
		});
		pnlButtons.add(btnAdd);

		btnDelete = new JButton("Smazat fázi");
		btnDelete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				StagePanel.this.btnDeletePerformed();
			}
		});
		pnlButtons.add(btnDelete);
	}

	/**
	 * 
	 */
	protected void btnAddPerformed() {
		Phase p = new Phase(stage, "");
		stage.addPhase(p);
		new PhaseDangerVariantFrame(this, p, criticalVarians);
	}

	/**
	 * 
	 */
	protected void btnDeletePerformed() {
		int selectedRow = tblPhases.getSelectedRow();
		if (selectedRow >= 0
				&& JOptionPane.showConfirmDialog(this,
						"Opravdu si přejete smazat tuto fázi?") == JOptionPane.YES_OPTION) {

			stage.removePhase(selectedRow);
			stageTableModel.fireTableDataChanged();
		}
	}

	/**
	 * 
	 */
	private void initTablePanel() {
		pnlTable = new JPanel();
		stageTableModel = new StageTableModel(stage);
		tblPhases = new JTable(stageTableModel);
		tblPhases.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				StagePanel.this.tblPhasesClicked();
			}
		});

		JScrollPane scroll = new JScrollPane(tblPhases);
		scroll.setViewportView(tblPhases);
		scroll.setPreferredSize(new Dimension(600, 300));

		pnlTable.add(scroll);
	}

	/**
	 * 
	 */
	protected void tblPhasesClicked() {
		int selectedRowIndex = tblPhases.getSelectedRow();
		int selectedColumnIndex = tblPhases.getSelectedColumn();
		if (selectedColumnIndex == 4 && selectedRowIndex >= 0) {
			new PhaseDangerVariantFrame(this, stage.getPhase(selectedRowIndex),
					criticalVarians);
		}
	}

	public Stage getStage() {
		return stage;
	}

	public void setStage(Stage stage) {
		this.stage = stage;
	}

	public void refresh() {
		stageTableModel.fireTableDataChanged();
		// ((StageTableModel) tblPhases.getModel()).fireTableDataChanged();
	}

	public CriticalDangerAnalysisFrame getOwner() {
		return owner;
	}

	public void exportData(int no, String file) {
		File f = new File(file);
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(f));

			int rowCount = stageTableModel.getRowCount();
			int columnCount = stageTableModel.getColumnCount();

			String radek;
			for (int i = 0; i < rowCount; i++) {
				radek = String.valueOf(no) + "." + (i + 1);
				for (int j = 0; j < columnCount - 1; j++) {
					radek += ";" + stageTableModel.getValueAt(i, j);
				}
				bw.write(radek);
				bw.newLine();
			}

			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
