//
// Created by rhetenor on 04.10.18.
//

#include <output/factories/LampFactory.h>
#include <output/factories/SoundFactory.h>
#include <output/factories/FlipperFactory.h>
#include <output/factories/SolenoidFactory.h>
#include <output/factories/DisplayFactory.h>
#include "OutputDriverFactory.h"

#include "utility/LoggerFactory.h"

#include "output/detail/DisplayController.h"
#include "output/detail/SoundBoardPinController.h"
#include "output/detail/DisplayBoardPinController.h"
#include "output/detail/DriverBoardPinController.h"

#include "output/items/Flipper.h"

namespace flippR_driver
{
namespace output
{
namespace OutputDriverFactory
{

using namespace nlohmann;

std::shared_ptr<OutputDriver> get_OutputDriver(const std::string & solenoid_config_path,
                                               const std::string & lamp_config_path,
                                               const std::string & sound_config_path,
                                               const std::string & display_config_path)
{
  utility::LoggerFactory::CreateOutputLogger();

  auto output_pin_mutex = std::make_shared<std::mutex>();
  std::shared_ptr<DriverBoardPinController> driver_board_pin_controller(new detail::DriverBoardPinController(output_pin_mutex));

  auto flippers = get_items<FlipperFactory, detail::DriverBoardPinController, items::Flipper>(solenoid_config_path, driver_board_pin_controller);
  auto solenoids = get_items<SolenoidFactory, detail::DriverBoardPinController, items::Solenoid>(solenoid_config_path, driver_board_pin_controller);
  auto lamps = get_items<LampFactory, detail::DriverBoardPinController, items::Lamp>(lamp_config_path, driver_board_pin_controller);

  driver_board_pin_controller->clear();

  auto sound_board_pin_controller = std::make_shared<detail::SoundBoardPinController>(output_pin_mutex);
  auto sounds = get_items<SoundFactory, detail::SoundBoardPinController, items::Sound>(sound_config_path, sound_board_pin_controller);

  auto display_board_pin_controller = std::make_shared<detail::DisplayBoardPinController>(get_update_frequency(display_config_path));
  auto displays = get_items<DisplayFactory, detail::DisplayBoardPinController, items::Display>(display_config_path, display_board_pin_controller);

  auto display_controller = std::make_unique<detail::DisplayController>(displays, display_board_pin_controller);

  return std::make_shared<OutputDriver>(std::move(display_controller), solenoids, lamps, sounds, flippers, displays);
}

std::chrono::microseconds get_update_frequency(const std::string & display_config_path)
{
  nlohmann::json display_config;
  std::ifstream(display_config_path) >> display_config;
  return std::chrono::microseconds{display_config.at(config_path::display_update_frequency).get<uint>()};
}

template<typename FactoryType, typename ControllerType, typename ItemType>
std::map<std::string, std::shared_ptr<ItemType>> get_items(const std::string &config_path, std::shared_ptr<PinController> pin_controller)
{
  std::ifstream config_stream{config_path};
  nlohmann::json config_json;
  config_stream >> config_json;

  std::map<std::string, std::shared_ptr<ItemType>> map;
  try{
    FactoryType factory{config_json, std::static_pointer_cast<ControllerType>(pin_controller)};
    map = factory.getItemMap();
  }
  catch(json::exception & e)
  {
    CLOG(ERROR, OUTPUT_LOGGER) << "File " << config_path << " seems to be corrupted: " << e.what();
    exit(EXIT_FAILURE);
  }

  return map;
}

}
}
}