#ifndef DEVICEREATTACHER_H
#define DEVICEREATTACHER_H

#include <functional>
#include <memory>
#include <boost/asio/deadline_timer.hpp>
#include <spinscale/co/dynamicPostingInvoker.h>
#include <spinscale/co/nonViralTaskNursery.h>
#include <spinscale/co/postTarget.h>
#include <spinscale/syncCancelerForAsyncWork.h>

namespace smo {

namespace device {

class DeviceManager;

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

	DeviceReattacher(const DeviceReattacher&) = delete;
	DeviceReattacher& operator=(const DeviceReattacher&) = delete;

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

private:
	sscl::co::DynamicNonViralPostingInvoker deviceReattacherCDaemon(
		sscl::co::ExplicitPostTarget postTarget,
		std::exception_ptr &exceptionPtr,
		std::function<void()> callback,
		sscl::SyncCancelerForAsyncWork &canceler);

	DeviceManager &parent;
	// io_context thread for timer (see ctor).
	std::shared_ptr<sscl::ComponentThread> ioThread;
	sscl::co::NonViralTaskNursery taskNursery;
	boost::asio::deadline_timer daemonTimer;
};

} // namespace device
} // namespace smo

#endif // DEVICEREATTACHER_H
