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.highlevel.util.AggregateManagerWrapperFactory;
import be.iminds.ilabt.jfed.lowlevel.JFedException;
import be.iminds.ilabt.jfed.lowlevel.api_wrapper.AggregateManagerWrapper;
import be.iminds.ilabt.jfed.fedmon.webapi.service.json.*;

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

/**
 * User: twalcari
 * Date: 6/17/2015
 * Time: 16:40
 */
public class UnshareLanTask extends Task {
    private final Slice slice;
    private final Server auth;
    private final String lanClientId;
    private final String sharedLanName;

    private final AggregateManagerWrapperFactory aggregateManagerWrapperFactory;
    private final HighLevelTaskFactory highLevelTaskFactory;

    public UnshareLanTask(Slice slice, Server auth, String lanClientId, String sharedLanName,
                          AggregateManagerWrapperFactory aggregateManagerWrapperFactory,
                          HighLevelTaskFactory highLevelTaskFactory) {
        super("Unshare Lan " + lanClientId + " as " + sharedLanName);
        this.slice = slice;
        this.auth = auth;
        this.lanClientId = lanClientId;
        this.sharedLanName = sharedLanName;
        this.aggregateManagerWrapperFactory = aggregateManagerWrapperFactory;
        this.highLevelTaskFactory = highLevelTaskFactory;
    }

    @Override
    public void doTask(TaskExecution taskExecution) throws JFedException, InterruptedException {
        AggregateManagerWrapper amWrapper =
                aggregateManagerWrapperFactory.getAggregateManagerWrapper(taskExecution, auth);
        amWrapper.unshareLan(slice.getUrn(), slice.getCredentials(), lanClientId, sharedLanName);
    }

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