#ifndef DEVICEREATTACHER_H
#define DEVICEREATTACHER_H

#include <boostAsioLinkageFix.h>
#include <atomic>
#include <memory>
#include <boost/asio/deadline_timer.hpp>
#include <spinscale/spinLock.h>

namespace smo {

namespace device {

class DeviceManager;

class DeviceReattacher
{
public:
    DeviceReattacher(
		DeviceManager& parent, std::shared_ptr<sscl::ComponentThread> ioThread);
    ~DeviceReattacher() = default;

    // Non-copyable
    DeviceReattacher(const DeviceReattacher&) = delete;
    DeviceReattacher& operator=(const DeviceReattacher&) = delete;

    // Control methods
    void start();
    void stop();

private:
    void scheduleNextTimeout();
    void onTimeout(const boost::system::error_code& error);

    DeviceManager &parent;
    std::shared_ptr<sscl::ComponentThread> ioThread;
    sscl::SpinLock shouldContinueLock;
    bool shouldContinue;
    boost::asio::deadline_timer timer;
};

} // namespace device
} // namespace smo

#endif // DEVICEREATTACHER_H
