cmake_minimum_required(VERSION 3.16)

project(ScreenWall VERSION 1.0.0 LANGUAGES CXX)

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

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

# 自动处理Qt的MOC、UIC、RCC
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

# 包含目录
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/../AvRecorder
    ${CMAKE_CURRENT_SOURCE_DIR}/../AvRecorder/ui
    ${CMAKE_CURRENT_SOURCE_DIR}/../AvRecorder/capturer
    ${CMAKE_CURRENT_SOURCE_DIR}/../AvRecorder/capturer/finder
    ${CMAKE_CURRENT_SOURCE_DIR}/../AvRecorder/recorder
    ${CMAKE_CURRENT_SOURCE_DIR}/../AvRecorder/basic
    ${CMAKE_CURRENT_SOURCE_DIR}/../AV/code/recorder
    ${CMAKE_CURRENT_SOURCE_DIR}/../AV/code/recorder/ui
    ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/ffmpeg/include
)

# AvRecorder 源文件
set(AVRECORDER_SOURCES
    ../AvRecorder/ui/opengl_video_widget.cpp
    ../AvRecorder/ui/audio_render.cpp
    ../AvRecorder/ui/audio_widget.cpp
    ../AvRecorder/ui/av_recorder.cpp
    ../AvRecorder/ui/settings_page.cpp
    ../AvRecorder/capturer/finder.cpp
    ../AvRecorder/capturer/ivideocapturer.cpp
    ../AvRecorder/recorder/video_recorder.cpp
    ../AvRecorder/recorder/audio_recorder.cpp
    ../AvRecorder/basic/basic.cpp
    ../AvRecorder/basic/frame.cpp
    ../AvRecorder/audioinput.cpp
)

# AvRecorder 头文件
set(AVRECORDER_HEADERS
    ../AvRecorder/ui/opengl_video_widget.h
    ../AvRecorder/ui/audio_render.h
    ../AvRecorder/ui/audio_widget.h
    ../AvRecorder/ui/av_recorder.h
    ../AvRecorder/ui/settings_page.h
    ../AvRecorder/capturer/finder.h
    ../AvRecorder/capturer/base_capturer.h
    ../AvRecorder/recorder/video_recorder.h
    ../AvRecorder/recorder/audio_recorder.h
    ../AvRecorder/basic/basic.h
    ../AvRecorder/basic/frame.h
    ../AvRecorder/basic/timer.h
    ../AvRecorder/audioinput.h
)

# 源文件
set(SOURCES
    main.cpp
    screenwall_widget.cpp
    ${AVRECORDER_SOURCES}
)

# 头文件
set(HEADERS
    screenwall_widget.h
    ${AVRECORDER_HEADERS}
)

# Windows特定设置
if(WIN32)
    # 添加Windows API库
    set(WIN32_LIBS
        dwmapi
        shell32
        psapi
        user32
        gdi32
        ole32
        oleaut32
    )
endif()

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

# 链接Qt库
target_link_libraries(ScreenWall
    Qt6::Core
    Qt6::Widgets
    Qt6::OpenGL
    Qt6::OpenGLWidgets
)

# 链接Windows库
if(WIN32)
    target_link_libraries(ScreenWall ${WIN32_LIBS})
endif()

# FFmpeg 库查找（AvRecorder 依赖）
set(FFMPEG_LIBS)

# 查找 FFmpeg 库
find_library(FFMPEG_AVCODEC_LIB
    NAMES avcodec
    PATHS
        ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/ffmpeg/lib
        ${CMAKE_CURRENT_SOURCE_DIR}/../lib
    NO_DEFAULT_PATH
)

find_library(FFMPEG_AVFORMAT_LIB
    NAMES avformat
    PATHS
        ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/ffmpeg/lib
        ${CMAKE_CURRENT_SOURCE_DIR}/../lib
    NO_DEFAULT_PATH
)

find_library(FFMPEG_AVUTIL_LIB
    NAMES avutil
    PATHS
        ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/ffmpeg/lib
        ${CMAKE_CURRENT_SOURCE_DIR}/../lib
    NO_DEFAULT_PATH
)

if(FFMPEG_AVCODEC_LIB AND FFMPEG_AVFORMAT_LIB AND FFMPEG_AVUTIL_LIB)
    list(APPEND FFMPEG_LIBS 
        ${FFMPEG_AVCODEC_LIB}
        ${FFMPEG_AVFORMAT_LIB}
        ${FFMPEG_AVUTIL_LIB}
    )
    message(STATUS "Found FFmpeg libraries")
    target_link_libraries(ScreenWall ${FFMPEG_LIBS})
else()
    message(WARNING "FFmpeg libraries not found. Some features may not work.")
endif()

# 编译器特定设置
if(MSVC)
    # MSVC特定设置
    target_compile_definitions(ScreenWall PRIVATE
        _CRT_SECURE_NO_WARNINGS
        NOMINMAX
        WIN32_LEAN_AND_MEAN
    )
    
    # 设置运行时库
    set_property(TARGET ScreenWall PROPERTY
        MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()

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

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

# 复制Qt DLL（Windows）
if(WIN32)
    # 获取Qt安装路径
    get_target_property(QT_QMAKE_EXECUTABLE Qt6::qmake IMPORTED_LOCATION)
    get_filename_component(QT_WINDEPLOYQT_EXECUTABLE ${QT_QMAKE_EXECUTABLE} PATH)
    set(QT_WINDEPLOYQT_EXECUTABLE "${QT_WINDEPLOYQT_EXECUTABLE}/windeployqt.exe")
    
    # 添加部署命令
    if(EXISTS ${QT_WINDEPLOYQT_EXECUTABLE})
        add_custom_command(TARGET ScreenWall POST_BUILD
            COMMAND ${QT_WINDEPLOYQT_EXECUTABLE} $<TARGET_FILE:ScreenWall>
            COMMENT "Deploying Qt libraries")
    endif()
endif()

# 打印配置信息
message(STATUS "ScreenWall Configuration:")
message(STATUS "  Qt6 Version: ${Qt6_VERSION}")
message(STATUS "  Build Type: ${CMAKE_BUILD_TYPE}")
message(STATUS "  C++ Standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "  Install Prefix: ${CMAKE_INSTALL_PREFIX}")

# 添加编译选项
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    target_compile_definitions(ScreenWall PRIVATE DEBUG_BUILD)
    message(STATUS "  Debug build enabled")
else()
    target_compile_definitions(ScreenWall PRIVATE RELEASE_BUILD)
    message(STATUS "  Release build enabled")
endif()

# 添加版本信息
target_compile_definitions(ScreenWall PRIVATE
    SCREENWALL_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}
    SCREENWALL_VERSION_MINOR=${PROJECT_VERSION_MINOR}
    SCREENWALL_VERSION_PATCH=${PROJECT_VERSION_PATCH}
    SCREENWALL_VERSION_STRING="${PROJECT_VERSION}"
)