//
// 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 std::string & name) :
  address{address},
  name{name},
  content{"12345678"}
{
  CLOG(DEBUG, OUTPUT_LOGGER) << "Created display " << name << " with address " << int{address} << ".";
}

std::string Display::get_name() const
{
  return this->name;
}

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 filled_display;
    return filled_display.insert(0, length, '9');
  }

  score_string.insert(0, length - score_length, ':');
  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;
}

}
}
}
}
