set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Quick REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Quick DBus QuickControls2 REQUIRED)
find_package(PkgConfig)
find_package(KF5WindowSystem)
pkg_check_modules(KYSDKWAYLANDHELPER_PKG kysdk-waylandhelper)

include_directories(ipc)
include_directories(utils)
include_directories(qml)

set(PROJECT_SOURCES
        main.cpp
        okinputmethod.cpp
        okinputmethod.h
        inputwindowmanager.cpp
        inputwindowmanager.h
        statusbarmanager.cpp
        statusbarmanager.h
        inputwindowmodel.cpp
        inputwindowmodel.h
        statusbaritem.h
        statusbaritem.cpp
        statusbarmodel.h
        statusbarmodel.cpp
        tooltip.cpp
        tooltip.h
        statusbarview.cpp
        statusbarview.h
        inputwindowview.cpp
        inputwindowview.h
        ipc/ipctypes.cpp
        ipc/ipctypes.h
        ipc/dbuschannel.cpp
        ipc/dbuschannel.h
        utils/geometrymanager.cpp
        utils/geometrymanager.h
        utils/inputwindowgeometrymanager.cpp
        utils/inputwindowgeometrymanager.h
        utils/statusbarsettings.cpp
        utils/statusbarsettings.h
        qml.qrc
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(ok-input-method
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET ok-input-method APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(ok-input-method SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(ok-input-method
          ${PROJECT_SOURCES}
        )
    endif()
endif()

target_compile_definitions(ok-input-method
  PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(ok-input-method
  PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Quick Qt${QT_VERSION_MAJOR}::DBus Qt${QT_VERSION_MAJOR}::QuickControls2)

target_include_directories(ok-input-method PRIVATE ${KYSDKWAYLANDHELPER_PKG_INCLUDE_DIRS})
target_link_directories(ok-input-method PRIVATE ${KYSDKWAYLANDHELPER_PKG_LIBRARY_DIRS})
target_link_libraries(ok-input-method PRIVATE KF5::WindowSystem ${KYSDKWAYLANDHELPER_PKG_LIBRARIES})

set_target_properties(ok-input-method PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_import_qml_plugins(ok-input-method)
    qt_finalize_executable(ok-input-method)
endif()

install(TARGETS ok-input-method DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/data/org.ukui.inputmethod.service" DESTINATION "${CMAKE_INSTALL_PREFIX}/share/dbus-1/services")
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/data/ok-input-method.conf" DESTINATION "${CMAKE_INSTALL_PREFIX}/share/ok-input-method")