package de.vogel612.helper.ui;

import javafx.application.Platform;
import javafx.stage.FileChooser.ExtensionFilter;

import javax.swing.filechooser.FileNameExtensionFilter;
import java.nio.file.Path;

import static de.vogel612.helper.ui.jfx.JFXDialog.DIALOG;

public class OverviewPresenter {

    public static final String DEFAULT_TARGET_LOCALE = "de";
    public static final String DEFAULT_ROOT_LOCALE = "";

    private final OverviewView view;
    private final ProjectView project;

    public OverviewPresenter(final OverviewView v, ProjectView pv) {
        view = v;
        project = pv;
        pv.addResourceSetListener(resourceSet -> {
            project.hide();
            view.loadFiles(resourceSet);
            view.show();
            view.selectLocale();
        });
        view.addFileRequestListener(this::fileChoosing);
    }


    public void show() {
        view.show();
    }

    public void onException(final Exception e, final String message) {
        Platform.runLater(() -> DIALOG.info(message, e.getMessage()));
        // FIXME: Allow termination for unrecoverable exception
    }

    /**
     * Allows the user to choose an arbitrary *.regex file and asks them to choose out of the available locales for
     * Left
     * and Right in the view.
     */
    public void fileChoosing() {
        project.hide();
        view.hide();
        final Path chosenFile = DIALOG.chooseFile("Select a resx or thp file",
                new ExtensionFilter("Resource file", "*.resx"),
                new ExtensionFilter("TranslationHelper Project file", "*.thp"));
        if (chosenFile.getFileName().toString().endsWith(".thp")) {
            project.loadProject(chosenFile);
            project.show();
        } else {
            view.loadFiles(chosenFile);
            view.show();
            view.selectLocale();
        }
    }
}

@@ -43,135 +43,43 @@
 afx.
-application.Platform;%0Aimport javafx.stage.FileChooser.ExtensionFilter;%0A%0Aimport javax.swing.filechooser.FileNameExtensionFilter;
+stage.FileChooser.ExtensionFilter;%0A
 %0Aimp
@@ -813,221 +813,8 @@
  %7D%0A%0A
-    public void onException(final Exception e, final String message) %7B%0A        Platform.runLater(() -%3E DIALOG.info(message, e.getMessage()));%0A        // FIXME: Allow termination for unrecoverable exception%0A    %7D%0A%0A
     

drop onException from Presenter