package view;

import presenter.Presenter;
import view.MainFrame;
import view.listener.ExportDataListener;
import view.listener.GenerateDataListener;
import view.listener.ImportDataListener;
import view.listener.PictureImportListener;
import view.panels.AboutPanel;

import javax.swing.*;
import java.awt.*;

/**
 * Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
 *
 * @Author: Armin Wolf
 * @Email: a_wolf28@uni-muenster.de
 * @Date: 29.05.2017.
 */
public class MenuBar {

    private MainFrame view;

    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenu toolsMenu;
    private JMenu aboutMenu;

    private JMenuItem exitItem;
    private JMenuItem importItem;
    private JMenuItem exportItem;
    private JMenuItem generateItem;
    private JMenuItem evaluateItem;
    private JMenuItem aboutItem;
    private JMenuItem importPicture;

    public MenuBar(MainFrame view) {
        this.menuBar = new JMenuBar();
        this.view = view;
        this.fileMenu = new JMenu("Datei");
        this.toolsMenu = new JMenu("Extras");
        this.aboutMenu = new JMenu("Info");


        this.exitItem = new JMenuItem("Exit");
        this.importItem = new JMenuItem("Import");
        this.importPicture = new JMenuItem("Bildimport");
        this.exportItem = new JMenuItem("Export");
        this.generateItem = new JMenuItem("Generiere...");
        this.aboutItem = new JMenuItem("Über das Programm");

        this.evaluateItem = new JMenuItem("Evaluation");


        fileMenu.add(exitItem);
        fileMenu.add(importItem);
        fileMenu.add(exportItem);

        toolsMenu.add(generateItem);
        toolsMenu.add(importPicture);
        toolsMenu.add(evaluateItem);


        aboutMenu.add(aboutItem);

        menuBar.add(fileMenu);
        menuBar.add(toolsMenu);
        menuBar.add(aboutMenu);
    }

    public void addActionListeners(){
        this.exitItem.addActionListener(e -> {System.exit(0);});
        this.evaluateItem.addActionListener(e -> {view.showEvauluationDialog();});
        this.importItem.addActionListener(new ImportDataListener((Presenter) view.getPresenter(), view));
        this.exportItem.addActionListener(new ExportDataListener((Presenter) view.getPresenter(), view));
        this.generateItem.addActionListener(new GenerateDataListener((Presenter) view.getPresenter()));
        this.aboutItem.addActionListener(e -> {
            SwingUtilities.invokeLater(() -> {
                JDialog dialog = new JDialog();
                dialog.setSize(410,420);
                dialog.setResizable(false);
                dialog.add(new AboutPanel());
                dialog.setVisible(true);
            });
        });
        this.importPicture.addActionListener(new PictureImportListener((Presenter) view.getPresenter(), view));

    }

    public JMenuBar getMenuBar() {
        return menuBar;
    }
}
