#pragma once

#include <QByteArray>
#include <QJsonObject>
#include <QMap>
#include <QList>
#include <QHash>
#include <QSharedPointer>
#include <atomic>
#include <memory>
#include <thread>
#include <functional>

#include "core/memory/MemoryPool.h"
#include "core/plugin_system/PluginManager.h"
#include "device_bus/tree/DeviceTreeModel.h"
#include "device_bus/registry/IDeviceRegistry.h"
#include "devices/DeviceTypes.h"
#include "devices/physical/SerialDevice.h"
#include "message_bus/egress/EgressRouter.h"
#include "message_bus/ingress/DriverIngressAdapter.h"
#include "message_bus/pipeline/PipelineEngine.h"

class DeviceBusManager
{
public:
    ~DeviceBusManager();

    void setDeviceTreeModel(std::shared_ptr<DeviceTreeModel> model);
    void setRegistry(softbus::device_bus::registry::IDeviceRegistry* registry);
    void setDirtyCallback(std::function<void()> cb);

    bool initialize(const QJsonObject& rootConfig);
    bool reconcileFromTreeModel();
    void shutdown();

private:
    void initializeSerialDevice(const QString& endpoint, const QJsonObject& params);
    void shutdownSerialDevice(const QString& endpoint);
    void processEgressLoop();
    // void initializeCanDriver();
    // void shutdownCanDriver();
    // void initializeTcpDriver();
    // void shutdownTcpDriver();
    // void initializeUdpDriver();
    // void shutdownUdpDriver();



private:
    std::shared_ptr<DeviceTreeModel> m_treeModel;
    // 添加map来记住所有设备的endpoint和type的映射
    QMap<QString, softbus::devices::DeviceKind> m_deviceEndpointsMap;

    QHash<QString, QSharedPointer<softbus::devices::physical::SerialDevice>> m_serialDevicesByEndpointSharedPtr;

    softbus::device_bus::registry::IDeviceRegistry* m_registry = nullptr;
    std::shared_ptr<softbus::core::memory::MemoryPool> m_memoryPool;
    std::shared_ptr<softbus::core::plugin_system::PluginManager> m_pluginManager;
    std::unique_ptr<softbus::message_bus::pipeline::PipelineEngine> m_pipelineEngine;
    std::unique_ptr<softbus::message_bus::ingress::DriverIngressAdapter> m_ingressAdapter;
    std::unique_ptr<softbus::message_bus::egress::EgressRouter> m_egressRouter;
    std::atomic<bool> m_egressRunning{false};
    std::thread m_egressThread;
    std::function<void()> m_dirtyCb;


};

