# ========================================================
# ROOT CMakeLists.txt
# Đặt ở: Navigations/CMakeLists.txt
# ========================================================
cmake_minimum_required(VERSION 3.0.2)
project(navigations VERSION 1.0.0 LANGUAGES CXX)

# Chuẩn C++
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

# Flags chung
# Warning flags - disabled to suppress warnings during build
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w")  # -w suppresses all warnings
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")

message(STATUS "========================================")
message(STATUS "Building Navigations Project")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "========================================")

# Build các packages theo thứ tự phụ thuộc
# 1. Core libraries (header-only hoặc base libraries)

if (NOT TARGET robot_time)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/robot_time)
endif()

if (NOT TARGET xmlrpcpp)
   add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/xmlrpcpp) 
endif()

if (NOT TARGET robot_cpp)
   add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/robot_cpp) 
endif()

if (NOT TARGET data_convert)
   add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/data_convert) 
endif()

if (NOT TARGET geometry2)
   add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/geometry2) 
endif()

if (NOT TARGET common_msgs)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/common_msgs) 
endif()

if (NOT TARGET laser_geometry)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/laser_geometry) 
endif()

if (NOT TARGET voxel_grid)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/voxel_grid) 
endif()

if (NOT TARGET robot_nav_2d_msgs)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/robot_nav_2d_msgs) 
endif()

if (NOT TARGET costmap_2d)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/costmap_2d)
endif()

if (NOT TARGET robot_nav_2d_utils)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/robot_nav_2d_utils) 
endif()

if (NOT TARGET laser_filter)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/laser_filter) 
endif()

# Port của ROS `angles`. KHÔNG phải gói `angles` ở src/Algorithms/Libraries — đó là package khác,
# target tên `angles`, và cả hai cùng có mặt trong cây. Project lẫn target của gói này đều tên
# `robot_angles_prv`, nên guard phải kiểm ĐÚNG tên đó: kiểm `angles` sẽ trúng gói kia và gói này
# lặng lẽ không bao giờ được add.
if (NOT TARGET robot_angles_prv)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/angles)
endif()

# Lõi hình học thuần, không phụ thuộc gói nào trong cây ngoài robot_cpp. Đặt sớm vì
# dock_pattern_detector khớp đường tròn trên chính đoạn thẳng mà gói này trích ra.
if (NOT TARGET robot_line_extraction)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/robot_line_extraction)
endif()

if (NOT TARGET dock_pattern_detector)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/dock_pattern_detector)
endif()

# Lõi grid map. hybrid_local_planner link thẳng vào `robot_grid_map_core`, nên nó phải có trước.
# Project và target đều tên `robot_grid_map_core`, không phải `grid_map_core`.
if (NOT TARGET robot_grid_map_core)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/grid_map_core)
endif()

# Port của ROS `image_geometry`. Project và target đều tên `robot_image_geometry`, không phải
# `image_geometry` — guard phải kiểm đúng tên đó.
if (NOT TARGET robot_image_geometry)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/image_geometry)
endif()

# Phải đứng SAU robot_image_geometry: nhánh standalone của nó link thẳng vào target đó.
if (NOT TARGET robot_depth_image_proc)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/depth_image_proc)
endif()

# Kho cảm biến thô dùng chung. move_base2 ĐẨY vào đây từ bên trong addLaserScan/addPointCloud2/...,
# còn bộ dò dock và (sau này) RL planner ĐỌC ra — nên nó phải có trước cả hai.
if (NOT TARGET robot_sensor_ports)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/robot_sensor_ports)
endif()

# Bộ giải lattice. sbpl_lattice_planner link vào nó, nên thứ tự này là ràng buộc thật.
if (NOT TARGET robot_sbpl)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/sbpl)
endif()

if (NOT TARGET robot_nav_core)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Navigations/Cores/robot_nav_core)
endif()

if (NOT TARGET nav_grid)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Navigations/Libraries/nav_grid) 
endif()

if (NOT TARGET robot_nav_core2)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Navigations/Cores/robot_nav_core2)
endif()

if (NOT TARGET robot_nav_core_adapter)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Navigations/Cores/robot_nav_core_adapter)
endif()

if (NOT TARGET move_base_core)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Navigations/Cores/move_base_core)
endif()

if (NOT TARGET mkt_msgs)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Libraries/mkt_msgs)
endif()

if (NOT TARGET angles)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Libraries/angles)
endif()

if (NOT TARGET kalman)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Libraries/kalman)
endif()

if (NOT TARGET score_algorithm)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Cores/score_algorithm)
endif()

if (NOT TARGET mkt_plugins)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Libraries/mkt_plugins)
endif()

if (NOT TARGET mkt_algorithm)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Libraries/mkt_algorithm)
endif()

if (NOT TARGET two_points_planner)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Packages/global_planners/two_points_planner)
endif()

if (NOT TARGET custom_planner)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Packages/global_planners/custom_planner)
endif()

if (NOT TARGET dock_planner)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Packages/global_planners/dock_planner)
endif()

# Phải đứng SAU `sbpl`: đây là plugin bọc bộ giải đó.
if (NOT TARGET sbpl_lattice_planner)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Packages/global_planners/sbpl_lattice_planner)
endif()

# LaneRoutePlanner: graph tier (Voronoi + làn một chiều) và lattice dự phòng. Thuật toán nằm ở
# lane_route_core (C++ thuần); lane_route_planner là adapter BaseGlobalPlanner, cần costmap_2d
# (đồ thị tham chiếu tính nền), robot_nav_core, robot_cpp, robot_time — đều đã add ở trên.
if (NOT TARGET lane_route_core)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/lane_route_core)
endif()

if (NOT TARGET lane_route_planner)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Packages/global_planners/lane_route_planner)
endif()

if (NOT TARGET pnkx_local_planner)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Packages/local_planners/pnkx_local_planner)
endif()

# Bốn local planner hiện thực interface robot_nav_core2::LocalPlanner, được gọi tới qua
# LocalPlannerAdapter. Đều cần robot_nav_core2 + robot_costmap_2d đã có ở trên.
#
# ⚠ move_base_common_params.yaml đang trỏ `PriestLocalPlanner` và `HybridDockingLocalPlanner`.
#   Thiếu một trong hai .so là move_base2 chết ngay ở `initialize()`.
if (NOT TARGET hybrid_local_planner)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Packages/local_planners/hybrid_local_planner)
endif()

if (NOT TARGET mppi_local_planner)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Packages/local_planners/mppi_local_planner)
endif()

if (NOT TARGET priest_local_planner)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Packages/local_planners/priest_local_planner)
endif()

if (NOT TARGET priest_mpcc_local_planner)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Packages/local_planners/priest_mpcc_local_planner)
endif()

if (NOT TARGET stanley_local_planner)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Packages/local_planners/stanley_local_planner)
endif()

if (NOT TARGET robot_actionlib_msgs)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Navigations/Libraries/robot_actionlib_msgs)
endif()

if (NOT TARGET robot_move_base_msgs)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Navigations/Libraries/robot_move_base_msgs)
endif()

if (NOT TARGET robot_clear_costmap_recovery)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Navigations/Libraries/robot_clear_costmap_recovery)
endif()

# 3. Mission / action / recovery framework, rồi navigation runtime
#
# Trước đây khối này là một shim di trú: nó đi tìm thư mục `Test/` bên cạnh (của AMR_T800) và add
# gói từ đó. Cây này đã di trú xong — mọi gói nằm in-tree dưới dạng git submodule — nên shim bị gỡ
# hẳn. Giữ lại là một quả bom hẹn giờ: `PNKX_NAV_CORE_BUILD_TEST_PACKAGES` mặc định ON, không tìm
# thấy `Test/` là FATAL_ERROR ngay lúc configure.
#
# Thứ tự dưới đây là RÀNG BUỘC THẬT, không phải phong cách. move_base2 tiêu thụ API của cả ba gói
# framework (ActionRunner <- action_core, RecoveryRunner <- recovery_core, MissionLayer <-
# mission_adapters) nên nó phải đứng cuối cùng.

if (NOT TARGET action_core)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/action_core)
endif()

if (NOT TARGET recovery_core)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/recovery_core)
endif()

if (NOT TARGET mission_adapters)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/mission_adapters)
endif()

# Lõi EnvMonitor + detector plugin (.so: contact, stall, unreachable, intrusion). move_base2 link
# thẳng vào `env_monitor_core`, nên phải add trước move_base2; cần robot_time, robot_cpp,
# robot_sensor_ports, robot_sensor_msgs — đều đã add ở trên.
if (NOT TARGET env_monitor_core)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Libraries/env_monitor_core)
endif()

# Guard là `move_base2_core`, không phải `move_base2`: project tên một đằng còn target thư viện tên
# một nẻo, và guard sai tên thì không bao giờ khớp — subdirectory được add hai lần khi configure
# lại, lỗi hiện ra dưới dạng "target đã tồn tại".
if (NOT TARGET move_base2_core)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Navigations/Packages/move_base2)
endif()

# Recovery vòng đường, dựng plan tránh trên costmap gộp. Cần CẢ recovery_core (interface) lẫn
# sbpl_lattice_planner (bộ lập plan nó gọi tới).
if (NOT TARGET sbpl_recovery)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Packages/recoveries/sbpl_recovery)
endif()

# Bộ dò biên dạng dock từ scan LiDAR thô, nạp như một action handler.
#
# ⚠ Guard kiểm `dock_pattern_action_handler`, KHÔNG phải `dock_pattern_action`: project tên một
#   đằng còn target thư viện tên một nẻo.
if (NOT TARGET dock_pattern_action_handler)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Packages/actions/dock_pattern_action)
endif()

# Bộ dò xe hàng 4 chân từ scan LiDAR thô, nạp như một action handler. Cần dock_pattern_detector
# (LegPatternMatcher), action_core và robot_sensor_ports — đều đã add ở trên.
#
# ⚠ Guard kiểm target thư viện `trolley_detection_action_handler`, như dock_pattern_action.
if (NOT TARGET trolley_detection_action_handler)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Packages/actions/trolley_detection_action)
endif()

# Bộ dò trạm dừng trước tường có hai dải phản quang, từ scan LiDAR thô, nạp như một action handler.
# Cần dock_pattern_detector (ReflectorPairMatcher), action_core và robot_sensor_ports — đều đã add ở
# trên.
#
# ⚠ Guard kiểm target thư viện `wall_reflector_detection_action_handler`, như hai gói action trên.
if (NOT TARGET wall_reflector_detection_action_handler)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/Algorithms/Packages/actions/wall_reflector_detection_action)
endif()

# C API for .NET/C# integration. Target is `nav_c_api` — the guard must name the same target,
# otherwise it never matches and the subdirectory is added twice on a re-entrant configure.
if (NOT TARGET nav_c_api)
    add_subdirectory(${CMAKE_SOURCE_DIR}/src/APIs/c_api)
endif()

message(STATUS "========================================")
message(STATUS "All packages configured successfully")
message(STATUS "========================================")
