################################################################################
# Set minimum required version of cmake, project name and compile options
################################################################################
cmake_minimum_required(VERSION 3.5)
project(turtlebot3_gazebo)

if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

if(MSVC)
  add_compile_definitions(_USE_MATH_DEFINES)
endif()

################################################################################
# Find ament packages and libraries for ament and system dependencies
# NOTE: Migrating from Gazebo Classic to Gazebo Sim (gz-8 / Harmonic)
################################################################################
find_package(ament_cmake REQUIRED)

# ROS 2 core packages
find_package(geometry_msgs REQUIRED)
find_package(nav_msgs REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(tf2 REQUIRED)

# Gazebo Sim (Harmonic) libraries
find_package(gz-sim8 REQUIRED)
find_package(gz-msgs10 REQUIRED)
find_package(gz-transport13 REQUIRED)
find_package(gz-physics7 REQUIRED)
find_package(gz-math7 REQUIRED)
find_package(gz-common5 REQUIRED)
find_package(gz-plugin2 REQUIRED)
find_package(sdformat14 REQUIRED)

# Optional: ros_gz bridge packages (for launch integration)
find_package(ros_gz_bridge QUIET)
find_package(ros_gz_sim QUIET)

################################################################################
# Build
################################################################################

include_directories(
  include
  ${gz-sim8_INCLUDE_DIRS}
  ${gz-msgs10_INCLUDE_DIRS}
  ${gz-transport13_INCLUDE_DIRS}
  ${gz-physics7_INCLUDE_DIRS}
  ${gz-math7_INCLUDE_DIRS}
  ${gz-common5_INCLUDE_DIRS}
  ${gz-plugin2_INCLUDE_DIRS}
  ${sdformat14_INCLUDE_DIRS}
)

set(dependencies
  "geometry_msgs"
  "nav_msgs"
  "rclcpp"
  "sensor_msgs"
  "tf2"
)

# --- Executable: turtlebot3_drive (ROS 2 navigation node, no gazebo deps) ---
set(EXEC_NAME "turtlebot3_drive")
add_executable(${EXEC_NAME} src/turtlebot3_drive.cpp)
ament_target_dependencies(${EXEC_NAME} ${dependencies})

# --- Shared Libraries: Custom Gazebo Sim System Plugins ---
# NOTE: These plugins originally used the Gazebo Classic API (gazebo::ModelPlugin)
#       with headers like <gazebo/gazebo.hh>. They must be rewritten to use the
#       gz-sim System API (ISystemPreUpdate, EntityComponentManager, etc.)
#       before they can compile. Default OFF until porting is done.
option(BUILD_GAZEBO_PLUGINS "Build custom Gazebo Sim system plugins" OFF)

if(BUILD_GAZEBO_PLUGINS)
  set(GZ_PLUGIN_LIBRARIES
    gz-sim8::gz-sim8
    gz-msgs10::gz-msgs10
    gz-transport13::gz-transport13
    gz-physics7::gz-physics7
    gz-math7::gz-math7
    gz-common5::gz-common5
    gz-plugin2::gz-plugin2
    sdformat14::sdformat14
  )

  add_library(traffic_light_plugin SHARED src/traffic_light_plugin.cpp)
  target_link_libraries(traffic_light_plugin ${GZ_PLUGIN_LIBRARIES})

  add_library(traffic_bar_plugin SHARED src/traffic_bar_plugin.cpp)
  target_link_libraries(traffic_bar_plugin ${GZ_PLUGIN_LIBRARIES})

  add_library(obstacle1 SHARED src/obstacle1.cpp)
  target_link_libraries(obstacle1 ${GZ_PLUGIN_LIBRARIES})

  add_library(obstacle2 SHARED src/obstacle2.cpp)
  target_link_libraries(obstacle2 ${GZ_PLUGIN_LIBRARIES})

  add_library(obstacles SHARED src/obstacles.cpp)
  target_link_libraries(obstacles ${GZ_PLUGIN_LIBRARIES})
endif()

################################################################################
# Install
################################################################################
install(TARGETS ${EXEC_NAME}
  DESTINATION lib/${PROJECT_NAME}
)

install(DIRECTORY launch config models rviz urdf worlds scripts gui
  DESTINATION share/${PROJECT_NAME}/
)

# Install Python scripts with correct permissions
install(PROGRAMS scripts/scan_frame_fix.py scripts/camera_topic_remap.py
  DESTINATION lib/${PROJECT_NAME}
)

install(DIRECTORY include/
  DESTINATION include/
)

################################################################################
# Export & Macro for ament package
################################################################################
ament_export_include_directories(include)
ament_export_dependencies(geometry_msgs)
ament_export_dependencies(nav_msgs)
ament_export_dependencies(rclcpp)
ament_export_dependencies(sensor_msgs)
ament_export_dependencies(tf2)
ament_package()
