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

#include <output/factories/LampFactory.h>
#include <output/factories/SoundFactory.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/detail/EightDigitDisplay.h"
#include "output/items/detail/SevenDigitDisplay.h"
#include "output/items/Flipper.h"

namespace flippR_driver
{
namespace output
{
namespace OutputDriverFactory
{

using namespace nlohmann;
namespace config {
namespace
{
json solenoids;
json lamps;
json sounds;
json displays;
}
}
std::shared_ptr<OutputDriver> get_OutputDriver(std::istream& solenoid_config,
                                               std::istream& lamp_config,
                                               std::istream& sound_config,
                                               std::istream& display_config)
{
  utility::LoggerFactory::CreateOutputLogger();

  solenoid_config >> config::solenoids;
  lamp_config >> config::lamps;
  sound_config >> config::sounds;
  display_config >> config::displays;

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

/*  auto solenoids = create_solenoids(driver_board_pin_controller);
  auto flippers = create_flippers(driver_board_pin_controller);*/

  LampFactory lamp_factory(config::lamps, driver_board_pin_controller);
  try
  {
    auto lamps = lamp_factory.getItemMap();
  }
  catch(json::exception &e)
  {
    CLOG(INFO, OUTPUT_LOGGER) << "File " << "sound_config.json" << " seems to be corrupted: " <<  ": " << e.what();
    exit(EXIT_FAILURE);
  }

  auto sound_board_pin_controller = create_SoundBoardPinController();
  SoundFactory sound_factory{config::sounds, sound_board_pin_controller};
  auto sounds = sound_factory.getItemMap();

  /*std::unique_ptr<DisplayBoardPinController> display_board_pin_controller(new detail::DisplayBoardPinController(parse_pins_display_board(display_config)));
  auto displays = create_displays();
  std::unique_ptr<DisplayController> display_controller(new detail::DisplayController(displays, std::move(display_board_pin_controller)));
  auto display_map = map_displays(displays);

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

std::map<std::string, std::shared_ptr<items::Solenoid>> create_solenoids(std::shared_ptr<DriverBoardPinController> &pin_controller)
{
  json port_extenders = get_element("port_extenders", config::solenoids, "");

  initialize_port_extenders(port_extenders, pin_controller.get(), "solenoid_config.json");

  json solenoids_json = get_element("solenoids", config::solenoids, "solenoid_config.json");
  std::chrono::milliseconds deactivation_time{ get_value<int>("deactivation_time_milliseconds", config::solenoids, "solenoid_config.json") };

  std::map<std::string, std::shared_ptr<items::Solenoid>> solenoids;
  for(auto &solenoid_json : solenoids_json)
  {
    auto solenoid = create_solenoid(solenoid_json, port_extenders, pin_controller, deactivation_time);
    solenoids.emplace(solenoid->get_name(), solenoid);
  }
  return solenoids;
}

std::shared_ptr<items::detail::Solenoid> create_solenoid(nlohmann::json &solenoid_json, nlohmann::json &port_extenders, std::shared_ptr<DriverBoardPinController> &pin_controller, std::chrono::milliseconds deactivation_time)
{
  std::string config_file_name = "config::solenoid.json";

  uint8_t pin_base = get_pin_base(solenoid_json, port_extenders, config_file_name);

  auto name = get_value<std::string>("name", solenoid_json, config_file_name);
  auto address = get_value<uint8_t>("address", solenoid_json, config_file_name);

    if(solenoid_json.find("deactivation_time_milliseconds") != solenoid_json.end())
    {
      deactivation_time = std::chrono::milliseconds(get_value<uint8_t>("deactivation_time_milliseconds", solenoid_json, config_file_name));
    }

    return std::make_shared<items::detail::Solenoid>(pin_controller, address, pin_base, name, deactivation_time);
}

std::map<std::string, std::shared_ptr<items::Flipper>> create_flippers(std::shared_ptr<DriverBoardPinController> &pin_controller)
{
  json port_extenders = get_element("port_extenders", config::solenoids, "solenoid_config.json");

  json flippers_json = get_element("flippers", config::solenoids, "solenoid_config.json");
  std::chrono::milliseconds deactivation_time{ get_value<int>("deactivation_time_milliseconds", config::solenoids, "solenoid_config.json") };

  std::map<std::string, std::shared_ptr<items::Flipper>> flippers;
  for(auto &flipper_json : flippers_json)
  {
    auto flipper = create_flipper(flipper_json, port_extenders, pin_controller);
    flippers.emplace(flipper->get_name(), flipper);
  }
  return flippers;
}

std::shared_ptr<items::detail::Flipper> create_flipper(nlohmann::json &flipper_json, json &port_extenders, std::shared_ptr<DriverBoardPinController> &pin_controller)
{
  std::string config_file_name = "solenoid_config.json";

  uint8_t pin_base = get_pin_base(flipper_json, port_extenders, config_file_name);
  auto address = get_value<uint8_t>("address", flipper_json, config_file_name);
  auto name = get_value<std::string>("name", flipper_json, config_file_name);

  return std::make_shared<items::detail::Flipper>(pin_controller, address, pin_base, name);
}

std::map<std::string, std::shared_ptr<items::Sound>> create_sounds(std::istream &sound_config, std::shared_ptr<SoundBoardPinController> &pin_controller)
{
  json port_extenders = get_element("port_extenders", config::sounds, "sound_config.json");
  initialize_port_extenders(port_extenders, pin_controller.get(), "sound_config.json");
  std::chrono::milliseconds deactivation_time{ get_value<int>("deactivation_time_milliseconds", config::sounds, "solenoid_config.json") };
  json sounds_json = get_element("sounds", config::sounds, "sound_config.json");

  std::map<std::string, std::shared_ptr<items::Sound>> sounds;
  for(auto &sound_json : sounds_json)
  {
    auto sound = create_sound(sound_json, port_extenders, pin_controller, deactivation_time);
    sounds.emplace(sound->get_name(), sound);
  }
  return sounds;
}

std::shared_ptr<items::detail::Sound> create_sound(json &sound_json, json &port_extenders, std::shared_ptr<SoundBoardPinController> &pin_controller, std::chrono::milliseconds &deactivation_time)
{
  std::string config_file_name = "sound_config.json";

  uint8_t pin_base = get_pin_base(sound_json, port_extenders, config_file_name);
  auto address = get_value<uint8_t>("address", sound_json, config_file_name);
  auto name = get_value<std::string>("name", sound_json, config_file_name);
  auto id = get_value<uint>("id", sound_json, config_file_name);

  return std::make_shared<items::detail::Sound>(pin_controller, address, pin_base, name, deactivation_time, id);
}

uint8_t get_sound_fire_address()
{
  json port_extenders = get_element("port_extenders", config::sounds, "sound_config.json");

  json fire_pin = get_element("fire_pin", config::sounds, "sound_config.json");
  auto pin_base = get_pin_base(fire_pin, port_extenders, "sound_config.json");
  auto address = get_value<uint8_t>("address", fire_pin, "sound_config.json");

  return address;
}

std::map<std::string, uint8_t> parse_pins_display_board(std::istream &display_config)
{
  std::string config_file = "display_config.json";

  json display_board_config = get_element("display_board", config::displays, config_file);

  std::map<std::string, uint8_t> pins_display;

  pins_display["run"] = get_value<uint8_t>("run", display_board_config, config_file);

  json display_select = display_board_config.at("display_select");
  for(int i = 1; i < 6; i++)
  {
    pins_display["display_select" + std::to_string(i)] = get_value<uint8_t>(std::to_string(i), display_select, config_file);
  }

  json segment_select = display_board_config.at("segment_select");
  pins_display["segment_select_A"] = get_value<uint8_t>("A", segment_select, config_file);
  pins_display["segment_select_B"] = get_value<uint8_t>("B", segment_select, config_file);
  pins_display["segment_select_C"] = get_value<uint8_t>("C", segment_select, config_file);

  json digit_select = display_board_config.at("digit_select");
  pins_display["digit_select_A"] = get_value<uint8_t>("A", digit_select, config_file);
  pins_display["digit_select_B"] = get_value<uint8_t>("B", digit_select, config_file);
  pins_display["digit_select_C"] = get_value<uint8_t>("C", digit_select, config_file);
  pins_display["digit_select_D"] = get_value<uint8_t>("D", digit_select, config_file);;

  return pins_display;
}

std::vector<std::shared_ptr<items::OutputDisplay>> create_displays(std::istream &display_config)
{
  json displays_json = get_element("displays", config::displays, "display_config.json");

  std::vector<std::shared_ptr<items::OutputDisplay>> displays;
  for(json &display_json : displays_json)
  {
    auto display = create_display(display_json);
    displays.push_back(display);
  }

  return displays;
}

std::shared_ptr<items::OutputDisplay> create_display(json & display_json)
{
/*  std::string config_file = "display_config.json";
  auto id = get_value<uint8_t>("id", display_json, config_file);
  auto address = get_value<uint8_t>("address", display_json, config_file);
  auto digits = get_value<uint8_t>("digits", display_json, config_file);
  if(digits == 8)
    return std::make_shared<items::detail::EightDigitDisplay>(address, id);

  else if(digits == 7)
    return std::make_shared<items::detail::SevenDigitDisplay>(address, id);

  else
    throw new std::logic_error("Display digits can either be 7 or 8");*/
}

std::map<uint8_t, std::shared_ptr<items::Display>> map_displays(const std::vector<std::shared_ptr<items::OutputDisplay>> &displays)
{
  std::map<uint8_t, std::shared_ptr<items::Display>> display_map;

  for(auto &display : displays)
  {
    display_map.emplace(display->get_address(), display);
  }

  return display_map;
}

void initialize_port_extenders(json &port_extenders, PinController * pin_controller, const std::string & file_name)
{
  for (auto & extender_json : port_extenders)
  {
    auto  i2c_address = get_value<uint8_t>("i2c_address", extender_json, file_name);
    auto  pin_base = get_value<uint8_t>("pin_base", extender_json, file_name);
    pin_controller->initialize_port_expander(i2c_address, pin_base);
  }
}

uint8_t get_pin_base(json & object, json & port_extenders, const std::string & config_file_name)
{
  if(object.find("extender") != object.end())
  {
    auto extender_name = get_value<std::string>("extender", object, config_file_name);
    for (auto & extender : port_extenders)
    {
      auto actual_extender = get_value<std::string>("name", extender, config_file_name);
      if (actual_extender == extender_name)
      {
        try
        {
          return extender.at("pin_base").get<uint8_t >();
        }
        catch(json::exception &e)
        {
          CLOG(INFO, OUTPUT_LOGGER) << "pin_base not set for " << extender_name << " in " << config_file_name;
          return 0;
        }
      }
    }
  }
  CLOG(INFO, OUTPUT_LOGGER) << "Extender not set for " << object << " in " << config_file_name;
  return 0;
}

json get_element(const std::string & name, json & object, const std::string & file_name)
{
  json sub_object;

  try
  {
    sub_object = object.at(name);
  }
  catch(json::exception &e)
  {
    CLOG(ERROR, OUTPUT_LOGGER) << "File " << file_name << " seems to be corrupted at " << name << ": " << e.what();
    exit(EXIT_FAILURE);
  }

  return sub_object;
}

template<typename type>
type get_value(const std::string & name, json & object, const std::string & file_name)
{
  type element;
  try
  {
    element = object.at(name).get<type>();
  }
  catch(json::exception &e)
  {
    /*CLOG(ERROR, OUTPUT_LOGGER)*/ std::cerr << "File " << file_name << " seems to be corrupted at " << name << ": " << e.what();
    exit(EXIT_FAILURE);
  }
  return element;
}

std::shared_ptr<SoundBoardPinController> create_SoundBoardPinController()
{

  return std::shared_ptr<SoundBoardPinController>();
}

}
}
}