/*
 * SolenoidFactory.cpp
 *
 *  Created on: January 6, 2020
 *      Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
 */

#include "output/items/detail/Solenoid.h"
#include "SolenoidFactory.h"

namespace flippR_driver
{
namespace output
{

SolenoidFactory::SolenoidFactory(nlohmann::json &object, std::shared_ptr<DriverBoardPinController> pin_controller) :
  ItemFactory{object, pin_controller}
{
  if (object.find(config_path::deactivation_time) != object.end())
  {
    this->deactivation_time = object.at(config_path::deactivation_time).get<uint8_t>();
  }
}

std::map<std::string, std::shared_ptr<items::Solenoid>> SolenoidFactory::getItemMap()
{
  auto solenoids = this->object.at(config_path::solenoid_path);
  std::map<std::string, std::shared_ptr<items::Solenoid>> solenoid_map;

  for (auto solenoid : solenoids)
  {
    auto name = solenoid.at(config_path::item_name).get<std::string>();
    auto address = solenoid.at(config_path::item_address).get<uint8_t >();
    std::chrono::milliseconds deactivation_time_chrono{this->deactivation_time};

    if (solenoid.find(config_path::deactivation_time) != solenoid.end())
    {
      deactivation_time_chrono = std::chrono::milliseconds{solenoid.at(config_path::deactivation_time).get<uint8_t>()};
    }

    auto solenoid_item = std::make_shared<items::detail::Solenoid>(std::static_pointer_cast<DriverBoardPinController>(this->pin_controller), address, name, deactivation_time_chrono);
    solenoid_map.emplace(name, solenoid_item);
  }
  return solenoid_map;
}

}
}
