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

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.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import cz.neubauer.tomas.hazardanalysis.Rizika;
import cz.neubauer.tomas.hazardanalysis.model.analysis.Analysis;
import cz.neubauer.tomas.hazardanalysis.model.analysis.AnalysisRecord;
import cz.neubauer.tomas.hazardanalysis.model.components.Component;
import cz.neubauer.tomas.hazardanalysis.model.dangers.DangerType;
import cz.neubauer.tomas.hazardanalysis.model.tablemodels.AnalysisTableModel;
import cz.neubauer.tomas.hazardanalysis.viewcontroller.administration.AdminFrame;
import cz.neubauer.tomas.hazardanalysis.viewcontroller.dangeranalysis.CriticalDangerAnalysisFrame;
import cz.neubauer.tomas.hazardanalysis.viewcontroller.dangeranalysis.CriticalDangerVariantFrame;
import cz.neubauer.tomas.hazardanalysis.viewcontroller.windowmanipulator.WindowManipulator;

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

	private Rizika application;

	private JMenuBar menuBar;

	private JMenu mnuFile;

	private JMenuItem mniSave, mniLoadDiagramXML;

	private JMenu mnuOptions;

	private JMenuItem mniShowAdmin;

	private JMenuItem mniShowCriticalAnalysis;

	private JPanel pnlTable;

	private JTable table;

	private AnalysisTableModel analysisTableModel;

	private String currentXMLFile;

	private AdminFrame adminFrame;

	public MainFrame(Rizika application) {
		this.application = application;
		initGUI();

		adminFrame = new AdminFrame(this);
	}

	private void initGUI() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("Rizika");

		initMenu();
		initTablePanel();
		// ---------- nechat zakomentované!!! -------------
		// initEditPanel();

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

	/**
	 * 
	 */
	private void initTablePanel() {
		analysisTableModel = new AnalysisTableModel();
		table = new JTable(analysisTableModel);
		// ---------- nechat zakomentované!!! -------------
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				MainFrame.this.tableClicked();
			}
		});

		JScrollPane jsp = new JScrollPane(table);
		jsp.setViewportView(table);
		jsp.setPreferredSize(new Dimension(500, 300));

		pnlTable = new JPanel();
		pnlTable.add(jsp);

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

	/**
	 * 
	 */
	private void initMenu() {
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		mnuFile = new JMenu("Soubor");
		menuBar.add(mnuFile);

		mnuOptions = new JMenu("Možnosti");
		menuBar.add(mnuOptions);

		mniLoadDiagramXML = new JMenuItem("Načíst XML");
		mniLoadDiagramXML.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				MainFrame.this.mniLoadDiagramXMLPerformed();
			}
		});
		mnuFile.add(mniLoadDiagramXML);

		mnuFile.addSeparator();

		mniSave = new JMenuItem("Exportovat");
		mniSave.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				MainFrame.this.mniSavePerformed();
			}
		});
		mnuFile.add(mniSave);

		mniShowAdmin = new JMenuItem("Administrace");
		mniShowAdmin.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				MainFrame.this.mniShowAdminPerformed();
			}

		});
		mnuOptions.add(mniShowAdmin);

		mniShowCriticalAnalysis = new JMenuItem("Analýza významných nebezpečí");
		mniShowCriticalAnalysis.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				MainFrame.this.mniShowCriticalAnalysisPerformed();
			}
		});
		mnuOptions.add(mniShowCriticalAnalysis);
	}

	/**
	 * 
	 */
	protected void mniShowCriticalAnalysisPerformed() {
		if (analysisTableModel.getAnalysis().getCriticalDangerVariants().size() > 0) {

			new CriticalDangerAnalysisFrame(this, analysisTableModel
					.getAnalysis().getCriticalDangerVariants());
		} else {
			JOptionPane.showMessageDialog(this,
					"Není stanoveno žádné závažné nebezpečí!", "Chyba!",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	/**
	 * 
	 */
	protected void mniLoadDiagramXMLPerformed() {
		JFileChooser jfch = new JFileChooser();
		if (jfch.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
			currentXMLFile = jfch.getSelectedFile().getAbsolutePath();
			Analysis a = application
					.createAnalysis("file:///" + currentXMLFile);
			analysisTableModel.setAnalysis(a);
		}
	}

	/**
	 * 
	 */
	protected void mniSavePerformed() {
		JFileChooser jfch = new JFileChooser();

		if (jfch.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
			String path = jfch.getSelectedFile().getAbsolutePath();
			Analysis a = analysisTableModel.getAnalysis();
			a.saveToFile(path);
		}
	}

	protected void mniShowAdminPerformed() {
		adminFrame.setVisible(true);
	}

	/**
	 * 
	 */
	protected void tableClicked() {
		int selectedRow = table.getSelectedRow();
		int selectedColumn = table.getSelectedColumn();
		if (selectedRow >= 0 && selectedColumn == 3) {
			AnalysisRecord selectedRecord = analysisTableModel.getAnalysis()
					.getAnalysisRecord(selectedRow);
			Component selectedComponent = selectedRecord.getComponent();

			ArrayList<DangerType> types = selectedComponent.getDangerTypes();
			if (types.size() > 0) {
				new CriticalDangerVariantFrame(types);
			}
		}
	}

	public Rizika getApplication() {
		return application;
	}

	public void refresh() {
		application.refresh();
		Analysis a = application.createAnalysis(currentXMLFile);
		if (a != null) {
			analysisTableModel.setAnalysis(a);
		}
	}

	public Analysis getAnalysis() {
		return analysisTableModel.getAnalysis();
	}
}
