cmake_minimum_required(VERSION 3.22)
project(desktop_widget_review LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(LingmoSDK REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(ICU REQUIRED IMPORTED_TARGET icu-i18n icu-uc)
pkg_check_modules(CURL REQUIRED IMPORTED_TARGET libcurl)
set(SRC ${CMAKE_CURRENT_SOURCE_DIR}/../../src)
file(GLOB WIDGET_SOURCES ${SRC}/desktop/widgets/*.cpp)
add_library(widget-code STATIC ${WIDGET_SOURCES} ${SRC}/core/config.cpp)
target_include_directories(widget-code PUBLIC ${SRC})
target_compile_options(widget-code PUBLIC -include${SRC}/core/common.h)
target_link_libraries(widget-code PUBLIC Lingmo::SDK PkgConfig::ICU PkgConfig::CURL)
add_executable(widget-review review.cpp)
target_link_libraries(widget-review PRIVATE widget-code)
enable_testing()
add_test(NAME widget-review COMMAND widget-review)
set_tests_properties(widget-review PROPERTIES PASS_REGULAR_EXPRESSION "Desktop widget review passed")

# Compile the real desktop host as well, so catalogue callback integration is checked.
find_path(LINGMO_FILES_INCLUDE_DIR lingmo/files/directory_lister.h REQUIRED)
add_library(widget-desktop-host OBJECT ${SRC}/desktop/desktopwindow.cpp)
target_link_libraries(widget-desktop-host PRIVATE widget-code)
target_include_directories(widget-desktop-host PRIVATE ${LINGMO_FILES_INCLUDE_DIR})
target_compile_options(widget-desktop-host PRIVATE -include${SRC}/namespace_compat.h)

# Exercise the actual dynamic provider contract used by the compositor.
set_target_properties(widget-code PROPERTIES POSITION_INDEPENDENT_CODE ON CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN YES)
add_library(widget-provider SHARED ${SRC}/desktop/widgets/widgetshelf.cpp)
target_link_libraries(widget-provider PRIVATE widget-code)
set_target_properties(widget-provider PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN YES OUTPUT_NAME lingmo-desktop-widgets VERSION 1.0 SOVERSION 1)
set(COMPOSITOR_SOURCE_DIR "" CACHE PATH "Compositor source for integration checks")
if(COMPOSITOR_SOURCE_DIR)
    pkg_check_modules(WAYLAND REQUIRED IMPORTED_TARGET wayland-server)
    pkg_check_modules(XKB REQUIRED IMPORTED_TARGET xkbcommon)
    add_library(notification-centre-host-check OBJECT ${COMPOSITOR_SOURCE_DIR}/src/shell/notification/notification_centre.cpp)
    target_include_directories(notification-centre-host-check PRIVATE ${COMPOSITOR_SOURCE_DIR}/src)
    target_compile_options(notification-centre-host-check PRIVATE -include${COMPOSITOR_SOURCE_DIR}/src/namespace_compat.h)
    target_link_libraries(notification-centre-host-check PRIVATE Lingmo::SDK PkgConfig::WAYLAND PkgConfig::XKB)
    add_executable(notification-provider-review provider.cpp)
    target_include_directories(notification-provider-review PRIVATE ${COMPOSITOR_SOURCE_DIR}/src)
    target_link_libraries(notification-provider-review PRIVATE Lingmo::SDK ${CMAKE_DL_LIBS})
    add_dependencies(notification-provider-review widget-provider)
    add_test(NAME notification-provider COMMAND notification-provider-review)
    set_tests_properties(notification-provider PROPERTIES ENVIRONMENT "LD_LIBRARY_PATH=$<TARGET_FILE_DIR:widget-provider>:$ENV{LD_LIBRARY_PATH}" PASS_REGULAR_EXPRESSION "Notification widget provider passed")
endif()
add_library(widget-application-host OBJECT ${SRC}/app/application.cpp)
target_link_libraries(widget-application-host PRIVATE widget-code)
target_include_directories(widget-application-host PRIVATE ${LINGMO_FILES_INCLUDE_DIR})
target_compile_options(widget-application-host PRIVATE -include${SRC}/namespace_compat.h)
