# SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: GPL-3.0-or-later

# [v4] Batch print unit tests switch (default OFF)
# Unit tests moved to top-level tests/batch-print (built under BUILD_TESTS)

# Find Qt components needed by batch-print modules (DBus for notifications, PrintSupport for QPdfWriter)
pkg_check_modules(FREETYPE REQUIRED freetype2)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS DBus PrintSupport Sql)

# Import the system pdfium target in this scope (reader/ creates it only for its own subtree)
if (NOT USE_PDFIUM_BUNDLE)
    pkg_check_modules(Deepin-pdfium REQUIRED IMPORTED_TARGET deepin-pdfium)
endif()

# batchprint-core: CupsClient + NotifyClient + PrintSettings + ErrorMessages (no reader source dependency)
add_library(batchprint-core STATIC
    cupsclient.h cupsclient.cpp
    icupsapi.h
    notifyclient.h notifyclient.cpp
    printsettings.h printsettings.cpp
    errormessages.h errormessages.cpp
)

target_include_directories(batchprint-core PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
)

target_link_libraries(batchprint-core PUBLIC
    Qt${QT_VERSION_MAJOR}::Gui
    Qt${QT_VERSION_MAJOR}::DBus
)

# batchprint-convert: FormatConverter (depends on selected reader/document sources + deepin-pdfium)
# Explicitly list only the reader source files needed for document loading/rendering,
# NOT the entire reader GUI (browser/sidebar/widgets/uiframe).
set(BATCHPRINT_READER_SOURCES
    ${CMAKE_SOURCE_DIR}/reader/app/Global.cpp
    ${CMAKE_SOURCE_DIR}/reader/document/Model.cpp
    ${CMAKE_SOURCE_DIR}/reader/document/PDFModel.cpp
    ${CMAKE_SOURCE_DIR}/reader/document/DjVuModel.cpp
    ${CMAKE_SOURCE_DIR}/reader/document/XpsDocumentAdapter.cpp
    ${CMAKE_SOURCE_DIR}/reader/document/XpsTextExtractor.cpp
)

# OFD: Model.cpp gets OFD_SUPPORT_ENABLED from the global add_compile_definitions,
# so OfdModel.cpp (implements OfdDocument::loadDocument) and rofd/cairo must be linked too.
if (OFD_SUPPORT_ENABLED)
    list(APPEND BATCHPRINT_READER_SOURCES
        ${CMAKE_SOURCE_DIR}/reader/document/OfdModel.cpp
        ${CMAKE_SOURCE_DIR}/reader/document/Navigation.cpp
    )
endif()

add_library(batchprint-convert STATIC
    formatconverter.h formatconverter.cpp
    ${BATCHPRINT_READER_SOURCES}
)

set_target_properties(batchprint-convert PROPERTIES
    AUTOMOC ON
    AUTOUIC ON
)

target_include_directories(batchprint-convert PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/reader
    ${CMAKE_SOURCE_DIR}/reader/app
    ${CMAKE_SOURCE_DIR}/reader/document
    ${PDFIUM_INCLUDE_DIRS}
    $<$<BOOL:${XPS_SUPPORT_ENABLED}>:${XPS_DEPS_INCLUDE_DIRS}>
    $<$<BOOL:${OFD_SUPPORT_ENABLED}>:${ROFD_INCLUDE_DIR}>
    $<$<BOOL:${OFD_SUPPORT_ENABLED}>:${OFD_CAIRO_INCLUDE_DIRS}>
)

target_compile_definitions(batchprint-convert PRIVATE
    INSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}"
    INSTALL_LIBDIR="${CMAKE_INSTALL_LIBDIR}"
    APP_VERSION="1.0.0"
)

target_link_libraries(batchprint-convert PUBLIC
    batchprint-core
    Qt${QT_VERSION_MAJOR}::Gui
    Qt${QT_VERSION_MAJOR}::Widgets
    Qt${QT_VERSION_MAJOR}::PrintSupport
    Qt${QT_VERSION_MAJOR}::Network
    Qt${QT_VERSION_MAJOR}::Svg
    Qt${QT_VERSION_MAJOR}::Concurrent
    Qt${QT_VERSION_MAJOR}::Sql
    Qt${QT_VERSION_MAJOR}::Xml
    ${DDJVU_LIBRARIES}
    ${LIBJPEG_LIBRARIES}
    ${FREETYPE_LIBRARIES}
    $<$<BOOL:${XPS_SUPPORT_ENABLED}>:${XPS_DEPS_LIBRARIES}>
    $<$<BOOL:${OFD_SUPPORT_ENABLED}>:${ROFD_FFI_LIBRARY}>
    $<$<BOOL:${OFD_SUPPORT_ENABLED}>:${OFD_CAIRO_LIBRARIES}>
)

# DTK linking
if(DTK_USE_TARGETS)
    target_link_libraries(batchprint-convert PUBLIC
        Dtk${DTK_VERSION_MAJOR}::Widget
        Dtk${DTK_VERSION_MAJOR}::Gui
        Dtk${DTK_VERSION_MAJOR}::Core
    )
endif()

if (QT_VERSION_MAJOR MATCHES 6)
    target_link_libraries(batchprint-convert PUBLIC Qt${QT_VERSION_MAJOR}::Core5Compat)
endif()

if (USE_PDFIUM_BUNDLE)
    target_link_libraries(batchprint-convert PUBLIC deepin-pdfium-reader)
else()
    target_link_libraries(batchprint-convert PUBLIC PkgConfig::Deepin-pdfium)
endif()

if (XPS_SUPPORT_ENABLED)
    target_compile_definitions(batchprint-convert PUBLIC XPS_SUPPORT_ENABLED)
    target_compile_options(batchprint-convert PUBLIC ${XPS_DEPS_CFLAGS_OTHER})
endif()

if (OFD_SUPPORT_ENABLED)
    target_compile_options(batchprint-convert PRIVATE ${OFD_CAIRO_CFLAGS_OTHER})
endif()

# Executable
add_executable(deepin-reader-batchprint
    main.cpp
    batchprintapp.h batchprintapp.cpp
)

target_link_libraries(deepin-reader-batchprint PRIVATE
    batchprint-core
    batchprint-convert
)

install(TARGETS deepin-reader-batchprint
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
