find_package(KF6 ${KF_MIN_VERSION} COMPONENTS
    TextEditor
    XmlGui
    CoreAddons
    I18n
)

if(NOT KF6TextEditor_FOUND)
    message(STATUS "KF6::TextEditor not found — skipping the Kate plugin target")
    return()
endif()

# --- optional Wayland pointer-warp support -------------------------------
# Warping the pointer on Wayland needs the pointer-constraints protocol glue
# (generated with wayland-scanner via ECM) plus Qt's Wayland native interface.
# If any piece is missing we build without it; WaylandCursorWarp::warp() then
# simply returns false and the X11 path (QCursor::setPos) is used instead.
set(OLLIE_WAYLAND OFF)
find_package(PkgConfig QUIET)
find_package(WaylandScanner QUIET)
find_package(Qt6 QUIET COMPONENTS GuiPrivate)
if(PkgConfig_FOUND)
    pkg_check_modules(WAYLAND_CLIENT QUIET wayland-client)
    pkg_check_modules(WAYLAND_PROTOCOLS QUIET wayland-protocols)
endif()

set(OLLIE_PLUGIN_SOURCES ollieplugin.cpp ollieplugin.h sampanel.cpp sampanel.h waylandcursorwarp.h)
set(OLLIE_WAYLAND_PROTO_SRCS "")

if(WaylandScanner_FOUND AND WAYLAND_CLIENT_FOUND AND Qt6GuiPrivate_FOUND)
    if(WAYLAND_PROTOCOLS_FOUND)
        pkg_get_variable(WAYLAND_PROTOCOLS_DIR wayland-protocols pkgdatadir)
    endif()
    set(PC_XML
        "${WAYLAND_PROTOCOLS_DIR}/unstable/pointer-constraints/pointer-constraints-unstable-v1.xml")
    if(EXISTS "${PC_XML}")
        # ECM wires the scanner output (basename-client-protocol.h + .c) into the
        # given source-list variable with correct generated-source handling.
        ecm_add_wayland_client_protocol(OLLIE_WAYLAND_PROTO_SRCS
            PROTOCOL "${PC_XML}"
            BASENAME pointer-constraints-unstable-v1)
        # kcoreaddons_add_plugin only compiles C++ sources — a generated .c in
        # its SOURCES is silently dropped. Build the C glue as its own static
        # lib and link it in.
        add_library(ollie_wayland_proto STATIC ${OLLIE_WAYLAND_PROTO_SRCS})
        target_include_directories(ollie_wayland_proto PUBLIC
            "${CMAKE_CURRENT_BINARY_DIR}" ${WAYLAND_CLIENT_INCLUDE_DIRS})
        set_target_properties(ollie_wayland_proto PROPERTIES
            POSITION_INDEPENDENT_CODE ON
            LINKER_LANGUAGE C
            AUTOMOC OFF AUTOUIC OFF AUTORCC OFF)
        list(APPEND OLLIE_PLUGIN_SOURCES waylandcursorwarp.cpp)
        set(OLLIE_WAYLAND ON)
    endif()
endif()

if(NOT OLLIE_WAYLAND)
    message(STATUS "Wayland pointer-warp support disabled (missing scanner / "
                   "wayland-client / Qt6 GuiPrivate / protocol xml)")
    list(APPEND OLLIE_PLUGIN_SOURCES waylandcursorwarp_stub.cpp)
endif()

kcoreaddons_add_plugin(olliepalette
    SOURCES ${OLLIE_PLUGIN_SOURCES}
    INSTALL_NAMESPACE "kf6/ktexteditor"
)

target_include_directories(olliepalette PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")

target_link_libraries(olliepalette PRIVATE
    palette
    radial
    olliecommands_lib
    project_lib
    plumb_lib
    sam_lib
    KF6::TextEditor
    KF6::XmlGui
    KF6::CoreAddons
    KF6::I18n
    Qt6::Widgets
)

if(OLLIE_WAYLAND)
    target_compile_definitions(olliepalette PRIVATE OLLIE_WAYLAND=1)
    target_include_directories(olliepalette PRIVATE ${WAYLAND_CLIENT_INCLUDE_DIRS})
    target_link_libraries(olliepalette PRIVATE
        ollie_wayland_proto
        Qt6::GuiPrivate
        ${WAYLAND_CLIENT_LIBRARIES})
endif()
