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

import be.iminds.ilabt.jfed.highlevel.jobs.CreateDiskImageJob;
import be.iminds.ilabt.jfed.highlevel.jobs.StateFinishedListener;
import be.iminds.ilabt.jfed.highlevel.tasks.CreateImageTask;
import be.iminds.ilabt.jfed.ui.javafx.GlyphUtils;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import org.controlsfx.glyphfont.FontAwesome;

/**
 * User: twalcari
 * Date: 12/1/2015
 * Time: 15:36
 */
public class CreateDiskImageStateListener implements StateFinishedListener<CreateDiskImageJob.CreateImageState> {
    @Override
    public void onStateFinished(CreateDiskImageJob.CreateImageState state) {
        final CreateImageTask createImageTask = state.getTask();


        showImageInfoDialog(createImageTask);
    }

    public static void showImageInfoDialog(CreateImageTask createImageTask) {
        VBox container = new VBox();
        container.setSpacing(5);

        Label label = new Label("Your disk image is currently being created. " +
                "This process typically takes around 15 minutes. " +
                "You will receive an e-mail when the disk image is ready.\n\n" +
                "If you want to use the image on a different testbed then the current, use the following RSpec snippet:");

        TextArea textArea = new TextArea();
        textArea.setText(String.format("<sliver_type name=\"raw-pc\">\n" +
                "\t<disk_image name=\"%s\"/>\n" +
                "</sliver_type>", createImageTask.getResultDiskImageUrl()));
        textArea.setPrefRowCount(3);
        textArea.setPrefColumnCount(40);
        textArea.setEditable(false);
        textArea.setMinHeight(85);

        Label label2 =
                new Label("If however, you want to use this image on the same testbed, it is better to use:");
        TextArea textArea2 = new TextArea();
        textArea2.setText(String.format("<sliver_type name=\"raw-pc\">\n" +
                "\t<disk_image name=\"%s\"/>\n" +
                "</sliver_type>", createImageTask.getResultDiskImageUrn()));
        textArea2.setPrefRowCount(3);
        textArea2.setPrefColumnCount(40);
        textArea2.setEditable(false);
        textArea2.setMinHeight(85);

        container.getChildren().addAll(label, textArea, label2, textArea2);

        DialogPane dialogPane = new DialogPane();
        dialogPane.setContent(container);
        dialogPane.setHeaderText("Your Disk Image is being prepared");
        dialogPane.setGraphic(GlyphUtils.createDialogGlyph(FontAwesome.Glyph.HDD_ALT, Color.GRAY));
        dialogPane.getButtonTypes().setAll(ButtonType.OK);

        Dialog<ButtonType> dialog = new Dialog<>();
        dialog.setTitle("Your Disk Image is being prepared");
        dialog.setDialogPane(dialogPane);

        dialog.show();
    }
}
