#
# $Id$
# $HeadURL$
# $Author$
#

# Make ${Qt6_Gui} optional
if(QT6_GUI_AVAILABLE AND TARGET Qt6::Gui)
  set(Qt6_Gui ${Qt6_Gui})
else()
  set(Qt6_Gui "")
endif()

# Link the real WebSockets module whenever the websocket code path is
# compiled: QT6_WEBSOCKETS_AVAILABLE (root CMakeLists) and the manual
# Qt6::WebSockets import are set when the real module was found. The old
# QT6_GUI_AVAILABLE condition never holds in this environment (Qt6
# discovery takes the minimal path) and the Qt6_WebSockets variable was
# never defined, so the websocket code compiled without the module on the
# link line and failed only at link time.
if(QT6_WEBSOCKETS_REAL_FOUND OR Qt6WebSockets_FOUND)
  set(Qt6_WebSockets Qt6::WebSockets)
else()
  set(Qt6_WebSockets "")
endif()


INCLUDE(MacroRTIDDSGen)
INCLUDE(MacroCodeGenIdl)

SET(xml_FILE ppm_service.xml)

#
# --- Library svcPpmCommon --------------------------------------------------------------------------------
#
SET(SRCS_common
  ppm_export.h
  ppm_common.cpp
  ppm_common.h
  ppm_db_impl_custom.cpp
  ppm_db_impl_custom.h
  logger.cpp
  logger.h
)

# Generate ppm_enums.idl / ppm.idl
IF(BUILD_RTI)
  CODEGEN_IDL_SVC(SRCS_common ${xml_FILE})
  # Generate *.cpp/*.hpp from idl
  RTIDDSGEN(SRCS_common ppm_enums.idl)
  RTIDDSGEN(SRCS_common ppm.idl)
ENDIF()
# The enums stub copy below would overwrite the rtiddsgen-generated
# <svc>_enums.hpp (RTIDDSGEN on <svc>_enums.idl writes the same build-dir
# path) on every cmake re-configure. Only keep it for BUILD_RTI=OFF.
IF(NOT BUILD_RTI)
# Stub file for compilation (fallback when no IDL exists)
CONFIGURE_FILE(
  ${CMAKE_SOURCE_DIR}/include/ParticipantManagement/ppm_enums.hpp.stub
  ${CMAKE_BINARY_DIR}/Lts/Services/ParticipantManagement/ppm_enums.hpp
  COPYONLY
)

ENDIF()
# Generate ppm_db_structs.cpp/.h
CODEGEN_DATABASE_STRUCTS(SRCS_common ${xml_FILE})
# Generate ppm_db.cpp, ppm_db_impl.cpp/.h
CODEGEN_DATABASE_SQL(SRCS_common ${xml_FILE})

# Generate ppm_dt[s,c]_types.h, ppm_ft[s,c]_types.h
IF(BUILD_RTI)
  CODEGEN_TOPIC_TYPEDEFS(SRCS_common ${xml_FILE})
ENDIF()
# generate ppm_[d|f]tc.h
IF(BUILD_RTI)
  CODEGEN_CLIENT_TOPICS_IFC(SRCS_common ${xml_FILE})
ENDIF()

NGL_ADD_LIBRARY(AAR SV WS HLA svcPpmCommon SHARED  ${SRCS_common})
NGL_TARGET_INCLUDE_DIRECTORIES(svcPpmCommon
  PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/../
    ${CMAKE_CURRENT_BINARY_DIR}/../
)
if(QT6_GUI_AVAILABLE)
  NGL_TARGET_LINK_LIBRARIES(svcPpmCommon
    PRIVATE
      RTI::nddscpp2
      LtsUtilsCore
    PUBLIC
      Qt6::Core
      ${Qt6_Gui}
      $<IF:$<TARGET_EXISTS:ddscore>,ddscore,>
      NGLLogger
      $<IF:$<TARGET_EXISTS:svcPdmCommon>,svcPdmCommon,>
      LtsUtilsCore
      LtsCoordTrans
      ngldb
  )
endif()
NGL_INSTALL_TARGET(svcPpmCommon)

#
# --- Library svcPpmClient --------------------------------------------------------------------------------
#
SET(SRCS_client
  ppm_export.h
  ppm_dtc_impl_custom.cpp
  ppm_dtc_impl_custom.h
)
# generate ppm_[d|f]tc_impl.[cpp|h]
IF(BUILD_RTI)
  CODEGEN_CLIENT_TOPICS_IMPL(SRCS_client ${xml_FILE})
ENDIF()

NGL_ADD_LIBRARY(WS AAR SV HLA svcPpmClient SHARED ${SRCS_client})
NGL_TARGET_INCLUDE_DIRECTORIES(svcPpmClient
  PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/../
    ${CMAKE_CURRENT_BINARY_DIR}/../
)

NGL_TARGET_LINK_LIBRARIES(svcPpmClient
  PRIVATE
    RTI::nddscpp2
    $<IF:$<TARGET_EXISTS:ddsconnector>,ddsconnector,>
  PUBLIC
    svcPpmCommon
    ddsftc
)
NGL_INSTALL_TARGET(svcPpmClient)

#
# --- Library svcPpmServer --------------------------------------------------------------------------------
#
SET(SRCS_server
  ppm_export.h
  convert_db_to_dds.h

)
# generate ppm_[d|f]ts.h, ppm_[d|f]ts_impl.[cpp|h]
IF(BUILD_RTI)
  CODEGEN_SERVER_TOPICS(SRCS_server ${xml_FILE})
ENDIF()

# WS for RecordReplayPlugin
NGL_ADD_LIBRARY(AAR SV WS svcPpmServer SHARED ${SRCS_server})
NGL_TARGET_INCLUDE_DIRECTORIES(svcPpmServer
  PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/../
    ${CMAKE_CURRENT_BINARY_DIR}/../
)

NGL_TARGET_LINK_LIBRARIES(svcPpmServer
  PRIVATE
    RTI::nddscpp2
    $<IF:$<TARGET_EXISTS:ddsconnector>,ddsconnector,>
    ddsfts
  PUBLIC
    svcPpmCommon
    ngldb
    LtsUtilsCore
)
NGL_INSTALL_TARGET(svcPpmServer)

#
# --- Topic Type Plugin --------------------------------------------------------------
#
IF(BUILD_RTI)
  CREATE_TOPIC_TYPE_PLUGIN(${xml_FILE})
ENDIF()

#
# --- RecordReplayPlugin --------------------------------------------------------------
#
CREATE_RECORD_REPLAY_PLUGIN_SHARED(${xml_FILE})

IF (TARGET ppmRecordReplayPlugin)
  TARGET_SOURCES(ppmRecordReplayPlugin
    PRIVATE
      RecordReplay/participant_for_replay.h
      RecordReplay/participant_handler_for_replay.cpp
      RecordReplay/participant_handler_for_replay.h
      RecordReplay/ppm_record_replay_plugin_custom.cpp
      RecordReplay/ppm_record_replay_plugin_custom.h
      ParticipantHandling/participant_donator.h
      ParticipantHandling/participant.cpp
      ParticipantHandling/participant.h
      ParticipantHandling/participant_handler.cpp
      ParticipantHandling/participant_handler.h
      ParticipantHandling/unit_position_calculator.cpp
      ParticipantHandling/unit_position_calculator.h

      settings.cpp
      settings.h

      web_socket_handler.cpp
      web_socket_handler.h
  )
  TARGET_LINK_LIBRARIES(ppmRecordReplayPlugin
    Qt6::Concurrent
    ${Qt6_WebSockets}
    tabRecordReplayPlugin
    svcBldClient
    svcPpmServer
    svcPdmClient
    LtsCoordTrans
    LtsUtilsCoreHeaderOnly # unit_position_calculator.cpp => LtsUtilsCore/qpoint3d.h
    nglsettings
    ngltools
  )
ENDIF()

#
# --- Executable svcParticipantManagement (sPPM) -------------------------------------------------
#
LIST(APPEND SRCS_exe
  BiteHandling/bite_handler.cpp
  BiteHandling/bite_handler.h

  dds_handler.cpp
  dds_handler.h

  EquipmentEventHandling/equipment_event_handler.cpp
  EquipmentEventHandling/equipment_event_handler.h

  exercise_time.cpp
  exercise_time.h
  main.cpp
  main_handler.cpp
  main_handler.h

  ParticipantHandling/firing_unit_validator.cpp
  ParticipantHandling/firing_unit_validator.h
  ParticipantHandling/get_participants.cpp
  ParticipantHandling/get_participants.h
  ParticipantHandling/participant_donator.h
  ParticipantHandling/participant.cpp
  ParticipantHandling/participant.h
  ParticipantHandling/participant_for_exercise.cpp
  ParticipantHandling/participant_for_exercise.h
  ParticipantHandling/participant_handler.cpp
  ParticipantHandling/participant_handler.h
  ParticipantHandling/participant_handler_for_exercise.cpp
  ParticipantHandling/participant_handler_for_exercise.h
  ParticipantHandling/proxy_listener.cpp
  ParticipantHandling/proxy_listener.h
  ParticipantHandling/unit_position_calculator.cpp
  ParticipantHandling/unit_position_calculator.h

  PartyHandling/party.cpp
  PartyHandling/party_donator.h
  PartyHandling/party.h
  PartyHandling/party_handler.cpp
  PartyHandling/party_handler.h

  settings.cpp
  settings.h

  TargetHandling/object_target_information.cpp
  TargetHandling/object_target_information.h
  TargetHandling/handler_target_information.cpp
  TargetHandling/handler_target_information.h

  TemplateHandling/template.cpp
  TemplateHandling/template.h
  TemplateHandling/template_handler.cpp
  TemplateHandling/template_handler.h

  TypeHandling/type_donator.h
  TypeHandling/type_handler.cpp
  TypeHandling/type_handler_exercise.cpp
  TypeHandling/type_handler_exercise.h
  TypeHandling/type_handler_global.cpp
  TypeHandling/type_handler_global.h
  TypeHandling/type_handler.h

  UasHandling/uas.cpp
  UasHandling/uas.h
  UasHandling/uas_handler.cpp
  UasHandling/uas_handler.h

  UasHandling/mission_plan_handler.h
  UasHandling/mission_plan_handler.cpp
  UasHandling/object_mission_plan.cpp
  UasHandling/object_mission_plan.h

  WebSocketInterface/data_manager.cpp
  WebSocketInterface/data_manager.h
  WebSocketInterface/json_keys.h
  WebSocketInterface/websocket_interface_handler.cpp
  WebSocketInterface/websocket_interface_handler.h
  WebSocketInterface/Messages/client_message.cpp
  WebSocketInterface/Messages/client_message.h
  WebSocketInterface/Messages/reply_message.cpp
  WebSocketInterface/Messages/reply_message.h

  web_socket_handler.cpp
  web_socket_handler.h  

  ${xml_FILE}
)

# Generate ppm.sql, ppm.xml
IF(BUILD_RTI)
  CODEGEN_SQL(SRCS_exe ${xml_FILE})
ENDIF()

NGL_ADD_SVC_ICON(SRCS_exe)
NGL_ADD_EXECUTABLE(AAR SV svcParticipantManagement ${SRCS_exe})
NGL_TARGET_LINK_LIBRARIES(svcParticipantManagement
  ppmRecordReplayPlugin
  Qt6::Core
  Qt6::Concurrent
  ${Qt6_WebSockets}
  $<IF:$<TARGET_EXISTS:ddsconnector>,ddsconnector,>
  LtsCommandLineParser
  nglsettings
  nglcoreapplication
  NGLLogger
  ProjectConfig
  svcCoreClient
  svcPdmClient
  svcPpmCommon
  svcPpmServer
  svcPpmClient
  svcTabClient
  svcAesClient
  svcCesServer
  svcCesClient
  svcBldClient
  svcTabServer
  svcExmClient
  ngltools
  LtsCoordTrans
  LtsWebSocketServer
)

NGL_INSTALL_TARGET(svcParticipantManagement)
NGL_INSTALL(AAR SV FILES log4cxx_ppm.props DESTINATION bin)
NGL_ADD_TARGET_LINK(svcParticipantManagement sPPM)

# copy ppm_predefined_data.sql to share
SET(in_file_sql  "ppm_predefined_data.sql")
NGL_INSTALL(AAR SV FILES ppm_predefined_data.sql DESTINATION share/Lts/sql)


# Uncomment if necessary
#ADD_SUBDIRECTORY(UnitTests)
ADD_SUBDIRECTORY(ModuleTests)
