cmake_minimum_required(VERSION 3.10)
project(mkt_algorithm VERSION 1.0.0 LANGUAGES CXX)

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

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

# Find dependencies
find_package(Boost REQUIRED COMPONENTS system)
find_package(Eigen3 REQUIRED)

# Thư mục include
include_directories(
    ${PROJECT_SOURCE_DIR}/include
    ${EIGEN3_INCLUDE_DIRS}
)

# Dependencies packages
set(PACKAGES_DIR
    geometry_msgs
    score_algorithm
    robot_nav_2d_msgs
    robot_nav_2d_utils
    kalman
    angles
    nav_grid
    robot_costmap_2d
    robot_sensor_msgs
    robot_std_msgs
)

# Tìm tất cả file source cho diff library
file(GLOB DIFF_SOURCES 
    "src/diff/diff_predictive_trajectory.cpp"
    "src/diff/diff_rotate_to_goal.cpp"
    "src/diff/diff_go_straight.cpp"
)

file(GLOB DIFF_HEADERS 
    "include/mkt_algorithm/diff/*.h"
)

# Tạo thư viện shared cho diff
add_library(mkt_algorithm_diff SHARED ${DIFF_SOURCES} ${DIFF_HEADERS})

# Link libraries
target_link_libraries(mkt_algorithm_diff
    PUBLIC
        ${PACKAGES_DIR}
        robot_cpp
        Boost::system
        Eigen3::Eigen
)

set_target_properties(mkt_algorithm_diff PROPERTIES
  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

# Set include directories
target_include_directories(mkt_algorithm_diff
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
        ${EIGEN3_INCLUDE_DIRS}
)

# Cài đặt header files
install(DIRECTORY include/mkt_algorithm
    DESTINATION include
  FILES_MATCHING PATTERN "*.h"
)

# Cài đặt library
install(TARGETS mkt_algorithm_diff
    EXPORT mkt_algorithm-targets
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION bin
)

# Cài đặt plugins.xml (nếu cần cho pluginlib)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/plugins.xml)
    install(FILES plugins.xml
        DESTINATION share/${PROJECT_NAME}
)
endif()

# Export targets
install(EXPORT mkt_algorithm-targets
    FILE mkt_algorithm-targets.cmake
    DESTINATION lib/cmake/mkt_algorithm
)

# Tùy chọn build
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
option(BUILD_TESTS "Build test programs" OFF)

# Flags biên dịch
# 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")

# In thông tin cấu hình
message(STATUS "=================================")
message(STATUS "Project: ${PROJECT_NAME}")
message(STATUS "Version: ${PROJECT_VERSION}")
message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "Eigen3 found: ${EIGEN3_FOUND}")
message(STATUS "=================================")
