/* Blinker
   Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org>
   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
   a blinkenarea.org project */

#include <iostream>
#include <stdlib.h>
#include <string>
#include <string.h>

#include <BlinkenLib/BlinkenFrame.h>

#include "Directory.h"
#include "File.h"
#include "InStreamFile.h"
#include "Mgrs.h"
#include "Module.h"
#include "Printer.h"
#include "StreamRecv.h"

namespace Blinker {

/**
 * @brief constructor
 * @param[in] name module name
 * @param[in] mgrs managers
 * @param[in] dirBase base directory
 */
Printer::Printer(const std::string &name, Mgrs &mgrs,
                 const Directory &dirBase):
  Module(name, mgrs, dirBase),
  m_fileInStream(dirBase.getFile("instream"), mgrs.m_streamMgr)
{
  // set up
  m_fileInStream.setStreamRecv(this);
}

/// virtual destructor
Printer::~Printer()
{
  // clean up
  m_fileInStream.setStreamRecv(NULL);
}

/// check for update of configuration
void Printer::updateConfig()
{
  // input stream name file was modified -> re-get input stream
  if (m_fileInStream.checkModified())
    m_fileInStream.update();
}

/**
 * @brief set current frame
 * @param[in] stream stream name
 * @param[in] pFrame current frame (NULL for none)
 */
void Printer::setFrame(const std::string &stream, stBlinkenFrame *pFrame)
{
  if (pFrame) {
    char *str = BlinkenFrameToString(pFrame);
    for (int i = strlen(str) - 2; i >= 0 && str[i] != '\n'; --i)
      str[i] = 0; // remove last line (delay)
    std::cout << "frame" << std::endl << str;
    free(str);
  } else {
    std::cout << "no frame" << std::endl;
  }
  (void)stream; // unused
}

} // namespace Blinker

