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

import be.iminds.ilabt.jfed.highlevel.controller.Task;
import be.iminds.ilabt.jfed.highlevel.controller.TaskExecution;
import be.iminds.ilabt.jfed.highlevel.model.Slice;
import be.iminds.ilabt.jfed.lowlevel.JFedException;
import be.iminds.ilabt.jfed.lowlevel.api_wrapper.UserAndSliceApiWrapper;
import be.iminds.ilabt.jfed.util.GeniUrn;
import be.iminds.ilabt.jfed.util.JavaFXLogger;

import java.util.Collections;
import java.util.List;

/**
 * User: twalcari
 * Date: 6/17/2015
 * Time: 16:25
 */
public class ShareSliceTask extends Task {
    private final Slice slice;
    private final GeniUrn userUrn;

    private final UserAndSliceApiWrapper userAndSliceApiWrapper;
    private final JavaFXLogger logger;
    private final HighLevelTaskFactory highLevelTaskFactory;

    public ShareSliceTask(Slice slice, GeniUrn userUrn, UserAndSliceApiWrapper userAndSliceApiWrapper, JavaFXLogger logger, HighLevelTaskFactory highLevelTaskFactory) {
        super("Share slice \"" + slice.getUrn().getEncodedResourceName() + "\" with " + userUrn.getEncodedResourceName());
        this.slice = slice;
        this.userUrn = userUrn;
        this.userAndSliceApiWrapper = userAndSliceApiWrapper;
        this.logger = logger;
        this.highLevelTaskFactory = highLevelTaskFactory;
    }

    @Override
    public void doTask(TaskExecution taskExecution) throws JFedException, InterruptedException {
        userAndSliceApiWrapper.shareSlice(
                JavaFXLogger.wrap(logger, taskExecution),
                slice.getUrn(),
                slice.getCredentials(),
                userUrn);

        //no exception, so we can add the user
        slice.addUser(userUrn);
    }

    @Override
    public List<Task> initDependsOn() {
        return Collections.singletonList(highLevelTaskFactory.getSliceCredential(slice));
    }
}
