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.ExperimentPartStateSlice;
import be.iminds.ilabt.jfed.highlevel.jobs.Job;
import be.iminds.ilabt.jfed.highlevel.jobs.SingleCallState;
import be.iminds.ilabt.jfed.highlevel.model.Sliver;
import be.iminds.ilabt.jfed.highlevel.tasks.HighLevelTaskFactory;
import be.iminds.ilabt.jfed.highlevel.tasks.RegisterSliversToSLACollectorTask;
import be.iminds.ilabt.jfed.lowlevel.JFedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.util.stream.Collectors;

/**
 * User: twalcari
 * Date: 1/14/2016
 * Time: 12:05
 */
public class RegisterSliversToSlaExperimentPartStateSlice extends ExperimentPartStateSlice<SfaExperimentPart> {
    private static final Logger LOG = LoggerFactory.getLogger(RegisterSliversToSlaExperimentPartStateSlice.class);
    private final HighLevelTaskFactory hltf;

    public RegisterSliversToSlaExperimentPartStateSlice(@Nonnull Job<?> job, @Nonnull SfaExperimentPart experimentPart,
                                                        HighLevelTaskFactory hltf) {
        super(job, experimentPart);
        this.hltf = hltf;
    }

    @Override
    public ExperimentTaskStatus statefulRun() throws JFedException, InterruptedException {
         setAndRunState(new RegisterSliversToSlaCollectorStateSlice());
        return null;
    }

    private class RegisterSliversToSlaCollectorStateSlice extends SingleCallState<RegisterSliversToSLACollectorTask> {

        protected RegisterSliversToSlaCollectorStateSlice() {
            //noinspection ConstantConditions
            super(hltf.registerSliversToSLACollector(experiment.getSlice(),
                    experimentPart.getComponentManagerSfaAuthority(),
                    experimentPart.getSlivers().stream().map(Sliver::getUrn).collect(Collectors.toList())));
        }
    }
}
