#include "core/plugin_system/PluginManager.h"

namespace softbus::core::plugin_system
{

void PluginManager::registerProtocolPlugin(std::shared_ptr<IProtocolPlugin> plugin)
{
    if (!plugin) {
        return;
    }
    m_protocolPlugins.insert(plugin->pluginId(), std::move(plugin));
}

std::shared_ptr<IProtocolPlugin> PluginManager::selectProtocolPlugin(const QString& protocolHint) const
{
    for (auto it = m_protocolPlugins.constBegin(); it != m_protocolPlugins.constEnd(); ++it) {
        if (it.value() && it.value()->supports(protocolHint)) {
            return it.value();
        }
    }
    return {};
}

void PluginManager::unregisterProtocolPlugin(const QString& pluginId)
{
    m_protocolPlugins.remove(pluginId);
}

void PluginManager::registerProtocolFramerPlugin(std::shared_ptr<IProtocolFramerPlugin> plugin)
{
    if (!plugin) {
        return;
    }
    m_protocolFramerPlugins.insert(plugin->pluginId(), std::move(plugin));
}

std::shared_ptr<IProtocolFramerPlugin> PluginManager::selectProtocolFramerPlugin(const QString& protocolHint) const
{
    for (auto it = m_protocolFramerPlugins.constBegin(); it != m_protocolFramerPlugins.constEnd(); ++it) {
        if (it.value() && it.value()->supports(protocolHint)) {
            return it.value();
        }
    }
    return {};
}

void PluginManager::unregisterProtocolFramerPlugin(const QString& pluginId)
{
    m_protocolFramerPlugins.remove(pluginId);
}

void PluginManager::registerProtocolMapperPlugin(std::shared_ptr<IProtocolMapperPlugin> plugin)
{
    if (!plugin) {
        return;
    }
    m_protocolMapperPlugins.insert(plugin->pluginId(), std::move(plugin));
}

std::shared_ptr<IProtocolMapperPlugin> PluginManager::selectProtocolMapperPlugin(const QString& protocolHint) const
{
    for (auto it = m_protocolMapperPlugins.constBegin(); it != m_protocolMapperPlugins.constEnd(); ++it) {
        if (it.value() && it.value()->supports(protocolHint)) {
            return it.value();
        }
    }
    return {};
}

void PluginManager::unregisterProtocolMapperPlugin(const QString& pluginId)
{
    m_protocolMapperPlugins.remove(pluginId);
}

QStringList PluginManager::registeredProtocolPluginIds() const
{
    return m_protocolPlugins.keys();
}

QStringList PluginManager::registeredProtocolFramerPluginIds() const
{
    return m_protocolFramerPlugins.keys();
}

QStringList PluginManager::registeredProtocolMapperPluginIds() const
{
    return m_protocolMapperPlugins.keys();
}

} // namespace softbus::core::plugin_system
