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

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

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

    private final Slice slice;

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


    private List<GeniUrn> aggregates = null;

    public AggregatesForSliceTask(@Nonnull Slice slice, UserAndSliceApiWrapper userAndSliceApiWrapper,
                                  JavaFXLogger logger, HighLevelTaskFactory highLevelTaskFactory) {
        super("Resolve Slice");
        this.slice = slice;
        this.userAndSliceApiWrapper = userAndSliceApiWrapper;
        this.logger = logger;
        this.highLevelTaskFactory = highLevelTaskFactory;
    }

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

    @Override
    public List<Task> initDependsOn() {
        List<Task> res = new ArrayList<>();
        res.add(highLevelTaskFactory.getUserCredential());
        res.add(highLevelTaskFactory.getSliceCredential(slice));
        return res;
    }

    public List<GeniUrn> getAggregates() {
        return aggregates;
    }

}
