package application.actions;

import application.ApplicationWindow;
import application.editor.Editor;

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

public class OpenAction extends AbstractSystemAction {

    private static final long serialVersionUID = -8290761032629599683L;

    private String lastDir = null;

    public OpenAction(ApplicationWindow frame) {
        super("Open...", frame);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Editor editor = frame.getEditor();
        if (editor == null) {
            return;
        }
        String wd = (lastDir != null) ? lastDir : System.getProperty("user.dir");

        JFileChooser fc = buildFileChooser(wd);
        int option = fc.showDialog(null, "Open Model File");
        if (option == JFileChooser.APPROVE_OPTION) {
            lastDir = fc.getSelectedFile().getParent();
            editor.open(fc.getSelectedFile());
            frame.setTitle(ApplicationWindow.title + " - " + fc.getSelectedFile().getAbsolutePath());
        }
    }

    private static JFileChooser buildFileChooser(String wd) {
        JFileChooser fc = new JFileChooser(wd);

        FileFilter model = new FileNameExtensionFilter("model", "model");
        FileFilter dtram = new FileNameExtensionFilter("dtram", "dtram");

        // Adds file filter for supported file format
        FileFilter defaultFilter = new FileFilter() {
            public boolean accept(File file) {
                String lowerCase = file.getName().toLowerCase();
                return lowerCase.endsWith(".model");
            }

            @Override
            public String getDescription() {
                return null;
            }
        };

        fc.addChoosableFileFilter(defaultFilter);
        fc.addChoosableFileFilter(model);
        fc.addChoosableFileFilter(dtram);
        return fc;
    }
}
