package be.iminds.ilabt.jfed.highlevel.jobs;

import be.iminds.ilabt.jfed.experiment.Experiment;
import be.iminds.ilabt.jfed.highlevel.controller.Task;
import be.iminds.ilabt.jfed.highlevel.controller.TaskThread;
import be.iminds.ilabt.jfed.highlevel.tasks.HighLevelTaskFactory;
import be.iminds.ilabt.jfed.lowlevel.authority.AuthorityFinder;
import be.iminds.ilabt.jfed.fedmon.webapi.service.json.*;
import be.iminds.ilabt.jfed.rspec.model.javafx_impl.FXRspecLink;

import javax.annotation.Nonnull;

/**
 * User: twalcari
 * Date: 12/2/2015
 * Time: 11:24
 */
public class ShareLanJob extends SingleTaskJob<Task, Void> {

    @Nonnull
    private final FXRspecLink link;
    @Nonnull
    private final String sharedLanName;
    private final boolean share;

    private final AuthorityFinder authorityFinder;

    public ShareLanJob(Experiment experiment, @Nonnull FXRspecLink link, @Nonnull String sharedLanName, boolean share,
                       HighLevelTaskFactory hltf, TaskThread tt, AuthorityFinder authorityFinder) {
        super((share ? "Sharing" : "Unsharing") + " link " + link.getClientId() + " with ID '" + sharedLanName + "'",
                experiment, hltf, tt);
        this.link = link;
        this.sharedLanName = sharedLanName;
        this.share = share;
        this.authorityFinder = authorityFinder;
    }

    @Override
    public Task createTask() {
        Server auth = authorityFinder.findByUrn(link.getComponentManagerUrns().get(0), AuthorityFinder.Purpose.CREATE_SLIVER); //contact the same auth as would be contacted for CreateSliver

        if (share)
            return hltf.getShareLanTask(experiment.getSlice(), auth, link.getClientId(), sharedLanName);
        else
            return hltf.getUnshareLanTask(experiment.getSlice(), auth, link.getClientId(), sharedLanName);
    }
}
