cmake_minimum_required(VERSION 3.16)
project(StreamHubClient CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# ── Find system packages ──────────────────────────────────────────────────────
find_package(OpenGL REQUIRED)

# SDL2: try cmake config first, fall back to pkg-config
find_package(SDL2 QUIET CONFIG)
if(NOT SDL2_FOUND)
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(SDL2 REQUIRED sdl2)
    add_library(SDL2::SDL2 INTERFACE IMPORTED)
    target_include_directories(SDL2::SDL2 INTERFACE ${SDL2_INCLUDE_DIRS})
    target_link_libraries(SDL2::SDL2 INTERFACE ${SDL2_LIBRARIES})
    target_compile_options(SDL2::SDL2 INTERFACE ${SDL2_CFLAGS_OTHER})
endif()

# ── Fetch Dear ImGui ──────────────────────────────────────────────────────────
include(FetchContent)

FetchContent_Declare(
    imgui
    GIT_REPOSITORY https://github.com/ocornut/imgui.git
    GIT_TAG        v1.91.8
    GIT_SHALLOW    TRUE
)
FetchContent_MakeAvailable(imgui)

# ── Fetch ImPlot ──────────────────────────────────────────────────────────────
FetchContent_Declare(
    implot
    GIT_REPOSITORY https://github.com/epezent/implot.git
    GIT_TAG        v0.17
    GIT_SHALLOW    TRUE
)
FetchContent_MakeAvailable(implot)

# ── Build ImGui + ImPlot as a static library ──────────────────────────────────
add_library(imgui_lib STATIC
    ${imgui_SOURCE_DIR}/imgui.cpp
    ${imgui_SOURCE_DIR}/imgui_draw.cpp
    ${imgui_SOURCE_DIR}/imgui_tables.cpp
    ${imgui_SOURCE_DIR}/imgui_widgets.cpp
    ${imgui_SOURCE_DIR}/backends/imgui_impl_sdl2.cpp
    ${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp
    ${implot_SOURCE_DIR}/implot.cpp
    ${implot_SOURCE_DIR}/implot_items.cpp
)
target_include_directories(imgui_lib PUBLIC
    ${imgui_SOURCE_DIR}
    ${imgui_SOURCE_DIR}/backends
    ${implot_SOURCE_DIR}
)
target_link_libraries(imgui_lib PUBLIC SDL2::SDL2 OpenGL::GL)
target_compile_options(imgui_lib PRIVATE -w)  # suppress warnings from vendored code

# ── Bundled resources (fonts, desktop entry, icon) ───────────────────────────
# Fonts (UI font + Font Awesome 6 Free Solid + glyph-macro header) are vendored
# in resources/fonts — no network access or system fonts required.
set(RESOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/resources)
set(FONT_DIR     ${RESOURCE_DIR}/fonts)

if(EXISTS ${FONT_DIR}/fa-solid-900.ttf AND EXISTS ${FONT_DIR}/IconsFontAwesome6.h)
    set(HAVE_FONT_AWESOME TRUE)
    message(STATUS "Font Awesome icons enabled (${FONT_DIR})")
else()
    set(HAVE_FONT_AWESOME FALSE)
    message(WARNING "Bundled Font Awesome missing — using ASCII icon fallbacks")
endif()

# Mirror resources next to the built executable so dev runs find them
file(COPY ${RESOURCE_DIR}/fonts DESTINATION ${CMAKE_BINARY_DIR}/resources)

# ── Application sources ───────────────────────────────────────────────────────
set(APP_SOURCES
    main.cpp
    App.cpp
    WSClient.cpp
    Protocol.cpp
    SourcePanel.cpp
    PlotPanel.cpp
    TriggerPanel.cpp
    StatsPanel.cpp
)

add_executable(StreamHubClient ${APP_SOURCES})

target_include_directories(StreamHubClient PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
    # Reuse WSFrame.h, SHA1.h, Base64.h from StreamHub without copying
    ${CMAKE_CURRENT_SOURCE_DIR}/../../Source/Applications/StreamHub
)

target_link_libraries(StreamHubClient PRIVATE
    imgui_lib
    SDL2::SDL2
    OpenGL::GL
    pthread
)

# Source-tree resource dir baked in as last-resort runtime fallback
target_compile_definitions(StreamHubClient PRIVATE
    APP_RESOURCE_DIR="${RESOURCE_DIR}"
)
if(HAVE_FONT_AWESOME)
    target_include_directories(StreamHubClient PRIVATE ${FONT_DIR})
    target_compile_definitions(StreamHubClient PRIVATE HAVE_FONT_AWESOME)
endif()

target_compile_options(StreamHubClient PRIVATE
    -Wall -Wextra -Wno-unused-parameter
)

# ── Install ───────────────────────────────────────────────────────────────────
install(TARGETS StreamHubClient DESTINATION bin)
install(DIRECTORY ${RESOURCE_DIR}/fonts DESTINATION share/streamhub-client)
install(FILES ${RESOURCE_DIR}/streamhub-client.desktop
        DESTINATION share/applications)
install(FILES ${RESOURCE_DIR}/icons/streamhub-client.svg
        DESTINATION share/icons/hicolor/scalable/apps)
