package be.iminds.jfed.gts_highlevel.jobs.gts_parts;

import be.iminds.ilabt.jfed.experiment.tasks.ExperimentTaskStatus;
import be.iminds.ilabt.jfed.highlevel.jobs.Job;
import be.iminds.ilabt.jfed.highlevel.jobs.SingleCallState;
import be.iminds.ilabt.jfed.highlevel.jobs.StateSlice;
import be.iminds.ilabt.jfed.lowlevel.JFedException;
import be.iminds.jfed.gts_highlevel.model.GtsXmlProject;
import be.iminds.jfed.gts_highlevel.tasks.DeleteGtsProjectTask;
import be.iminds.jfed.gts_highlevel.tasks.GtsTaskFactory;

/**
 * User: twalcari
 * Date: 1/18/2016
 * Time: 17:17
 */
public class DestroyGtsProjectStateSlice extends StateSlice {

    private final GtsExperimentPartsContainer container;
    private final GtsXmlProject project;
    private final GtsTaskFactory gtsTaskFactory;

    protected DestroyGtsProjectStateSlice(Job<?> job, GtsExperimentPartsContainer container, GtsTaskFactory gtsTaskFactory) {
        super(job);
        this.container = container;
        this.gtsTaskFactory = gtsTaskFactory;

        assert container.shouldBeDestroyedOnTermination();

        this.project = container.getGtsProject();

        if(project == null)
            throw new IllegalStateException(String.format("Could not find GTS project registered in experiment %s", experiment.getName()));
    }

    @Override
    public String getName() {
        return "Destroy GTS Project '" + project.getId() + "'";
    }

    @Override
    public ExperimentTaskStatus statefulRun() throws JFedException, InterruptedException {
        final DeleteGtsProjectState deleteGtsProjectState = new DeleteGtsProjectState();
        setAndRunState(deleteGtsProjectState);

        return deleteGtsProjectState.getStatus();
    }


    public final class DeleteGtsProjectState extends SingleCallState<DeleteGtsProjectTask> {

        protected DeleteGtsProjectState() {
            super(gtsTaskFactory.createDeleteGtsProjectTask(project));
        }
    }
}
