#include <softbus_opcua/UaEventBridge.h>

#include <softbus_core/logging/Logger.h>

namespace softbus::opcua {

UaEventBridge::UaEventBridge(UA_Server* server)
    : server_(server)
{
}

void UaEventBridge::attachBus(softbus::bus::Bus* bus)
{
    bus_ = bus;
    if (!bus_) {
        return;
    }
    eventSub_ = bus_->subscribe("event/#", [this](const softbus::bus::BusMessage& msg) {
        onBusMessage(msg);
    });
}

void UaEventBridge::onBusMessage(const softbus::bus::BusMessage& message)
{
    if (message.type != softbus::bus::BusPayloadType::Event) {
        return;
    }
    emitDeviceOffline(message.event.deviceId + ": " + message.event.message);
}

void UaEventBridge::emitHeartbeat(const std::string& objectRef)
{
    SB_LOG_DEBUG("UaEventBridge", "Heartbeat stub for ", objectRef);
    (void)server_;
}

void UaEventBridge::emitDeviceOnline(const std::string& stableDeviceKey)
{
    SB_LOG_INFO("UaEventBridge", "Device online stub: ", stableDeviceKey);
    (void)server_;
}

void UaEventBridge::emitDeviceOffline(const std::string& stableDeviceKey)
{
    SB_LOG_INFO("UaEventBridge", "Device offline stub: ", stableDeviceKey);
    (void)server_;
}

void UaEventBridge::emitParseError(const std::string& objectRef, const std::string& detail)
{
    SB_LOG_WARN("UaEventBridge", "Parse error at ", objectRef, ": ", detail);
    (void)server_;
}

} // namespace softbus::opcua
