#include "core/plugin_system/PluginManager.h"

namespace softbus::core::plugin_system
{

void PluginManager::registerProtocolPlugin(std::shared_ptr<IProtocolPlugin> plugin)
{
    if (!plugin) {
        return;
    }
    const auto protocol = plugin->protocolType();
    m_protocolPluginsByType.insert(protocol, plugin);
    m_protocolPluginsById.insert(plugin->pluginId(), std::move(plugin));
}

std::shared_ptr<IProtocolPlugin> PluginManager::selectProtocolPlugin(softbus::core::models::ProtocolType protocol) const
{
    return m_protocolPluginsByType.value(protocol);
}

void PluginManager::unregisterProtocolPlugin(const QString& pluginId)
{
    const auto it = m_protocolPluginsById.find(pluginId);
    if (it == m_protocolPluginsById.end() || !it.value()) {
        return;
    }
    m_protocolPluginsByType.remove(it.value()->protocolType());
    m_protocolPluginsById.erase(it);
}

void PluginManager::registerProtocolFramerPlugin(std::shared_ptr<IProtocolFramerPlugin> plugin)
{
    if (!plugin) {
        return;
    }
    const auto protocol = plugin->protocolType();
    m_protocolFramerPluginsByType.insert(protocol, plugin);
    m_protocolFramerPluginsById.insert(plugin->pluginId(), std::move(plugin));
}

std::shared_ptr<IProtocolFramerPlugin> PluginManager::selectProtocolFramerPlugin(
    softbus::core::models::ProtocolType protocol) const
{
    return m_protocolFramerPluginsByType.value(protocol);
}

void PluginManager::unregisterProtocolFramerPlugin(const QString& pluginId)
{
    const auto it = m_protocolFramerPluginsById.find(pluginId);
    if (it == m_protocolFramerPluginsById.end() || !it.value()) {
        return;
    }
    m_protocolFramerPluginsByType.remove(it.value()->protocolType());
    m_protocolFramerPluginsById.erase(it);
}

void PluginManager::registerProtocolMapperPlugin(std::shared_ptr<IProtocolMapperPlugin> plugin)
{
    if (!plugin) {
        return;
    }
    const auto protocol = plugin->protocolType();
    m_protocolMapperPluginsByType.insert(protocol, plugin);
    m_protocolMapperPluginsById.insert(plugin->pluginId(), std::move(plugin));
}

std::shared_ptr<IProtocolMapperPlugin> PluginManager::selectProtocolMapperPlugin(
    softbus::core::models::ProtocolType protocol) const
{
    return m_protocolMapperPluginsByType.value(protocol);
}

void PluginManager::unregisterProtocolMapperPlugin(const QString& pluginId)
{
    const auto it = m_protocolMapperPluginsById.find(pluginId);
    if (it == m_protocolMapperPluginsById.end() || !it.value()) {
        return;
    }
    m_protocolMapperPluginsByType.remove(it.value()->protocolType());
    m_protocolMapperPluginsById.erase(it);
}

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

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

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

} // namespace softbus::core::plugin_system
