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

import be.iminds.ilabt.jfed.experimenter_gui.ui.status.TaskStatusIndicator;
import be.iminds.ilabt.jfed.highlevel.controller.Task;
import be.iminds.ilabt.jfed.highlevel.controller.TaskExecution;
import be.iminds.ilabt.jfed.highlevel.controller.TaskFinishedCallback;
import javafx.application.Platform;

/**
 * User: twalcari
 * Date: 6/4/2014
 * Time: 16:48
 */
public class ProgressItemTaskFinishedCallback implements TaskFinishedCallback {
    private final ProgressItem progressItem;

    public ProgressItemTaskFinishedCallback(ProgressItem progressItem) {
        this.progressItem = progressItem;
    }

    @Override
    public void onTaskExecutionFinished(final Task task, final TaskExecution taskExecution, final TaskExecution.TaskState state) {

        Platform.runLater(() -> {
            if (state == TaskExecution.TaskState.SUCCESS)
                progressItem.setProgress(TaskStatusIndicator.Status.SUCCESS);
            else if (state == TaskExecution.TaskState.FAILED || state == TaskExecution.TaskState.CANCELLED)
                progressItem.setProgress(TaskStatusIndicator.Status.FAILED);
            else
                throw new IllegalStateException("Did not expected state " + state.name()
                        + " for finished task " + task.toString());
        });

    }
}