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 RebootAction extends ExperimentAction {

    private final Sliver sliver;

    RebootAction(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(String.format("You are about to reboot sliver %s. All data in memory WILL BE LOST. Are you sure that you want to continue?", sliver.getUrnString()));
        confirmAlert.setTitle("Rebooting sliver");


        ButtonType rebootOSButton = new ButtonType("Reboot OS", ButtonData.OTHER);

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

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


        confirmAlert.showAndWait()
                .filter(button -> button == rebootOSButton)
                .ifPresent(button -> experimentController.reboot(sliver));
    }
}
