#ifndef _MARIONETTE_H
#define _MARIONETTE_H

#include <boost/asio/signal_set.hpp>
#include <cstdint>
#include <atomic>
#include <functional>
#include <memory>
#include <spinscale/component.h>

namespace sscl {

class PuppeteerThread;

} // namespace sscl

namespace smo {
namespace mrntt {

class MarionetteComponent
:	public sscl::pptr::PuppeteerComponent
{
public:
	MarionetteComponent(const std::shared_ptr<sscl::PuppeteerThread> &thread)
	:	sscl::pptr::PuppeteerComponent(thread)
	{}
	~MarionetteComponent() = default;

public:
	typedef std::function<void(bool)> mrnttLifetimeMgmtOpCbFn;
	void initializeReq(sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback);
	void finalizeReq(sscl::Callback<mrnttLifetimeMgmtOpCbFn> callback);
	// Intentionally doesn't take a callback.
	void exceptionInd();

	void handleLoopExceptionHook() override;

	static void preJoltHook(sscl::PuppeteerThread &thr);

protected:
	void postJoltHook() override;
	void tryBlock1Hook() override;
	void preLoopHook() override;
	void postLoopHook() override;
	void postTryBlock1CatchHook() override;
	void handleTryBlock1TypedException(const std::exception& e) override;
	void handleTryBlock1UnknownException() override;

private:
	class MrnttLifetimeMgmtOp;
	class TerminationEvent;

	std::unique_ptr<boost::asio::signal_set> signals;
	bool callShutdownSalmanoff = false;
};

extern std::shared_ptr<sscl::PuppeteerThread> thread;
extern MarionetteComponent mrntt;

void marionetteInitializeReqCb(bool success);
void marionetteFinalizeReqCb(bool success);

} // namespace mrntt
} // namespace smo

#endif
