package be.iminds.ilabt.jfed.experimenter_gui.slice.actions;

import be.iminds.ilabt.jfed.experiment.ExperimentController;
import be.iminds.ilabt.jfed.highlevel.model.Sliver;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.stage.Window;

/**
 * User: twalcari
 * Date: 2/4/2015
 * Time: 09:54
 */
public class ReloadOSAction extends ExperimentAction {

    private final Sliver sliver;

    ReloadOSAction(Window parentWindow, ExperimentController experimentController, Sliver sliver) {
        super(parentWindow, experimentController);
        this.sliver = sliver;
    }

    @Override
    public void run() {
        assert (Platform.isFxApplicationThread());

        Alert confirmAlert = new Alert(Alert.AlertType.WARNING);
        confirmAlert.setContentText("This operation will revert the node back to its original image.\n\nAll changes made WILL BE LOST.");
        confirmAlert.setTitle("Reload OS image");

        ButtonType reloadButtonType = new ButtonType("Reload OS and lose changes", ButtonData.OTHER);

        confirmAlert.getButtonTypes().setAll(reloadButtonType, ButtonType.CANCEL);

        ((Button) confirmAlert.getDialogPane().lookupButton(ButtonType.CANCEL)).setDefaultButton(true);

        // ((Button)confirmAlert.getDialogPane().lookupButton(reloadButtonType)).setMinWidth(300);
        confirmAlert.getDialogPane().setMinWidth(450);


        confirmAlert.showAndWait()
                .filter(button -> button == reloadButtonType)
                .ifPresent(button ->
                        experimentController.reloadOS(sliver));
    }
}
