package view.listener;

import presenter.Presenter;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
 *
 * @Author: Armin Wolf
 * @Email: a_wolf28@uni-muenster.de
 * @Date: 17.09.2017.
 */
public class PictureImportListener implements ActionListener {
    private Presenter presenter;
    private Container component;

    public PictureImportListener(Presenter presenter, Container component) {
        this.presenter = presenter;
        this.component = component;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        SwingUtilities.invokeLater(() -> {
            File file = null;
            JFileChooser chooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
            chooser.setPreferredSize(new Dimension(800, 700));
            chooser.setFileFilter(new FileNameExtensionFilter("Bilder", "tiff", "png","jpg","jpeg"));

            chooser.setMultiSelectionEnabled(false);
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);


            if (chooser.showOpenDialog(component) == JFileChooser.APPROVE_OPTION) {
                //System.out.println ("Datei "+chooser.getSelectedFile()+ " ausgewählt.");
                file = chooser.getSelectedFile();
                final File input = file;
                Thread t = new Thread(() -> presenter.startPictureDataImport(input));
                t.start();
            }
        });
    }
}
