cmake_minimum_required(VERSION 3.16)

project(bianbu-control-center-sdk VERSION 0.3.0 LANGUAGES CXX)

include(GNUInstallDirs)

# Some build systems (e.g. Debian/CI) pass common cache variables. Older CMake
# versions warn if they are not referenced by the project. We intentionally do
# not change behavior here; we only reference them to avoid noisy warnings.
include(CTest)
foreach(_var IN ITEMS
    CMAKE_EXPORT_NO_PACKAGE_REGISTRY
    CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY
    FETCHCONTENT_FULLY_DISCONNECTED
)
    if(DEFINED ${_var})
        set(_ignored "${${_var}}")
    endif()
endforeach()
unset(_ignored)
unset(_var)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 REQUIRED COMPONENTS Widgets)

add_library(bianbu-control-center-sdk-widgets SHARED
    src/widgets/IconTextButton.cpp
    src/widgets/PrimaryButton.cpp
    src/widgets/SecondaryButton.cpp
    src/widgets/StyledComboBox.cpp
    src/widgets/StyledChoiceControl.cpp
    src/widgets/ButtonUtils.cpp
    src/widgets/Switch.cpp
    src/modal/DialogButton.cpp
    src/modal/ModalOverlayManager.cpp
    src/modal/OverlayPanelBase.cpp
    # OverlayPanelBase now includes requestClose() signal; OverlayPanelBaseWithSignal is a header-only alias.
    include/lib-bianbu-control-center/widgets/IconTextButton.h
    include/lib-bianbu-control-center/widgets/PrimaryButton.h
    include/lib-bianbu-control-center/widgets/SecondaryButton.h
    include/lib-bianbu-control-center/widgets/StyledComboBox.h
    include/lib-bianbu-control-center/widgets/StyledChoiceControl.h
    include/lib-bianbu-control-center/widgets/ButtonUtils.h
    include/lib-bianbu-control-center/widgets/Switch.h
    include/lib-bianbu-control-center/modal/DialogButton.h
    include/lib-bianbu-control-center/modal/ModalOverlayManager.h
    include/lib-bianbu-control-center/modal/OverlayPanelBase.h
    include/lib-bianbu-control-center/modal/OverlayPanelBaseWithSignal.h
    include/lib-bianbu-control-center/modal/Modal.h
    include/lib-bianbu-control-center/modal/StyleHelper.h
)

set_target_properties(bianbu-control-center-sdk-widgets PROPERTIES
    AUTOMOC ON
    AUTOUIC OFF
    AUTORCC OFF
    VERSION ${PROJECT_VERSION}
    SOVERSION 1
)

target_include_directories(bianbu-control-center-sdk-widgets
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

target_link_libraries(bianbu-control-center-sdk-widgets
    PUBLIC
        Qt6::Widgets
)

target_compile_definitions(bianbu-control-center-sdk-widgets PRIVATE
    QT_NO_KEYWORDS
)

install(TARGETS bianbu-control-center-sdk-widgets
    EXPORT bianbu-control-center-sdkTargets
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(DIRECTORY include/lib-bianbu-control-center/
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/lib-bianbu-control-center
    FILES_MATCHING PATTERN "*.h"
)

# pkg-config
configure_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/bianbu-control-center-sdk.pc.in
    ${CMAKE_CURRENT_BINARY_DIR}/bianbu-control-center-sdk.pc
    @ONLY
)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/bianbu-control-center-sdk.pc
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)

# Minimal CMake package config
include(CMakePackageConfigHelpers)

write_basic_package_version_file(
    ${CMAKE_CURRENT_BINARY_DIR}/bianbu-control-center-sdkConfigVersion.cmake
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY SameMajorVersion
)

configure_package_config_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/cmake/bianbu-control-center-sdkConfig.cmake.in
    ${CMAKE_CURRENT_BINARY_DIR}/bianbu-control-center-sdkConfig.cmake
    INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/bianbu-control-center-sdk
)

install(FILES
    ${CMAKE_CURRENT_BINARY_DIR}/bianbu-control-center-sdkConfig.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/bianbu-control-center-sdkConfigVersion.cmake
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/bianbu-control-center-sdk
)

install(EXPORT bianbu-control-center-sdkTargets
    NAMESPACE BianbuCC::
    FILE bianbu-control-center-sdkTargets.cmake
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/bianbu-control-center-sdk
)
