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.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 javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

/**
 * User: twalcari
 * Date: 6/16/2015
 * Time: 17:54
 */
public class OtherUserSshKeysTask extends Task {
    //input parameters
    private final GeniUrn userUrn;
    private final List<Task> extraTaskDeps;

    //results
    private List<String> keys;

    //environment parameters
    private final UserAndSliceApiWrapper userAndSliceApiWrapper;
    private final JavaFXLogger logger;
    private final HighLevelTaskFactory highLevelTaskFactory;

    public OtherUserSshKeysTask(@Nonnull GeniUrn userUrn, @Nullable List<Task> extraTaskDeps,
                                UserAndSliceApiWrapper userAndSliceApiWrapper, JavaFXLogger logger,
                                HighLevelTaskFactory highLevelTaskFactory) {
        super("Get SSH keys for " + userUrn);
        this.userUrn = userUrn;
        this.extraTaskDeps = extraTaskDeps;
        this.userAndSliceApiWrapper = userAndSliceApiWrapper;
        this.logger = logger;
        this.highLevelTaskFactory = highLevelTaskFactory;
    }

    @Override
    public void doTask(TaskExecution taskExecution) throws JFedException, InterruptedException {
        keys = userAndSliceApiWrapper.getSshKeysForUser(JavaFXLogger.wrap(logger, taskExecution), userUrn);
    }

    public GeniUrn getUserUrn() {
        return userUrn;
    }

    public List<String> getKeys() {
        return keys;
    }

    @Override
    public List<Task> initDependsOn() {
        List<Task> res = new ArrayList<>();
        res.add(highLevelTaskFactory.getUserCredential());
        if (extraTaskDeps != null)
            res.addAll(extraTaskDeps);
        return res;
    }
}
