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

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

namespace hark
{
namespace node
{
    HRLE::HRLE():
        NodeDef("HRLE")
    {
        *this
            << Terminal::in("NB_CHANNELS").def(8)
            << Terminal::in("LENGTH").def(512)
            >> Terminal::out("OUTPUT");
    }

    void
    HRLE::reset()
    {
    }

    void
    HRLE::initialize(InputDataBundle &input)
    {
        this->nb_channels = input["NB_CHANNELS"].last().as<int>();
        this->length = input["LENGTH"].last().as<int>();

        this->pslength = this->length / 2 + 1;
    }

    bool
    HRLE::operator () (size_t count, InputDataBundle &input, OutputDataBundle &output)
    {
        ////const auto &operation_flag = input["OPERATION_FLAG"].last().as<bool>();

#ifdef DEBUG
        ptime start_time;
        ptime end_time;
        start_time = microsec_clock::local_time();
#endif

        if(count == 0){ this->initialize(input); }

        auto out = data::create<Matrix<std::complex<float> > >(this->pslength, this->nb_channels * this->nb_channels);
        auto &eyes = out.cast<Matrix<std::complex<float> > >();
        //for(size_t k = 0; k < this->pslength; k++) {
        //    for(size_t i = 0; i < this->nb_channels; i++) {
        //        for(size_t j = 0; j < this->nb_channels; j++) {
        //            if(i == j){
        //                eyes(k, i * this->nb_channels + j) = std::complex<float>(1.0f, 0.0f);
        //            }
        //            else{
        //                eyes(k, i * this->nb_channels + j) = std::complex<float>(0.0f, 0.0f);
        //            }
        //        }
        //    }
        //}
        eyes.setZero();
        for(size_t k = 0; k < this->pslength; k++) {
            for(size_t i = 0; this->nb_channels; i++) {
                eyes(k, i * this->nb_channels + i) = std::complex<float>(1.0f, 0.0f);
            }
        }

        output["OUTPUT"].push(out);

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

        return true;
    }
}
}

/* end of file */
