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

import be.iminds.ilabt.jfed.highlevel.controller.JFedHighLevelException;
import be.iminds.ilabt.jfed.highlevel.controller.Task;
import be.iminds.ilabt.jfed.highlevel.controller.TaskExecution;
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.lowlevel.api_wrapper.UserAndSliceApiWrapper;
import be.iminds.ilabt.jfed.fedmon.webapi.service.json.*;

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

/**
 * User: twalcari
 * Date: 6/17/2015
 * Time: 11:22
 */
public final class GetAdvertisementTask extends Task {

    private final Server auth;
    private final boolean available;
    private String advertisementRspecString;

    private final UserAndSliceApiWrapper userAndSliceApiWrapper;
    private final AggregateManagerWrapperFactory aggregateManagerWrapperFactory;
    private final HighLevelTaskFactory highLevelTaskFactory;


    private GetAdvertisementTask(Server auth, boolean available, UserAndSliceApiWrapper userAndSliceApiWrapper,
                                AggregateManagerWrapperFactory aggregateManagerWrapperFactory,
                                HighLevelTaskFactory highLevelTaskFactory) {
        super("Fetch Advertisement RSpec on " + auth.getName() + " (" + (available ? "available)" : "all)"));
        this.auth = auth;
        this.available = available;
        this.userAndSliceApiWrapper = userAndSliceApiWrapper;
        this.aggregateManagerWrapperFactory = aggregateManagerWrapperFactory;
        this.highLevelTaskFactory = highLevelTaskFactory;
    }

    public static GetAdvertisementTask createFetchAllAdvertisementTask(Server auth, UserAndSliceApiWrapper userAndSliceApiWrapper,
                                                                AggregateManagerWrapperFactory aggregateManagerWrapperFactory,
                                                                HighLevelTaskFactory highLevelTaskFactory) {
        return new GetAdvertisementTask(auth, false, userAndSliceApiWrapper, aggregateManagerWrapperFactory, highLevelTaskFactory);
    }

    public static GetAdvertisementTask createFetchAvailableAdvertisementTask(Server auth, UserAndSliceApiWrapper userAndSliceApiWrapper,
                                                                AggregateManagerWrapperFactory aggregateManagerWrapperFactory,
                                                                HighLevelTaskFactory highLevelTaskFactory) {
        return new GetAdvertisementTask(auth, true, userAndSliceApiWrapper, aggregateManagerWrapperFactory, highLevelTaskFactory);
    }

    @Override
    public void doTask(TaskExecution taskExecution) throws JFedException, InterruptedException {
        AggregateManagerWrapper amWrapper =
                aggregateManagerWrapperFactory.getAggregateManagerWrapper(taskExecution, auth);

        if (!userAndSliceApiWrapper.hasUserCredentials())
            throw new JFedException("UserAndSliceApiWrapper did not have user Credentials.");

        advertisementRspecString =
                amWrapper.listResources(userAndSliceApiWrapper.getCachedUserCredentialsForAM(), available);

        if (advertisementRspecString == null)
            throw new JFedHighLevelException("Got null when asked " + auth.getDefaultComponentManagerUrn() + " for advertisement.");
    }

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

    public String getAdvertisementRspecString() {
        return advertisementRspecString;
    }
}
