cmake_minimum_required(VERSION 3.16)
file(STRINGS "${CMAKE_CURRENT_LIST_DIR}/VERSION" SUN_STREAM_VERSION LIMIT_COUNT 1)
project(sun_stream_host VERSION "${SUN_STREAM_VERSION}" LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

find_package(PkgConfig REQUIRED)
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0)
pkg_check_modules(GSTREAMER REQUIRED IMPORTED_TARGET
    gstreamer-1.0 gstreamer-app-1.0 gstreamer-video-1.0 gstreamer-allocators-1.0)
pkg_check_modules(GIO_UNIX REQUIRED IMPORTED_TARGET gio-unix-2.0)

add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../shared/pairing"
    "${CMAKE_CURRENT_BINARY_DIR}/sun-pairing")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../linux-player"
    "${CMAKE_CURRENT_BINARY_DIR}/linux-player")

add_executable(sun-stream-host
    src/main.cpp
    src/desktop_input_server.cpp
    src/linux_gamepad.cpp
    src/linux_gamepad_test.cpp
    ../sun-engine/windows/gamepad_state.cpp
    ../sun-engine/windows/sun_gamepad_codec.cpp
    ../sun-engine/windows/protocol_v2.cpp
)
target_compile_definitions(sun-stream-host PRIVATE SUN_STREAM_VERSION="${PROJECT_VERSION}")
target_compile_options(sun-stream-host PRIVATE -Wall -Wextra -Wpedantic -Wno-deprecated-declarations)
target_link_libraries(sun-stream-host PRIVATE
    PkgConfig::GTK
    PkgConfig::GSTREAMER
    PkgConfig::GIO_UNIX
    Threads::Threads
    OpenSSL::SSL
    OpenSSL::Crypto
    sun_pairing
    sun-player-workspace
    ${CMAKE_DL_LIBS}
)
set_target_properties(sun-stream-host PROPERTIES OUTPUT_NAME sun-stream)
set(SUN_SMART_SDK_DIR "" CACHE PATH "Verified Linux x64 SUN libwebrtc artifact directory")
set(SUN_SMART_SIGNALING_DIR "" CACHE PATH "Verified private Linux SUN signaling artifact directory")
if(SUN_SMART_SDK_DIR OR SUN_SMART_SIGNALING_DIR)
    if(NOT SUN_SMART_SDK_DIR OR NOT SUN_SMART_SIGNALING_DIR)
        message(FATAL_ERROR "Linux Smart packaging requires matching SDK and signaling receipts")
    endif()
    find_package(Python3 REQUIRED COMPONENTS Interpreter)
    set_property(TARGET sun-stream-host APPEND PROPERTY LINK_DEPENDS
        "${SUN_SMART_SDK_DIR}/sdk-manifest.json"
        "${SUN_SMART_SIGNALING_DIR}/signaling-manifest.json")
    add_custom_command(TARGET sun-stream-host POST_BUILD
        COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/../tools/stage_linux_smart.py"
            --install --configuration "$<IF:$<CONFIG:Debug>,Debug,Release>"
            --directory "$<TARGET_FILE_DIR:sun-stream-host>/smart-modules"
            --sdk "${SUN_SMART_SDK_DIR}" --signaling "${SUN_SMART_SIGNALING_DIR}"
        VERBATIM)
    include(GNUInstallDirs)
    target_compile_definitions(sun-player-core PUBLIC SUN_SMART_INSTALL_DIR="${CMAKE_INSTALL_FULL_LIBDIR}/sun-stream/rtc")
    install(DIRECTORY "$<TARGET_FILE_DIR:sun-stream-host>/smart-modules/" DESTINATION "${CMAKE_INSTALL_LIBDIR}/sun-stream/rtc")
endif()
add_custom_command(TARGET sun-stream-host POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<TARGET_FILE:sun-stream-host>
        $<TARGET_FILE_DIR:sun-stream-host>/sun-engine-host
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        "${CMAKE_CURRENT_SOURCE_DIR}/../windows-host/assets/host-ui/logo.png"
        $<TARGET_FILE_DIR:sun-stream-host>/sun-stream-host.png
)

install(TARGETS sun-stream-host RUNTIME DESTINATION bin)
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../shared/desktop/ui-assets/" DESTINATION share/sun-stream-host/player/shared/desktop/ui-assets COMPONENT PlayerAssets)
install(FILES
    "${CMAKE_CURRENT_SOURCE_DIR}/../tv-app/src/main/res/drawable-nodpi/tv_windows.png"
    "${CMAKE_CURRENT_SOURCE_DIR}/../tv-app/src/main/res/drawable-nodpi/tv_linux.png"
    "${CMAKE_CURRENT_SOURCE_DIR}/../tv-app/src/main/res/drawable-nodpi/tv_apple.png"
    DESTINATION share/sun-stream-host/player/tv-app/src/main/res/drawable-nodpi COMPONENT PlayerAssets)
install(PROGRAMS "${CMAKE_CURRENT_BINARY_DIR}/sun-engine-host" DESTINATION bin)
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../shared/gamepad-artwork/"
    DESTINATION share/sun-stream-host/gamepad-artwork
    FILES_MATCHING
        PATTERN "gamepad_test_*.png"
        PATTERN "gamepad_physical_series.png"
        PATTERN "gamepad_physical_one.png"
        PATTERN "gamepad_physical_edge.png"
        PATTERN "gamepad_physical_elite2.png")

include(CTest)
if(BUILD_TESTING)
    add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../shared/video" "${CMAKE_CURRENT_BINARY_DIR}/codec-matrix-tests")
    get_target_property(SUN_HOST_TEST_LINKS sun-stream-host LINK_LIBRARIES)
    add_executable(sun-linux-motion-tests
        tests/motion_tests.cpp src/desktop_input_server.cpp src/linux_gamepad.cpp
        src/linux_gamepad_test.cpp ../sun-engine/windows/gamepad_state.cpp
        ../sun-engine/windows/sun_gamepad_codec.cpp)
    target_link_libraries(sun-linux-motion-tests PRIVATE ${SUN_HOST_TEST_LINKS})
    target_include_directories(sun-linux-motion-tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src")
    target_compile_definitions(sun-linux-motion-tests PRIVATE SUN_STREAM_VERSION="${PROJECT_VERSION}")
    target_compile_options(sun-linux-motion-tests PRIVATE -UNDEBUG -Wno-deprecated-declarations -ffunction-sections -fdata-sections)
    target_link_options(sun-linux-motion-tests PRIVATE -Wl,--gc-sections)
    add_test(NAME sun-linux-motion COMMAND sun-linux-motion-tests "${CMAKE_CURRENT_BINARY_DIR}/motion-evidence")
    set_tests_properties(sun-linux-motion PROPERTIES TIMEOUT 45 SKIP_RETURN_CODE 77)
    add_executable(sun-linux-receiver-destination-tests
        tests/receiver_destination_tests.cpp src/desktop_input_server.cpp src/linux_gamepad.cpp
        src/linux_gamepad_test.cpp ../sun-engine/windows/gamepad_state.cpp
        ../sun-engine/windows/sun_gamepad_codec.cpp)
    target_link_libraries(sun-linux-receiver-destination-tests PRIVATE ${SUN_HOST_TEST_LINKS})
    target_include_directories(sun-linux-receiver-destination-tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src")
    target_compile_definitions(sun-linux-receiver-destination-tests PRIVATE SUN_STREAM_VERSION="${PROJECT_VERSION}")
    target_compile_options(sun-linux-receiver-destination-tests PRIVATE -UNDEBUG -Wno-deprecated-declarations -ffunction-sections -fdata-sections)
    target_link_options(sun-linux-receiver-destination-tests PRIVATE -Wl,--gc-sections)
    add_test(NAME sun-linux-receiver-destination COMMAND sun-linux-receiver-destination-tests)
    set_tests_properties(sun-linux-receiver-destination PROPERTIES TIMEOUT 30)
    add_executable(sun-linux-input-protocol-tests EXCLUDE_FROM_ALL tests/input_protocol_tests.cpp)
    target_link_libraries(sun-linux-input-protocol-tests PRIVATE ${SUN_HOST_TEST_LINKS})
    target_compile_definitions(sun-linux-input-protocol-tests PRIVATE SUN_STREAM_VERSION="${PROJECT_VERSION}")
    target_compile_options(sun-linux-input-protocol-tests PRIVATE
        -Wno-deprecated-declarations -ffunction-sections -fdata-sections)
    target_link_options(sun-linux-input-protocol-tests PRIVATE -Wl,--gc-sections)
    add_executable(sun-linux-tv-codec-tests EXCLUDE_FROM_ALL
        tests/tv_codec_tests.cpp src/desktop_input_server.cpp src/linux_gamepad.cpp
        src/linux_gamepad_test.cpp ../sun-engine/windows/gamepad_state.cpp
        ../sun-engine/windows/sun_gamepad_codec.cpp)
    # This fixture runs the shared native probe, not the application shell.
    set(SUN_CODEC_TEST_LINKS ${SUN_HOST_TEST_LINKS})
    list(REMOVE_ITEM SUN_CODEC_TEST_LINKS sun-player-workspace)
    target_link_libraries(sun-linux-tv-codec-tests PRIVATE ${SUN_CODEC_TEST_LINKS} sun-player-core)
    target_compile_definitions(sun-linux-tv-codec-tests PRIVATE SUN_STREAM_VERSION="${PROJECT_VERSION}")
    target_compile_options(sun-linux-tv-codec-tests PRIVATE -Wno-deprecated-declarations -ffunction-sections -fdata-sections)
    target_link_options(sun-linux-tv-codec-tests PRIVATE -Wl,--gc-sections)
    # Opt-in source fixture: requires a private profile/Xvfb, no application package.
    add_executable(sun-linux-tv-session-tests EXCLUDE_FROM_ALL
        tests/tv_session_end_tests.cpp src/desktop_input_server.cpp src/linux_gamepad.cpp
        src/linux_gamepad_test.cpp ../sun-engine/windows/gamepad_state.cpp
        ../sun-engine/windows/sun_gamepad_codec.cpp)
    target_link_libraries(sun-linux-tv-session-tests PRIVATE ${SUN_HOST_TEST_LINKS})
    target_compile_definitions(sun-linux-tv-session-tests PRIVATE SUN_STREAM_VERSION="${PROJECT_VERSION}")
    target_compile_options(sun-linux-tv-session-tests PRIVATE -Wno-deprecated-declarations)
    add_executable(sun-update-policy-tests ../shared/updates/tests.cpp)
    target_link_libraries(sun-update-policy-tests PRIVATE OpenSSL::Crypto Threads::Threads)
    add_test(NAME sun-update-policy COMMAND sun-update-policy-tests)
    add_executable(sun-linux-host-request-tests tests/local_host_request_tests.cpp)
    target_link_libraries(sun-linux-host-request-tests PRIVATE sun-player-core)
    add_test(NAME sun-linux-current-host-request COMMAND sun-linux-host-request-tests)
    set_tests_properties(sun-linux-current-host-request PROPERTIES TIMEOUT 30 RUN_SERIAL TRUE)
    add_executable(sun-linux-local-access-tests tests/local_access_tests.cpp)
    target_link_libraries(sun-linux-local-access-tests PRIVATE sun-player-core)
    add_test(NAME sun-linux-current-local-access COMMAND sun-linux-local-access-tests)
    add_executable(sun-linux-host-c1-tests tests/desktop_host_tests.cpp)
    target_link_libraries(sun-linux-host-c1-tests PRIVATE sun-player-core)
    add_test(NAME sun-linux-host-desktop-c1 COMMAND sun-linux-host-c1-tests)
    set_tests_properties(sun-linux-host-desktop-c1 PROPERTIES TIMEOUT 15)
    add_executable(sun-linux-desktop-input-tests tests/desktop_input_tests.cpp
        src/desktop_input_server.cpp src/linux_gamepad.cpp
        ../sun-engine/windows/gamepad_state.cpp ../sun-engine/windows/sun_gamepad_codec.cpp ../sun-engine/windows/protocol_v2.cpp)
    target_link_libraries(sun-linux-desktop-input-tests PRIVATE sun-player-core)
    target_compile_options(sun-linux-desktop-input-tests PRIVATE -UNDEBUG)
    target_link_options(sun-linux-desktop-input-tests PRIVATE
        -Wl,--wrap=open,--wrap=ioctl,--wrap=read,--wrap=write,--wrap=close)
    add_test(NAME sun-linux-desktop-input COMMAND sun-linux-desktop-input-tests)
    add_executable(sun-rtc-input-packet-tests ../shared/internet/tests/internet_input_packet_tests.cpp ../sun-engine/windows/protocol_v2.cpp)
    target_link_libraries(sun-rtc-input-packet-tests PRIVATE OpenSSL::Crypto)
    add_test(NAME sun-rtc-input-packets COMMAND sun-rtc-input-packet-tests)
    set_tests_properties(sun-linux-desktop-input PROPERTIES TIMEOUT 45)
    add_executable(sun-local-input-intent-tests ../windows-player/tests/local_input_intent_tests.cpp)
    target_compile_options(sun-local-input-intent-tests PRIVATE -UNDEBUG)
    add_test(NAME sun-local-input-intent COMMAND sun-local-input-intent-tests)
    add_test(NAME sun-stream-native-self-test COMMAND sun-stream-host --self-test)
endif()

include("${CMAKE_CURRENT_LIST_DIR}/../shared/video/gstreamer_worker.cmake")
add_dependencies(sun-stream-host sun-stream-codec-inspector)
if(TARGET sun-linux-tv-codec-tests)
    add_dependencies(sun-linux-tv-codec-tests sun-stream-codec-inspector)
endif()
