/*****************************************************************************************
 *                                                                                       *
 * OpenSpace                                                                             *
 *                                                                                       *
 * Copyright (c) 2014-2016                                                               *
 *                                                                                       *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this  *
 * software and associated documentation files (the "Software"), to deal in the Software *
 * without restriction, including without limitation the rights to use, copy, modify,    *
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to    *
 * permit persons to whom the Software is furnished to do so, subject to the following   *
 * conditions:                                                                           *
 *                                                                                       *
 * The above copyright notice and this permission notice shall be included in all copies *
 * or substantial portions of the Software.                                              *
 *                                                                                       *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,   *
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A         *
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT    *
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  *
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE  *
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                                         *
 ****************************************************************************************/

#ifndef __MODULEENGINE_H__
#define __MODULEENGINE_H__

#include <openspace/util/openspacemodule.h>
#include <openspace/scripting/scriptengine.h>

#include <memory>
#include <vector>

namespace openspace {

/**
 * The ModuleEngine is the central repository for registering and accessing
 * OpenSpaceModule for the current application run. By initializing (#initialize) the
 * ModuleEngine, the default set of OpenSpaceModule%s as generated by CMake in the
 * <code>moduleregistration.h</code> file is automatically registered and created.
 * Additional OpenSpaceModule%s can be registered with the #registerModule function, which
 * will internally call the OpenSpaceModule::initialize method.
 */
class ModuleEngine {
public:
    /**
     * Registers all of the OpenSpaceModule%s which are created by the CMake configuration
     * and stored in the <code>moduleregistration.h</code> file. For all of those modules
     * the OpenSpaceModule::initialize method with will called.
     * \throw ghoul::RuntimeError If two modules in the default modules have the same
     * name
    */
    void initialize();
    
    /**
     * Deinitializes all of the contained OpenSpaceModule%s by calling the
     * OpenSpaceModule::deinitialize methods.
     */
    void deinitialize();

    /**
     * Registers the passed \p module with this ModuleEngine. The OpenSpaceModule::create
     * method will be called on the \p module in the process.
     * \param module The OpenSpaceModule that is to be registered
     * \throw ghoul::RuntimeError If the name of the \p module was already registered 
     * previously
     * \pre \p module must not be nullptr
     */
    void registerModule(std::unique_ptr<OpenSpaceModule> module);
    
    /**
     * Returns a list of all registered OpenSpaceModule%s that have been registered with
     * this ModuleEngine. All returned OpenSpaceModule%s are guaranteed to be initialized.
     * \return A list of all registered OpenSpaceModule%s
     */
    std::vector<OpenSpaceModule*> modules() const;

    /**
     * Returns the combined minimum OpenGL version. The return value is the maximum
     * version of all registered modules' OpenGL versions.
     * \return The combined minimum OpenGL version
     */
    ghoul::systemcapabilities::OpenGLCapabilitiesComponent::Version
        requiredOpenGLVersion() const;

    /**
    * Returns the Lua library that contains all Lua functions available to affect the
    * modules.
    */
    static scripting::LuaLibrary luaLibrary();

private:
    /// The list of all registered OpenSpaceModule%s
    std::vector<std::unique_ptr<OpenSpaceModule>> _modules;
};

} // namespace openspace

#endif // __MODULEENGINE_H__
