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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;

/**
 * User: twalcari
 * Date: 6/16/2015
 * Time: 17:36
 */
public class ProjectMemberSshKeysTask extends Task {
    private static final Logger LOG = LoggerFactory.getLogger(ProjectMemberSshKeysTask.class);

    private final String projectName;

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

    private int totalMembersCount = -1;
    private int totalKeyCount = -1;
    private int memberInfoRequested = 0;

    public ProjectMemberSshKeysTask(UserAndSliceApiWrapper userAndSliceApiWrapper, JavaFXLogger logger,
                                    HighLevelTaskFactory highLevelTaskFactory) {
        super("Get all User SSH Keys");
        this.userAndSliceApiWrapper = userAndSliceApiWrapper;
        this.logger = logger;
        this.highLevelTaskFactory = highLevelTaskFactory;
        this.projectName = null;
    }

    public ProjectMemberSshKeysTask(@Nonnull String projectName, UserAndSliceApiWrapper userAndSliceApiWrapper,
                                    JavaFXLogger logger, HighLevelTaskFactory highLevelTaskFactory) {
        super("Get User SSH Keys For users in project " + projectName);
        this.projectName = projectName;
        this.userAndSliceApiWrapper = userAndSliceApiWrapper;
        this.logger = logger;
        this.highLevelTaskFactory = highLevelTaskFactory;
    }

    @Override
    public void doTask(TaskExecution taskExecution) throws JFedException, InterruptedException {
        List<GeniUrn> userUrns =
                userAndSliceApiWrapper.getUsersForSubAuthority(JavaFXLogger.wrap(logger, taskExecution), projectName);
        if (userUrns == null) {
            LOG.warn("Could not retrieve SSH-keys of other users");
            userUrns = Collections.emptyList();
        }

        totalMembersCount = userUrns.size();

        totalKeyCount = 0;
        for (GeniUrn userUrn : userUrns) {
            try {
                memberInfoRequested++;
                List<String> keys = userAndSliceApiWrapper.getSshKeysForUser(JavaFXLogger.wrap(logger, taskExecution), userUrn);
                totalKeyCount += keys != null ? keys.size() : 0;
            } catch (JFedException e) {
                LOG.warn("Could not get user key for {}", userUrn, e);
            }
        }
    }

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

    public int getTotalMembersCount() {
        return totalMembersCount;
    }

    public int getTotalKeyCount() {
        return this.totalKeyCount;
    }

    public int getMemberInfoRequested() {
        return memberInfoRequested;
    }
}
