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

import be.iminds.ilabt.jfed.highlevel.controller.TaskExecution;
import be.iminds.ilabt.jfed.lowlevel.AnyCredential;
import be.iminds.ilabt.jfed.lowlevel.api_wrapper.AggregateManagerWrapper;
import be.iminds.ilabt.jfed.lowlevel.api_wrapper.impl.AutomaticAggregateManagerWrapper;
import be.iminds.ilabt.jfed.fedmon.webapi.service.json.*;
import be.iminds.ilabt.jfed.util.GeniUrn;
import be.iminds.ilabt.jfed.util.JavaFXLogger;

import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.List;

/**
 * User: twalcari
 * Date: 6/16/2015
 * Time: 15:39
 */
@Singleton
public class AggregateManagerWrapperFactory {
    private final AutomaticAggregateManagerWrapper.AutomaticAggregateManagerWrapperFactory
            automaticAggregateManagerWrapperFactory;
    private final JavaFXLogger logger;


    @Inject
    public AggregateManagerWrapperFactory(AutomaticAggregateManagerWrapper.AutomaticAggregateManagerWrapperFactory automaticAggregateManagerWrapperFactory,
                                          JavaFXLogger logger) {
        this.automaticAggregateManagerWrapperFactory = automaticAggregateManagerWrapperFactory;
        this.logger = logger;
    }

    public void setSpeaksFor(List<AnyCredential> speaksForCredentials, @Nullable GeniUrn speakingForUrn) {
        this.automaticAggregateManagerWrapperFactory.setSpeaksFor(speaksForCredentials, speakingForUrn);
    }
    public boolean isSpeaksFor() {
        return this.automaticAggregateManagerWrapperFactory.isSpeaksFor();
    }
    public List<AnyCredential> getSpeaksForCredentials() {
        return this.automaticAggregateManagerWrapperFactory.getSpeaksForCredentials();
    }
    public GeniUrn getSpeaksForUserUrn() {
        return this.automaticAggregateManagerWrapperFactory.getSpeaksForUserUrn();
    }


    public AggregateManagerWrapper getAggregateManagerWrapper(Server amServer){
        return automaticAggregateManagerWrapperFactory.create(amServer);
    }


    /**
     * this is public, anyone who wants to use this directly is welcome.
     *
     * @param amServer        the Aggregate Manager to connect to
     * @param taskExecution the TaskExecution object that needs to receive copies of all calls. May be null.
     */
    public AggregateManagerWrapper getAggregateManagerWrapper(TaskExecution taskExecution, Server amServer) {
        JavaFXLogger wrappedLogger;

        if (taskExecution != null) {
            wrappedLogger = JavaFXLogger.wrap(logger, taskExecution);
        } else {
            wrappedLogger = logger;
        }

        return automaticAggregateManagerWrapperFactory.create(wrappedLogger, amServer);
    }
}
