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.SfaModel;
import be.iminds.ilabt.jfed.lowlevel.GeniUserProvider;
import be.iminds.ilabt.jfed.lowlevel.JFedException;
import be.iminds.ilabt.jfed.lowlevel.api_wrapper.UserAndSliceApiWrapper;
import be.iminds.ilabt.jfed.util.JavaFXLogger;

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

/**
 * User: twalcari
 * Date: 6/17/2015
 * Time: 10:18
 */
public class ListSubAuthoritiesTask extends Task {
    private UserAndSliceApiWrapper.SubAuthoritySupport subAuthoritySupport;
    private List<String> subAuthNames;

    private final UserAndSliceApiWrapper userAndSliceApiWrapper;
    private final JavaFXLogger logger;
    private final GeniUserProvider geniUserProvider;
    private final SfaModel sfaModel;
    private final HighLevelTaskFactory highLevelTaskFactory;


    public ListSubAuthoritiesTask(UserAndSliceApiWrapper userAndSliceApiWrapper, JavaFXLogger logger,
                                  GeniUserProvider geniUserProvider, SfaModel sfaModel,
                                  HighLevelTaskFactory highLevelTaskFactory) {
        super("List Sub Authorities");
        this.userAndSliceApiWrapper = userAndSliceApiWrapper;
        this.logger = logger;
        this.geniUserProvider = geniUserProvider;
        this.sfaModel = sfaModel;
        this.highLevelTaskFactory = highLevelTaskFactory;
    }

    @Override
    public void doTask(TaskExecution taskExecution) throws JFedException, InterruptedException {
        subAuthoritySupport = userAndSliceApiWrapper.getSubAuthoritySupport(JavaFXLogger.wrap(logger, taskExecution));
        sfaModel.setSubAuthoritySupport(subAuthoritySupport); //sfaModel cannot always see this itself, as this does not always require a call!

        subAuthNames = userAndSliceApiWrapper.getSubAuthorityNames(
                JavaFXLogger.wrap(logger, taskExecution),
                geniUserProvider.getLoggedInGeniUser().getUserUrn());
    }

    public List<String> getSubAuthNames() {
        return subAuthNames;
    }

    public UserAndSliceApiWrapper.SubAuthoritySupport getSubAuthoritySupport() {
        return subAuthoritySupport;
    }

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