#include "GenericFactory.h"

#include <fsfw/cfdp/CfdpDistributor.h>
#include <fsfw/cfdp/handler/CfdpHandler.h>
#include <fsfw/cfdp/handler/RemoteConfigTableIF.h>
#include <fsfw/events/EventManager.h>
#include <fsfw/health/HealthTable.h>
#include <fsfw/internalerror/InternalErrorReporter.h>
#include <fsfw/ipc/QueueFactory.h>
#include <fsfw/pus/CService200ModeCommanding.h>
#include <fsfw/pus/CService201HealthCommanding.h>
#include <fsfw/pus/Service17Test.h>
#include <fsfw/pus/Service1TelecommandVerification.h>
#include <fsfw/pus/Service20ParameterManagement.h>
#include <fsfw/pus/Service2DeviceAccess.h>
#include <fsfw/pus/Service3Housekeeping.h>
#include <fsfw/pus/Service5EventReporting.h>
#include <fsfw/pus/Service8FunctionManagement.h>
#include <fsfw/pus/Service9TimeManagement.h>
#include <fsfw/storagemanager/PoolManager.h>
#include <fsfw/tcdistribution/CcsdsDistributor.h>
#include <fsfw/tcdistribution/PusDistributor.h>
#include <fsfw/timemanager/CdsShortTimeStamper.h>
#include <fsfw_hal/host/HostFilesystem.h>
#include <mission/tmtc/CfdpTmFunnel.h>
#include <mission/tmtc/PusTmFunnel.h>
#include <mission/tmtc/TmFunnelHandler.h>

#include "OBSWConfig.h"
#include "eive/definitions.h"
#include "fsfw/pus/Service11TelecommandScheduling.h"
#include "mission/cfdp/Config.h"
#include "objects/systemObjectList.h"
#include "tmtc/pusIds.h"

#if OBSW_ADD_TCPIP_SERVERS == 1
#if OBSW_ADD_TMTC_UDP_SERVER == 1
// UDP server includes
#include "fsfw/osal/common/UdpTcPollingTask.h"
#include "fsfw/osal/common/UdpTmTcBridge.h"
#endif
#if OBSW_ADD_TMTC_TCP_SERVER == 1
// TCP server includes
#include "fsfw/osal/common/TcpTmTcBridge.h"
#include "fsfw/osal/common/TcpTmTcServer.h"
#endif
#endif

#if OBSW_ADD_TEST_CODE == 1
#include <test/testtasks/TestTask.h>
#endif

#ifndef OBSW_TM_TO_PTME
#define OBSW_TM_TO_PTME 0
#endif

namespace cfdp {

PacketInfoList<64> PACKET_LIST;
LostSegmentsList<128> LOST_SEGMENTS;
EntityId REMOTE_CFDP_ID(UnsignedByteField<uint16_t>(config::EIVE_GROUND_CFDP_ENTITY_ID));
RemoteEntityCfg GROUND_REMOTE_CFG(REMOTE_CFDP_ID);
OneRemoteConfigProvider REMOTE_CFG_PROVIDER(GROUND_REMOTE_CFG);
HostFilesystem HOST_FS;
EiveUserHandler USER_HANDLER(HOST_FS);
EiveFaultHandler EIVE_FAULT_HANDLER;

}  // namespace cfdp

void ObjectFactory::produceGenericObjects(HealthTableIF** healthTable_, PusTmFunnel** pusFunnel,
                                          CfdpTmFunnel** cfdpFunnel) {
  // Framework objects
  new EventManager(objects::EVENT_MANAGER);
  auto healthTable = new HealthTable(objects::HEALTH_TABLE);
  if (healthTable_ != nullptr) {
    *healthTable_ = healthTable;
  }
  new InternalErrorReporter(objects::INTERNAL_ERROR_REPORTER);
  new VerificationReporter();
  auto* timeStamper = new CdsShortTimeStamper(objects::TIME_STAMPER);
  StorageManagerIF* tcStore;
  StorageManagerIF* tmStore;
  {
    PoolManager::LocalPoolConfig poolCfg = {{250, 16},  {250, 32},   {250, 64},
                                            {150, 128}, {120, 1024}, {120, 2048}};
    tcStore = new PoolManager(objects::TC_STORE, poolCfg);
  }

  {
    PoolManager::LocalPoolConfig poolCfg = {{300, 16},  {350, 32},   {350, 64},
                                            {200, 128}, {150, 1024}, {150, 2048}};
    tmStore = new PoolManager(objects::TM_STORE, poolCfg);
  }

  {
    PoolManager::LocalPoolConfig poolCfg = {{300, 16},  {200, 32}, {150, 64},  {150, 128},
                                            {100, 256}, {50, 512}, {50, 1024}, {10, 2048}};
    new PoolManager(objects::IPC_STORE, poolCfg);
  }

#if OBSW_ADD_TCPIP_SERVERS == 1
#if OBSW_ADD_TMTC_UDP_SERVER == 1
  auto udpBridge = new UdpTmTcBridge(objects::UDP_TMTC_SERVER, objects::CCSDS_PACKET_DISTRIBUTOR);
  new UdpTcPollingTask(objects::UDP_TMTC_POLLING_TASK, objects::UDP_TMTC_SERVER);
  sif::info << "Created UDP server for TMTC commanding with listener port "
            << udpBridge->getUdpPort() << std::endl;
  udpBridge->setMaxNumberOfPacketsStored(150);
#endif
#if OBSW_ADD_TMTC_TCP_SERVER == 1
  auto tcpBridge = new TcpTmTcBridge(objects::TCP_TMTC_SERVER, objects::CCSDS_PACKET_DISTRIBUTOR);
  auto tcpServer = new TcpTmTcServer(objects::TCP_TMTC_POLLING_TASK, objects::TCP_TMTC_SERVER);
  // TCP is stream based. Use packet ID as start marker when parsing for space packets
  tcpServer->setSpacePacketParsingOptions({common::PUS_PACKET_ID, common::CFDP_PACKET_ID});
  sif::info << "Created TCP server for TMTC commanding with listener port "
            << tcpServer->getTcpPort() << std::endl;
  tcpBridge->setMaxNumberOfPacketsStored(150);
#endif /* OBSW_USE_TMTC_TCP_BRIDGE == 0 */
#endif /* OBSW_ADD_TCPIP_BRIDGE == 1 */

  auto* ccsdsDistrib =
      new CcsdsDistributor(config::EIVE_PUS_APID, objects::CCSDS_PACKET_DISTRIBUTOR);
  new PusDistributor(config::EIVE_PUS_APID, objects::PUS_PACKET_DISTRIBUTOR, ccsdsDistrib);

  *cfdpFunnel = new CfdpTmFunnel(objects::CFDP_TM_FUNNEL, config::EIVE_CFDP_APID, *tmStore, 50);
  *pusFunnel = new PusTmFunnel(objects::PUS_TM_FUNNEL, *timeStamper, *tmStore, 80);
#if OBSW_ADD_TCPIP_SERVERS == 1
#if OBSW_ADD_TMTC_UDP_SERVER == 1
  (*cfdpFunnel)->addDestination(*udpBridge, 0);
  (*pusFunnel)->addDestination(*udpBridge, 0);
#endif
#if OBSW_ADD_TMTC_TCP_SERVER == 1
  (*cfdpFunnel)->addDestination(*tcpBridge, 0);
  (*pusFunnel)->addDestination(*tcpBridge, 0);
#endif
#endif
  // Every TM packet goes through this funnel
  new TmFunnelHandler(objects::TM_FUNNEL, **pusFunnel, **cfdpFunnel);

  // PUS service stack
  new Service1TelecommandVerification(objects::PUS_SERVICE_1_VERIFICATION, config::EIVE_PUS_APID,
                                      pus::PUS_SERVICE_1, objects::PUS_TM_FUNNEL, 20);
  new Service2DeviceAccess(objects::PUS_SERVICE_2_DEVICE_ACCESS, config::EIVE_PUS_APID,
                           pus::PUS_SERVICE_2, 3, 10);
  new Service3Housekeeping(objects::PUS_SERVICE_3_HOUSEKEEPING, config::EIVE_PUS_APID,
                           pus::PUS_SERVICE_3);
  new Service5EventReporting(
      PsbParams(objects::PUS_SERVICE_5_EVENT_REPORTING, config::EIVE_PUS_APID, pus::PUS_SERVICE_5),
      15, 45);
  new Service8FunctionManagement(objects::PUS_SERVICE_8_FUNCTION_MGMT, config::EIVE_PUS_APID,
                                 pus::PUS_SERVICE_8, 16, 60);
  new Service9TimeManagement(
      PsbParams(objects::PUS_SERVICE_9_TIME_MGMT, config::EIVE_PUS_APID, pus::PUS_SERVICE_9));

  new Service11TelecommandScheduling<common::OBSW_MAX_SCHEDULED_TCS>(
      PsbParams(objects::PUS_SERVICE_11_TC_SCHEDULER, config::EIVE_PUS_APID, pus::PUS_SERVICE_11),
      ccsdsDistrib);
  new Service17Test(
      PsbParams(objects::PUS_SERVICE_17_TEST, config::EIVE_PUS_APID, pus::PUS_SERVICE_17));
  new Service20ParameterManagement(objects::PUS_SERVICE_20_PARAMETERS, config::EIVE_PUS_APID,
                                   pus::PUS_SERVICE_20);
  new CService200ModeCommanding(objects::PUS_SERVICE_200_MODE_MGMT, config::EIVE_PUS_APID,
                                pus::PUS_SERVICE_200, 8);
  new CService201HealthCommanding(objects::PUS_SERVICE_201_HEALTH, config::EIVE_PUS_APID,
                                  pus::PUS_SERVICE_201);

#if OBSW_ADD_CFDP_COMPONENTS == 1
  using namespace cfdp;
  MessageQueueIF* cfdpMsgQueue = QueueFactory::instance()->createMessageQueue(32);
  CfdpDistribCfg distribCfg(objects::CFDP_DISTRIBUTOR, *tcStore, cfdpMsgQueue);
  new CfdpDistributor(distribCfg);

  auto* msgQueue = QueueFactory::instance()->createMessageQueue(32);
  FsfwHandlerParams params(objects::CFDP_HANDLER, HOST_FS, **cfdpFunnel, *tcStore, *tmStore,
                           *msgQueue);
  cfdp::IndicationCfg indicationCfg;
  UnsignedByteField<uint16_t> apid(config::EIVE_LOCAL_CFDP_ENTITY_ID);
  cfdp::EntityId localId(apid);
  GROUND_REMOTE_CFG.defaultChecksum = cfdp::ChecksumType::CRC_32;
  CfdpHandlerCfg cfdpCfg(localId, indicationCfg, USER_HANDLER, EIVE_FAULT_HANDLER, PACKET_LIST,
                         LOST_SEGMENTS, REMOTE_CFG_PROVIDER);
  auto* cfdpHandler = new CfdpHandler(params, cfdpCfg);
  // All CFDP packets arrive wrapped inside CCSDS space packets
  CcsdsDistributorIF::DestInfo info("CFDP Destination", config::EIVE_CFDP_APID,
                                    cfdpHandler->getRequestQueue(), true);
  ccsdsDistrib->registerApplication(info);
#endif
}