package riomhaire.lti;


import org.springframework.web.servlet.ModelAndView;
import riomhaire.lti.core.model.LTIMessage;
import riomhaire.lti.core.model.configuration.ClientConfiguration;
import riomhaire.lti.core.model.configuration.ToolConfiguration;
import riomhaire.lti.core.model.interfaces.ApplicationRegistry;
import riomhaire.lti.core.model.interfaces.CommandDispatcher;
import riomhaire.lti.core.model.interfaces.NoKeyResolver;
import riomhaire.lti.core.model.interfaces.UniKeyResolver;

import java.util.Optional;

/**
 *  ApplicationRegistry is the 'environment' within which commands ... it should contain things like
 *  downstream service proxies etc.
 *
 *  The registry is also where the command dispatcher resides so that commands can call other commands.
 *
 *  The Environment os specific to the message that the command processes and the responses they generate.
 *  In this model all commands take the same message model and return the same consistent model.
 */

public class Registry implements ApplicationRegistry<ModelAndView,LTIMessage> {

    // This is the command dispatcher
    private final CommandDispatcher<ModelAndView, ApplicationRegistry<ModelAndView,LTIMessage>, LTIMessage> commandDispatcher;
    // We need somewhere to read a tool/partner integration from - this service proxy performs that role.
    private final UniKeyResolver<Optional<ClientConfiguration>, String> clientRegistrationResolver;
    // This is where most of the tools config is stored
    private final NoKeyResolver<Optional<ToolConfiguration>> toolConfigurationResolver;

    /**
     * Constructor contains all the services and objects to run the application
     * @param commandDispatcher
     * @param clientRegistrationResolver
     * @param toolConfigurationResolver
     */
    public Registry(CommandDispatcher<ModelAndView, ApplicationRegistry<ModelAndView, LTIMessage>, LTIMessage> commandDispatcher, UniKeyResolver<Optional<ClientConfiguration>, String> clientRegistrationResolver, NoKeyResolver<Optional<ToolConfiguration>> toolConfigurationResolver) {
        this.commandDispatcher = commandDispatcher;
        this.clientRegistrationResolver = clientRegistrationResolver;
        this.toolConfigurationResolver = toolConfigurationResolver;
    }

    /**
     *  Return the current client configuration resolver
     * @return     ClientRegistrationResolver
     */
    @Override
    public UniKeyResolver<Optional<ClientConfiguration>, String> clientRegistrationResolver() {
        return clientRegistrationResolver;
    }

    /**
     * Return the current tool configuration resolver
     * This is where most of the tool configuration is stored -  keys, secrets etc
     *
     * @return ClientRegistrationResolver
     */
    @Override
    public NoKeyResolver<Optional<ToolConfiguration>> toolConfigurationResolver() {
        return toolConfigurationResolver;
    }

    /**
     * Returns access to the current command dispatcher
     *
     * @return  CommandDispatcher
     */
    @Override
    public CommandDispatcher<ModelAndView, ApplicationRegistry<ModelAndView, LTIMessage>, LTIMessage> commandDispatcher() {
        return commandDispatcher;
    }


}

