/*
 * SoundFactory.cpp
 *
 *  Created on: December 28, 2019
 *      Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
 */
#include <output/SoundBoardPinController.h>
#include "SoundFactory.h"
namespace  flippR_driver
{
namespace output
{

SoundFactory::SoundFactory(nlohmann::json &object, std::shared_ptr<SoundBoardPinController> pin_controller) :
  Factory{object, pin_controller},
  deactivation_time{0}
{
  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::Item>> SoundFactory::getItemMap()
{
  auto sounds = this->object.at(config_path::sound_path);
  std::map<std::string, std::shared_ptr<items::Item>> sound_map;
  for (auto sound : sounds)
  {
    auto name = sound.at(config_path::item_name).get<std::string>();
    auto address = sound.at(config_path::item_address).get<uint8_t >();
    auto extender = sound.at(config_path::item_extender).get<std::string>();
    auto pin_base = this->get_extender_pin_base(extender);
    auto id = sound.at(config_path::item_identifier).get<uint>();

    std::chrono::milliseconds deactivation_time_chrono{this->deactivation_time};
    if (object.find(config_path::deactivation_time) != object.end())
    {
      deactivation_time_chrono = std::chrono::milliseconds{object.at(config_path::deactivation_time).get<uint8_t>()};
    }
    auto sound_item = std::make_shared<items::detail::Sound>(std::static_pointer_cast<SoundBoardPinController>(this->pin_controller), address, pin_base, name, deactivation_time_chrono, id);

    sound_map.emplace(name, sound_item);
  }
  return sound_map;
}

}
}
