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

import be.iminds.ilabt.jfed.experiment.SfaExperimentPart;
import be.iminds.ilabt.jfed.experiment.tasks.ExperimentTaskStatus;
import be.iminds.ilabt.jfed.highlevel.jobs.Job;
import be.iminds.ilabt.jfed.highlevel.jobs.UpdateUntilReadyStateSlice;
import be.iminds.ilabt.jfed.highlevel.jobs.states.JobStateFactory;
import be.iminds.ilabt.jfed.highlevel.jobs.states.UpdateUntilReadyOrFailTaskState;
import be.iminds.ilabt.jfed.lowlevel.JFedException;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * User: twalcari
 * Date: 1/14/2016
 * Time: 12:05
 */
public class UpdateSfaExperimentPartUntilReadyStateSlice extends UpdateUntilReadyStateSlice<SfaExperimentPart> {

    private final JobStateFactory jobStateFactory;

    public UpdateSfaExperimentPartUntilReadyStateSlice(@Nonnull Job<?> job,@Nonnull SfaExperimentPart experimentPart,
                                                       @Nullable Integer waitForReadyDelay, JobStateFactory jobStateFactory) {
        super(job, experimentPart, waitForReadyDelay);
        this.jobStateFactory = jobStateFactory;
    }

    @Override
    public ExperimentTaskStatus statefulRun() throws JFedException, InterruptedException {
        UpdateUntilReadyOrFailTaskState statusState =
                jobStateFactory.createGetStatusUntilReadyOrFailTaskState(job, experimentPart);

        if (waitForReadyDelay != null)
            statusState.setWaitForReadyDelay(waitForReadyDelay);

        setAndRunState(statusState);
        return statusState.getStatus();
    }
}
