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.lowlevel.GeniUserProvider;
import be.iminds.ilabt.jfed.lowlevel.JFedException;
import be.iminds.ilabt.jfed.lowlevel.ApiInfo;
import be.iminds.ilabt.jfed.lowlevel.api.AbstractFederationApi;
import be.iminds.ilabt.jfed.lowlevel.api.FederationSliceAuthorityApi2;
import be.iminds.ilabt.jfed.fedmon.webapi.service.json.*;
import be.iminds.ilabt.jfed.lowlevel.connection.JFedConnection;
import be.iminds.ilabt.jfed.lowlevel.connection.JFedConnectionProvider;
import be.iminds.ilabt.jfed.lowlevel.connection.SfaConnection;
import be.iminds.ilabt.jfed.preferences.JFedPreferences;
import be.iminds.ilabt.jfed.util.JavaFXLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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


    private final Server userAuthority;

    private boolean supportSliverRegistration;
    private boolean known;

    private final JavaFXLogger logger;
    private final GeniUserProvider geniUserProvider;
    private final JFedConnectionProvider connectionProvider;
    private final JFedPreferences jFedPreferences;


    public UserAuthorityGetVersionTask(Server userAuthority, JavaFXLogger logger,
                                       GeniUserProvider geniUserProvider, JFedConnectionProvider connectionProvider,
                                       JFedPreferences jFedPreferences) {
        super("GetVersion Info @ " + userAuthority.getName());

        this.userAuthority = userAuthority;
        this.logger = logger;
        this.geniUserProvider = geniUserProvider;
        this.connectionProvider = connectionProvider;
        this.jFedPreferences = jFedPreferences;
        supportSliverRegistration = false; //initial value
        known = false;

        boolean hasFedMAApiv2 = ApiInfo.hasService(userAuthority, ApiInfo.ApiName.GENI_CH_MA, 2);
        boolean hasFedSAApiv2 = ApiInfo.hasService(userAuthority, ApiInfo.ApiName.GENI_CH_SA, 2);

        if (!hasFedSAApiv2) {
            known = true;
            supportSliverRegistration = false;
        }

        LOG.debug("UserAuthorityGetVersionTask: init done known={} supportSliverRegistration={} hasFedMAApiv2={} hasFedSAApiv2={}",
                known, supportSliverRegistration, hasFedMAApiv2, hasFedSAApiv2);
    }

    @Override
    public void doTask(TaskExecution taskExecution) throws JFedException, InterruptedException {
        FederationSliceAuthorityApi2 sa =
                new FederationSliceAuthorityApi2(JavaFXLogger.wrap(logger, taskExecution), jFedPreferences);

        JFedConnection con =
                connectionProvider.getConnectionByAuthority(geniUserProvider.getLoggedInGeniUser(), userAuthority, FederationSliceAuthorityApi2.class);

        AbstractFederationApi.FederationApiReply<FederationSliceAuthorityApi2.GetVersionSAResult> gv =
                sa.getVersion((SfaConnection) con);
        if (!gv.getGeniResponseCode().isSuccess()) {
            known = true;
            supportSliverRegistration = false;
            throw new JFedException("GetVersion call failed", gv.getXmlRpcCallDetailsWithCodeValueError(), gv.getGeniResponseCode());
        }
        FederationSliceAuthorityApi2.GetVersionSAResult r = gv.getValue();
        if (r == null) {
            known = true;
            supportSliverRegistration = false;
            throw new JFedException("null reply for GetVersion call", gv.getXmlRpcCallDetailsWithCodeValueError(), gv.getGeniResponseCode());
        }
        known = true;
        supportSliverRegistration = r.getServices().contains("SLIVER_INFO");
    }

    public boolean isKnown() {
        return known;
    }

    public boolean isSupportSliverRegistration() {
        return supportSliverRegistration;
    }
}
