/*
   HARK Library
   Copyright (C) 2021 Honda Research Institute Japan Inc.
 */

#if !defined(_HARKLIB_NODE_SLEEP_H_INCLUDED_)
#define _HARKLIB_NODE_SLEEP_H_INCLUDED_ 1

#include <memory>
#include <chrono>
#include <thread>
#include <atomic>

#include "hark-lib/hark-lib.h"
#include "hark-lib/node.h"

namespace hark
{
namespace node
{
    class Sleep: public NodeDef, public std::enable_shared_from_this<Sleep>
    {
    public:
        Sleep();
        ~Sleep();

        void initialize(InputDataBundle &input);
        virtual void reset();
        virtual bool operator () (size_t count, InputDataBundle &input, OutputDataBundle &output);

    private:
        std::shared_ptr<Sleep> self; // To safely use "this" in a lambda function in another thread.

        std::chrono::high_resolution_clock::time_point start;
        std::chrono::milliseconds interrupt_cycle;
        std::atomic<bool> loop_continue;
        std::atomic<size_t> stay_count;
        std::thread *timer_th;
        float seconds;
        bool use_input_output;
    };
}
}

#endif /* #if !defined(_HARKLIB_NODE_SLEEP_H_INCLUDED_) */

/* end of file */
