/*
 * ItemFactory.cpp
 *
 *  Created on: December 28, 2019
 *      Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
 */

#include "ItemFactory.h"

namespace flippR_driver
{
namespace output
{

ItemFactory::ItemFactory(nlohmann::json &object, std::shared_ptr<PinController> pin_controller) :
  pin_controller{pin_controller}
{
  this->object = object;
  if (object.find(config_path::port_extenders) != object.end())
  {
    this->port_extenders = object.at(config_path::port_extenders);
    this->initialize_port_extenders();
  }
}

void ItemFactory::initialize_port_extender(nlohmann::json &extender)
{
  auto i2c_address = extender.at("i2c_address").get<uint8_t>();
  auto pin_base = extender.at("pin_base").get<uint8_t>();
  this->pin_controller->initialize_port_expander(i2c_address, pin_base);
}

void ItemFactory::initialize_port_extenders()
{
  for (auto extender : this->port_extenders)
  {
    this->initialize_port_extender(extender);
  }
}

uint8_t ItemFactory::get_extender_pin_base(std::string &name)
{
  for (auto extender : port_extenders)
  {
    if (extender.at("name").get<std::string>() == name)
    {
      return extender.at("pin_base").get<uint8_t>();
    }
  }
  return 0;
}

}
}
