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: 7/29/2015
 * Time: 11:02
 */
public class ListSliceMembersTask extends Task {
    private final Slice slice;
    private final UserAndSliceApiWrapper userAndSliceApiWrapper;
    private final JavaFXLogger logger;
    private final HighLevelTaskFactory hltf;

    private List<GeniUrn> memberUrns;

    public ListSliceMembersTask(Slice slice, UserAndSliceApiWrapper userAndSliceApiWrapper, JavaFXLogger logger, HighLevelTaskFactory hltf) {
        super("List members of slice " + slice.getName());
        this.slice = slice;
        this.userAndSliceApiWrapper = userAndSliceApiWrapper;
        this.logger = logger;
        this.hltf = hltf;
    }


    @Override
    protected void doTask(TaskExecution taskExecution) throws JFedException, InterruptedException {
        memberUrns =
                userAndSliceApiWrapper.getUsersForSlice(JavaFXLogger.wrap(logger, taskExecution), slice.getUrn(), slice.getCredentials());
    }

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

    public List<GeniUrn> getMemberUrns() {
        return Collections.unmodifiableList(memberUrns);
    }
}
