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.log.Logger;
import be.iminds.ilabt.jfed.log.ResultListener;
import be.iminds.ilabt.jfed.lowlevel.JFedException;
import be.iminds.ilabt.jfed.lowlevel.api_wrapper.UserAndSliceApiWrapper;
import be.iminds.ilabt.jfed.fedmon.webapi.service.json.*;
import be.iminds.ilabt.jfed.util.GeniUrn;
import be.iminds.ilabt.jfed.util.JavaFXLogger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * User: twalcari
 * Date: 6/17/2015
 * Time: 15:24
 */
public final class UnregisterSliversTask extends Task {
    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(UnregisterSliversTask.class);

    private final Slice slice;
    private final Collection<GeniUrn> deletedSliverUrns;
    private final Task extraTaskToDependOn;
    private final Server auth;
    private final ResultListener[] extraResultListeners;

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

    public UnregisterSliversTask(Slice slice, Server auth, Collection<GeniUrn> deletedSliverUrns,
                                 Task extraTaskToDependOn, UserAndSliceApiWrapper userAndSliceApiWrapper,
                                 JavaFXLogger logger, HighLevelTaskFactory highLevelTaskFactory, ResultListener... extraResultListeners) {
        super("UnregisterSliversTask slivers deleted @ " + auth.getDefaultComponentManagerUrn());

        this.slice = slice;
        this.deletedSliverUrns = deletedSliverUrns;
        this.auth = auth;
        this.extraTaskToDependOn = extraTaskToDependOn;
        this.userAndSliceApiWrapper = userAndSliceApiWrapper;
        this.logger = logger;
        this.highLevelTaskFactory = highLevelTaskFactory;
        this.extraResultListeners = extraResultListeners;
    }

    @Override
    protected void doTask(TaskExecution taskExecution) throws JFedException, InterruptedException {
        ResultListener[] resultListeners = Arrays.copyOf(extraResultListeners, extraResultListeners.length + 1);
        resultListeners[extraResultListeners.length] = taskExecution;
        Logger logger = JavaFXLogger.wrap(this.logger, resultListeners);

        Collection<GeniUrn> sliverUrns = deletedSliverUrns;
        LOG.debug("UnregisterSliversTask has {} sliver Urns to unregister.", sliverUrns.size());
        if (!sliverUrns.isEmpty())
            userAndSliceApiWrapper.unregisterAggregatesForSlice(logger,
                    slice.getCredentials(), slice.getUrn(),
                    auth.getDefaultComponentManagerAsGeniUrn(), sliverUrns);
    }

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