package be.iminds.ilabt.jfed.highlevel.jobs;

import be.iminds.ilabt.jfed.experiment.Experiment;
import be.iminds.ilabt.jfed.highlevel.controller.TaskExecution;
import be.iminds.ilabt.jfed.highlevel.controller.TaskThread;
import be.iminds.ilabt.jfed.highlevel.model.Sliver;
import be.iminds.ilabt.jfed.highlevel.tasks.HighLevelTaskFactory;
import be.iminds.ilabt.jfed.highlevel.tasks.OpenConsoleTask;

import javax.annotation.Nullable;
import java.net.URL;

/**
 * User: twalcari
 * Date: 12/1/2015
 * Time: 17:41
 */
public class OpenConsoleJob extends SingleTaskJob<OpenConsoleTask, URL> {
    private final Sliver sliver;

    private URL result = null;

    public OpenConsoleJob(Experiment experiment, Sliver sliver, HighLevelTaskFactory hltf, TaskThread tt) {
        super("Open Console for sliver " + sliver.getUrn().toString(), experiment, hltf, tt);
        this.sliver = sliver;
    }

    @Override
    public OpenConsoleTask createTask() {
        return hltf.openConsole(sliver);
    }

    @Override
    protected void onFinished(OpenConsoleTask task, TaskExecution<OpenConsoleTask> taskExecution) {
        if (taskExecution.getState() == TaskExecution.TaskState.SUCCESS) {
            assert (task.getUrl() != null);

            result = task.getUrl();

            updateMessage(task.getUrl().toExternalForm());
        }
    }

    @Override
    @Nullable
    public URL getResult() {
        return result;
    }
}
