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

import be.iminds.ilabt.jfed.experimenter_gui.util.StringConverters;
import be.iminds.ilabt.jfed.highlevel.controller.TaskExecution;
import be.iminds.ilabt.jfed.highlevel.tasks.HighLevelTaskFactory;
import be.iminds.ilabt.jfed.lowlevel.api_wrapper.AggregateManagerWrapper;
import be.iminds.ilabt.jfed.fedmon.webapi.service.json.*;
import be.iminds.ilabt.jfed.util.GeniUrn;
import javafx.concurrent.Task;

/**
 * User: twalcari
 * Date: 1/6/2015
 * Time: 14:39
 */
public class DeleteDiskImageTask extends Task<Boolean> {

    private final Server authority;
    private final AggregateManagerWrapper.ImageInfo imageInfo;

    private final HighLevelTaskFactory hltf;

    public DeleteDiskImageTask(Server authority, AggregateManagerWrapper.ImageInfo imageInfo, HighLevelTaskFactory hltf) {
        this.authority = authority;
        this.imageInfo = imageInfo;
        this.hltf = hltf;

        updateTitle(String.format("Delete disk image '%s' from %s",
                StringConverters.IMAGEINFO_STRING_CONVERTER.toString(imageInfo),
                authority.getName()));
    }

    @Override
    protected Boolean call() throws Exception {
        TaskExecution taskExecution =
                hltf.submitTask(hltf.deleteImage(authority, GeniUrn.parse(imageInfo.getUrn())));

        while (!taskExecution.isCompleted()) {
            Thread.sleep(250);
        }
        return true;
    }
}
