#pragma once

#include <softbus_bus/Bus.h>

#include <chrono>
#include <mutex>
#include <string>
#include <unordered_map>

namespace softbus::monitor {

class HeartbeatMonitor {
public:
    explicit HeartbeatMonitor(std::chrono::milliseconds timeout = std::chrono::seconds(5));

    void attachBus(softbus::bus::Bus* bus);
    void tick();

private:
    void onHeartbeat(const softbus::bus::BusMessage& message);
    void publishOfflineEvent(const std::string& deviceId);

    softbus::bus::Bus* bus_{nullptr};
    softbus::bus::Bus::SubscriptionId hbSub_{0};
    std::chrono::milliseconds timeout_;
    std::mutex mutex_;
    std::unordered_map<std::string, std::chrono::steady_clock::time_point> lastSeen_;
};

} // namespace softbus::monitor
