#include <bsp_q7s/callbacks/q7sGpioCallbacks.h>
#include <bsp_q7s/core/XiphosWdtHandler.h>
#include <bsp_q7s/objectFactory.h>
#include <devices/gpioIds.h>
#include <fsfw/storagemanager/LocalPool.h>
#include <fsfw/storagemanager/PoolManager.h>
#include <mission/power/gsDefs.h>
#include <mission/system/EiveSystem.h>

#include "OBSWConfig.h"
#include "bsp_q7s/core/CoreController.h"
#include "busConf.h"
#include "devConf.h"
#include "devices/addresses.h"
#include "eive/objects.h"
#include "fsfw_hal/linux/gpio/LinuxLibgpioIF.h"
#include "linux/ObjectFactory.h"
#include "linux/callbacks/gpioCallbacks.h"
#include "mission/genericFactory.h"
#include "mission/system/systemTree.h"
#include "mission/tmtc/tmFilters.h"

void ObjectFactory::produce(void* args) {
  ObjectFactory::setStatics();
  HealthTableIF* healthTable = nullptr;
  PusTmFunnel* pusFunnel = nullptr;
  CfdpTmFunnel* cfdpFunnel = nullptr;
  StorageManagerIF* ipcStore = nullptr;
  StorageManagerIF* tmStore = nullptr;

  bool enableHkSets = false;
#if OBSW_ENABLE_PERIODIC_HK == 1
  enableHkSets = true;
#endif

  PersistentTmStores stores;
  readFirmwareVersion();
  new XiphosWdtHandler(objects::XIPHOS_WDT);
  ObjectFactory::produceGenericObjects(&healthTable, &pusFunnel, &cfdpFunnel,
                                       *SdCardManager::instance(), &ipcStore, &tmStore, stores, 200,
                                       true, true);

  LinuxLibgpioIF* gpioComIF = nullptr;
  SerialComIF* uartComIF = nullptr;
  SpiComIF* spiMainComIF = nullptr;
  I2cComIF* i2cComIF = nullptr;
  PowerSwitchIF* pwrSwitcher = nullptr;
  createCommunicationInterfaces(&gpioComIF, &uartComIF, &spiMainComIF, &i2cComIF);
  /* Adding gpios for chip select decoding to the gpioComIf */
  q7s::gpioCallbacks::initSpiCsDecoder(gpioComIF);
  gpioCallbacks::disableAllDecoder(gpioComIF);
  createPlI2cResetGpio(gpioComIF);

  new CoreController(objects::CORE_CONTROLLER, enableHkSets);
  createPcduComponents(gpioComIF, &pwrSwitcher, enableHkSets);
  satsystem::EIVE_SYSTEM.setI2cRecoveryParams(pwrSwitcher);

  auto* stackHandler = new Stack5VHandler(*pwrSwitcher);

#if OBSW_ADD_RAD_SENSORS == 1
  createRadSensorComponent(gpioComIF, *stackHandler);
#endif
#if OBSW_ADD_SUN_SENSORS == 1
  createSunSensorComponents(gpioComIF, spiMainComIF, *pwrSwitcher, q7s::SPI_DEFAULT_DEV, true);
#endif

#if OBSW_ADD_ACS_BOARD == 1
  createAcsBoardComponents(*spiMainComIF, gpioComIF, uartComIF, *pwrSwitcher, true,
                           adis1650x::Type::ADIS16505);
#endif
  HeaterHandler* heaterHandler;
  createHeaterComponents(gpioComIF, pwrSwitcher, healthTable, heaterHandler);
#if OBSW_ADD_TMP_DEVICES == 1
  std::vector<std::pair<object_id_t, address_t>> tmpDevsToAdd = {{
      {objects::TMP1075_HANDLER_TCS_0, addresses::TMP1075_TCS_0},
      {objects::TMP1075_HANDLER_TCS_1, addresses::TMP1075_TCS_1},
      {objects::TMP1075_HANDLER_PLPCDU_0, addresses::TMP1075_PLPCDU_0},
      // damaged
      // {objects::TMP1075_HANDLER_PLPCDU_1, addresses::TMP1075_PLPCDU_1},
      {objects::TMP1075_HANDLER_IF_BOARD, addresses::TMP1075_IF_BOARD},
  }};

  createTmpComponents(tmpDevsToAdd);
#endif
  createSolarArrayDeploymentComponents(*pwrSwitcher, *gpioComIF);

  const char* battAndImtqI2cDev = q7s::I2C_PL_EIVE;
  if (core::FW_VERSION_MAJOR >= 4) {
    battAndImtqI2cDev = q7s::I2C_PS_EIVE;
  }
#if OBSW_ADD_MGT == 1
  createImtqComponents(pwrSwitcher, enableHkSets, battAndImtqI2cDev);
#endif
  createReactionWheelComponents(gpioComIF, pwrSwitcher);

#if OBSW_ADD_BPX_BATTERY_HANDLER == 1
  createBpxBatteryComponent(enableHkSets, battAndImtqI2cDev);
#endif
  createPowerController(true, enableHkSets);

#if OBSW_ADD_PL_PCDU == 1
  createPlPcduComponents(gpioComIF, spiMainComIF, pwrSwitcher, *stackHandler);
#endif
#if OBSW_ADD_SYRLINKS == 1
  createSyrlinksComponents(pwrSwitcher);
#endif /* OBSW_ADD_SYRLINKS == 1 */

  createRtdComponents(q7s::SPI_DEFAULT_DEV, gpioComIF, pwrSwitcher, spiMainComIF);
  createPayloadComponents(gpioComIF, *pwrSwitcher);

#if OBSW_ADD_STAR_TRACKER == 1
  createStrComponents(pwrSwitcher, *SdCardManager::instance());
#endif /* OBSW_ADD_STAR_TRACKER == 1 */

#if OBSW_ADD_CCSDS_IP_CORES == 1
  CcsdsIpCoreHandler* ipCoreHandler = nullptr;
  CcsdsComponentArgs ccsdsArgs(*gpioComIF, *ipcStore, *tmStore, stores, *pusFunnel, *cfdpFunnel,
                               &ipCoreHandler, 0, 0);
  createCcsdsIpComponentsWrapper(ccsdsArgs);
#endif /* OBSW_ADD_CCSDS_IP_CORES == 1 */

#if OBSW_ADD_SCEX_DEVICE == 1
  createScexComponents(q7s::UART_SCEX_DEV, pwrSwitcher, *SdCardManager::instance(), false,
                       power::Switches::PDU1_CH5_SOLAR_CELL_EXP_5V);
#endif
  /* Test Task */
#if OBSW_ADD_TEST_CODE == 1
  createTestComponents(gpioComIF);
#endif /* OBSW_ADD_TEST_CODE == 1 */

  createMiscComponents();
  createThermalController(*heaterHandler, false);
  createAcsController(true, enableHkSets, *SdCardManager::instance());
  satsystem::init(false);
}