# 模块：domain、persistence、workspace、depth、animation（时间采样）
set(CORE_ROOT ${CMAKE_CURRENT_SOURCE_DIR})

option(LT_WITH_VIPS "Use libvips for GB-scale PNG/JPEG (libvips-dev / pkg-config vips)" ON)

set(CORE_SOURCES
    ${CORE_ROOT}/domain/Project.cpp
    ${CORE_ROOT}/image/ImageFileLoader.cpp
    ${CORE_ROOT}/large_image/VipsBackend.cpp
    ${CORE_ROOT}/workspace/ProjectWorkspace.cpp
    ${CORE_ROOT}/persistence/PersistentBinaryObject.cpp
    ${CORE_ROOT}/persistence/AnimationBinary.cpp
    ${CORE_ROOT}/persistence/EntityPayloadBinary.cpp
    ${CORE_ROOT}/persistence/JsonFileAtomic.cpp
    ${CORE_ROOT}/persistence/EntityBundleStore.cpp
    ${CORE_ROOT}/persistence/AnimationBundleStore.cpp
    ${CORE_ROOT}/persistence/AsyncProjectWriter.cpp
    ${CORE_ROOT}/animation/AnimationEvaluator.cpp
    ${CORE_ROOT}/animation/AnimationSampling.cpp
    ${CORE_ROOT}/depth/DepthService.cpp
    ${CORE_ROOT}/net/ModelServerClient.cpp
    ${CORE_ROOT}/library/EntityJson.cpp
    ${CORE_ROOT}/library/ToolJson.cpp
    ${CORE_ROOT}/library/ResourceLibraryProvider.cpp
    ${CORE_ROOT}/library/OnlineResourceLibraryProvider.cpp
    ${CORE_ROOT}/eval/ProjectEvaluator.cpp
)

set(CORE_HEADERS
    ${CORE_ROOT}/image/ImageDecodeConfig.h
    ${CORE_ROOT}/image/ImageFileLoader.h
    ${CORE_ROOT}/large_image/VipsBackend.h
    ${CORE_ROOT}/domain/EntityIntro.h
    ${CORE_ROOT}/domain/Project.h
    ${CORE_ROOT}/workspace/ProjectWorkspace.h
    ${CORE_ROOT}/persistence/PersistentBinaryObject.h
    ${CORE_ROOT}/persistence/AnimationBinary.h
    ${CORE_ROOT}/persistence/EntityPayloadBinary.h
    ${CORE_ROOT}/persistence/JsonFileAtomic.h
    ${CORE_ROOT}/persistence/EntityBundleStore.h
    ${CORE_ROOT}/persistence/AnimationBundleStore.h
    ${CORE_ROOT}/persistence/AsyncProjectWriter.h
    ${CORE_ROOT}/animation/AnimationEvaluator.h
    ${CORE_ROOT}/animation/AnimationSampling.h
    ${CORE_ROOT}/depth/DepthService.h
    ${CORE_ROOT}/net/ModelServerClient.h
    ${CORE_ROOT}/library/EntityJson.h
    ${CORE_ROOT}/library/ToolJson.h
    ${CORE_ROOT}/library/ResourceLibraryProvider.h
    ${CORE_ROOT}/library/OnlineResourceLibraryProvider.h
    ${CORE_ROOT}/eval/ProjectEvaluator.h
)

add_library(core STATIC
    ${CORE_SOURCES}
    ${CORE_HEADERS}
)

target_include_directories(core
    PUBLIC
        ${CORE_ROOT}
)

target_link_libraries(core
    PUBLIC
        ${QT_PACKAGE}::Core
        ${QT_PACKAGE}::Gui
        ${QT_PACKAGE}::Network
        ${QT_PACKAGE}::Concurrent
)

# libvips：优先 IMPORTED_TARGET；失败则用 pkg-config 变量（部分环境下更稳）
set(LT_VIPS_ENABLED OFF)
if(LT_WITH_VIPS)
    find_package(PkgConfig QUIET)
    if(PKG_CONFIG_FOUND)
        pkg_check_modules(VIPS_PC IMPORTED_TARGET vips)
        if(TARGET PkgConfig::VIPS_PC)
            target_compile_definitions(core PRIVATE LT_HAVE_VIPS=1)
            target_link_libraries(core PUBLIC PkgConfig::VIPS_PC)
            set(LT_VIPS_ENABLED ON)
            message(STATUS "libvips: enabled via IMPORTED_TARGET (LT_HAVE_VIPS)")
        else()
            pkg_check_modules(VIPS_LEGACY QUIET vips)
            if(VIPS_LEGACY_FOUND)
                target_compile_definitions(core PRIVATE LT_HAVE_VIPS=1)
                target_include_directories(core PRIVATE ${VIPS_LEGACY_INCLUDE_DIRS})
                target_link_directories(core PUBLIC ${VIPS_LEGACY_LIBRARY_DIRS})
                target_link_libraries(core PUBLIC ${VIPS_LEGACY_LIBRARIES})
                if(VIPS_LEGACY_CFLAGS_OTHER)
                    separate_arguments(_vips_cflags UNIX_COMMAND "${VIPS_LEGACY_CFLAGS_OTHER}")
                    target_compile_options(core PRIVATE ${_vips_cflags})
                endif()
                set(LT_VIPS_ENABLED ON)
                message(STATUS "libvips: enabled via pkg-config variables (LT_HAVE_VIPS)")
            endif()
        endif()
    endif()
endif()
if(LT_WITH_VIPS AND NOT LT_VIPS_ENABLED)
    message(
        WARNING
            "未找到 libvips（无 pkg-config 模块 vips），已关闭 LT_HAVE_VIPS，大图仍走 Qt 解码。\n"
            "  Debian/Ubuntu: sudo apt install pkg-config libvips-dev\n"
            "  Fedora:          sudo dnf install pkgconf-pkg-config vips-devel\n"
            "  安装后终端执行:   pkg-config --modversion vips   （应打印版本号）\n"
            "  若已安装仍失败:   export PKG_CONFIG_PATH=\"/usr/lib/x86_64-linux-gnu/pkgconfig:\$PKG_CONFIG_PATH\""
    )
endif()
