
#include <cmath>
#include <complex>
#include <algorithm>
#include <iostream>

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

namespace hark
{
namespace node
{
    Delay::Delay():
        NodeDef("Delay")
    {
        *this
            << Terminal::in("INPUT")
            << Terminal::in("DELAY").def(0)
            >> Terminal::out("OUTPUT");
    }

    void
    Delay::reset()
    {
    }

    void
    Delay::initialize(InputDataBundle &input)
    {
        this->delay = input["DELAY"].last().as<int>();

        int look_ahead = (this->delay > 0) ? this->delay : 0;
        int look_back = (this->delay < 0) ? -this->delay : 0;

        input["INPUT"].set_look_window(look_back, look_ahead);
    }

    bool
    Delay::operator () (size_t count, InputDataBundle &input, OutputDataBundle &output)
    {
#ifdef DEBUG
        ptime start_time;
        ptime end_time;
        start_time = microsec_clock::local_time();
#endif

        if(count == 0){ this->initialize(input); }
        if(!input["INPUT"].is_source_ready()){ return false; }

        if(count - this->delay >= 0){
            output["OUTPUT"].push(input["INPUT"].at(count - this->delay));
        }
        else{
            data empty;
            output["OUTPUT"].push(empty);
        }

#ifdef DEBUG
        end_time = microsec_clock::local_time();
        std::cout << end_time - start_time << std::endl;
#endif

        return true;
    }
}
}

/* end of file */
