cmake_minimum_required(VERSION 3.16)

# 项目信息
project(SimpleScreenWall VERSION 1.0.0 LANGUAGES CXX)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置构建类型
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

# 查找Qt6组件
find_package(Qt6 REQUIRED COMPONENTS
    Core
    Widgets
    Gui
)

# 启用Qt的MOC、UIC、RCC
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

# 源文件
set(SOURCES
    main_simple.cpp
    screenwall_simple.cpp
)

# 头文件
set(HEADERS
    screenwall_simple.h
)

# 创建可执行文件
add_executable(SimpleScreenWall
    ${SOURCES}
    ${HEADERS}
)

# 链接Qt库
target_link_libraries(SimpleScreenWall
    Qt6::Core
    Qt6::Widgets
    Qt6::Gui
)

# Windows特定设置
if(WIN32)
    # 链接Windows API库
    target_link_libraries(SimpleScreenWall
        user32
        gdi32
        shell32
        psapi
        dwmapi
        ole32
        oleaut32
    )
    
    # 设置Windows子系统
    set_target_properties(SimpleScreenWall PROPERTIES
        WIN32_EXECUTABLE TRUE
    )
    
    # MSVC特定设置
    if(MSVC)
        # 设置编译选项
        target_compile_options(SimpleScreenWall PRIVATE
            /W3          # 警告级别3
            /MP          # 多处理器编译
            /utf-8       # UTF-8编码
        )
        
        # 设置链接选项
        target_link_options(SimpleScreenWall PRIVATE
            /SUBSYSTEM:WINDOWS
        )
        
        # 设置运行时库
        set_property(TARGET SimpleScreenWall PROPERTY
            MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL"
        )
    endif()
endif()

# 设置输出目录
set_target_properties(SimpleScreenWall PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/bin"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/bin"
)

# 编译定义
target_compile_definitions(SimpleScreenWall PRIVATE
    QT_DEPRECATED_WARNINGS
    QT_DISABLE_DEPRECATED_BEFORE=0x060000
    UNICODE
    _UNICODE
    WIN32_LEAN_AND_MEAN
    NOMINMAX
)

# 包含目录
target_include_directories(SimpleScreenWall PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
)

# 安装规则
install(TARGETS SimpleScreenWall
    RUNTIME DESTINATION bin
)

# Windows部署
if(WIN32)
    # 查找windeployqt
    find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINTS ${Qt6_DIR}/../../../bin)
    
    if(WINDEPLOYQT_EXECUTABLE)
        # 添加部署命令
        add_custom_command(TARGET SimpleScreenWall POST_BUILD
            COMMAND ${WINDEPLOYQT_EXECUTABLE} $<TARGET_FILE:SimpleScreenWall>
            COMMENT "Deploying Qt libraries"
        )
        
        message(STATUS "Found windeployqt: ${WINDEPLOYQT_EXECUTABLE}")
    else()
        message(WARNING "windeployqt not found. Manual deployment may be required.")
    endif()
endif()

# 打印构建信息
message(STATUS "")
message(STATUS "========== SimpleScreenWall Build Configuration ==========")
message(STATUS "Project: ${PROJECT_NAME} v${PROJECT_VERSION}")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "C++ standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "Qt version: ${Qt6_VERSION}")
message(STATUS "Compiler: ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "Platform: ${CMAKE_SYSTEM_NAME} ${CMAKE_SYSTEM_VERSION}")
message(STATUS "Architecture: ${CMAKE_SYSTEM_PROCESSOR}")
message(STATUS "Source directory: ${CMAKE_CURRENT_SOURCE_DIR}")
message(STATUS "Binary directory: ${CMAKE_CURRENT_BINARY_DIR}")
message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")
message(STATUS "=========================================================")
message(STATUS "")

# 构建后信息
add_custom_target(build_info ALL
    COMMAND ${CMAKE_COMMAND} -E echo "SimpleScreenWall build completed successfully!"
    COMMAND ${CMAKE_COMMAND} -E echo "Executable: $<TARGET_FILE:SimpleScreenWall>"
    DEPENDS SimpleScreenWall
)

# 清理规则
set_property(DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
    "${CMAKE_BINARY_DIR}/bin"
)

# CPack配置（可选）
if(WIN32)
    set(CPACK_GENERATOR "NSIS;ZIP")
    set(CPACK_PACKAGE_NAME "SimpleScreenWall")
    set(CPACK_PACKAGE_VENDOR "SimpleScreenWall Team")
    set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "轻量级实时窗口预览系统")
    set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
    set(CPACK_PACKAGE_INSTALL_DIRECTORY "SimpleScreenWall")
    set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
    
    # NSIS特定设置
    set(CPACK_NSIS_DISPLAY_NAME "SimpleScreenWall")
    set(CPACK_NSIS_PACKAGE_NAME "SimpleScreenWall")
    set(CPACK_NSIS_CONTACT "support@simplescreenwall.com")
    set(CPACK_NSIS_HELP_LINK "https://github.com/simplescreenwall/simplescreenwall")
    
    include(CPack)
endif()

# 开发者选项
option(BUILD_TESTS "Build unit tests" OFF)
option(ENABLE_DEBUG_OUTPUT "Enable debug output" OFF)

if(ENABLE_DEBUG_OUTPUT)
    target_compile_definitions(SimpleScreenWall PRIVATE DEBUG_OUTPUT)
endif()

if(BUILD_TESTS)
    enable_testing()
    # 这里可以添加测试相关的配置
    message(STATUS "Unit tests enabled")
endif()

# 性能分析选项
option(ENABLE_PROFILING "Enable profiling" OFF)
if(ENABLE_PROFILING AND MSVC)
    target_compile_options(SimpleScreenWall PRIVATE /PROFILE)
    target_link_options(SimpleScreenWall PRIVATE /PROFILE)
endif()