//
// Created by rhetenor on 10.10.18.
//

#include "Display.h"
#include "utility/config.h"

#include <string>
#include <algorithm>

namespace flippR_driver
{
namespace output
{
namespace items
{
namespace detail
{

Display::Display(const uint8_t & address, const uint8_t & id) :
  address(address),
  id(id)
{
  CLOG(INFO, OUTPUT_LOGGER) << "Created display with id " << id << " and address " << address << ".";
}

uint8_t Display::get_id() const
{
  return this->id;
}

void Display::write_score(const unsigned int & score, const unsigned int & length)
{
  auto score_string = std::to_string(score);

  fit_score_string(score_string, length);

  write_content(score_string, length);
}

std::string Display::fit_score_string(std::string & score_string, const unsigned int & length)
{
  auto score_length = score_string.length();

  if (score_length > length)
  {
    CLOG(DEBUG, OUTPUT_LOGGER) << "Score too long for display";
    std::string full_display;
    return full_display.insert(0, length, '9');
  }

  score_string.insert(0, length - score_length, '\0');
  return score_string;
}

void Display::write_content(std::string & content, const unsigned int & length)
{
  if(content.size() > length)
  {
    CLOG(WARNING, OUTPUT_LOGGER) << "Cannot write more than " << length << " digits on " << length << "-Digit Display. Truncating!";
    content = content.substr(0, length);
  }

  this->content = content;
}

std::string Display::get_content() const
{
  return this->content;
}

uint8_t Display::get_address() const
{
  return this->address;
}

}
}
}
}
