package be.iminds.ilabt.jfed.experimenter_gui.login;

import be.iminds.ilabt.jfed.experimenter_gui.debug.LogViewer;
import be.iminds.ilabt.jfed.ui.javafx.GuiceFXMLUtil;
import be.iminds.ilabt.jfed.ui.javafx.util.StageUtils;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.stage.Stage;

import javax.inject.Inject;
import javax.inject.Provider;

/**
 * User: twalcari
 * Date: 8/9/2016
 * Time: 15:34
 */
public class URLLoginDialog {
    private final Stage stage;
    private final URLLoginController urlLoginController;

    private final GuiceFXMLUtil guiceFXMLUtil;
    private final Provider<LogViewer> logviewerProvider;

    @Inject
    public URLLoginDialog(GuiceFXMLUtil guiceFXMLUtil, Provider<LogViewer> logviewerProvider) {
        this.guiceFXMLUtil = guiceFXMLUtil;
        this.logviewerProvider = logviewerProvider;

        GuiceFXMLUtil.Result<?, URLLoginController> result = guiceFXMLUtil.createFromFXML(URLLoginController.class);
        urlLoginController = result.getController();

        stage = new Stage();
        stage.setTitle("jFed login: fetch certificate");
        StageUtils.setJFedStageIcons(stage);
        urlLoginController.setStage(stage);

        final Scene scene = new Scene(result.getRoot());
        scene.getAccelerators().put(
                new KeyCodeCombination(KeyCode.F10, KeyCodeCombination.CONTROL_DOWN),
                () -> this.logviewerProvider.get().show()
        );
        stage.setScene(scene);
    }


    public String showDialog(String initialUrl) {
        urlLoginController.loadUrl(initialUrl);
        stage.showAndWait();

        return urlLoginController.getResult();
    }


}
