cmake_minimum_required(VERSION 3.22)

project(SfiziosoPlayer VERSION 1.0.0 LANGUAGES C CXX)
# Native plugin formats require numeric versions; expose the prerelease in the UI.
set(SFIZIOSO_PLAYER_RELEASE_VERSION "1.0.0-rc.1")

# Keep the whole dependency tree on at least C++17. The vendored sfizioso
# CMake defaults to C++14 when it owns the cache, but its current sources use
# hexadecimal floating-point literals which GCC accepts only in C++17 or later.
set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to be used")
if(CMAKE_CXX_STANDARD LESS 17)
    set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to be used" FORCE)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_OSX_DEPLOYMENT_TARGET "12.0" CACHE STRING "macOS deployment target")

# Linux VST3 links static libraries into a shared-object plugin.
if(UNIX AND NOT APPLE)
    set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()

set(SFIZIOSO_PLAYER_BASE_BUNDLE_ID "com.sfizioso.player")
set(SFIZIOSO_PLAYER_PLUGIN_BUNDLE_ID "${SFIZIOSO_PLAYER_BASE_BUNDLE_ID}.plugin")
set(SFIZIOSO_PLAYER_AU_BUNDLE_ID "${SFIZIOSO_PLAYER_BASE_BUNDLE_ID}.au")
set(SFIZIOSO_PLAYER_VST3_BUNDLE_ID "${SFIZIOSO_PLAYER_BASE_BUNDLE_ID}.vst3")
set(SFIZIOSO_PLAYER_STANDALONE_BUNDLE_ID "com.sfizioso.player.standalone")

option(SFIZIOSO_PLAYER_TESTS
    "Build Sfizioso Player tests (defaults to top-level builds only)"
    ${PROJECT_IS_TOP_LEVEL})

# Shared WebView-UI build helper (npm install -> vite build -> zip -> embed).
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/AddWebviewUi.cmake)

# ---------------------------------------------------------------------------
# Dependencies — dual-use.
#
# Built STANDALONE (top-level), this repo owns the JUCE + sfizioso engine
# submodules. Consumed via add_subdirectory() by a parent project (e.g. the
# private sample-machine repo), the parent has already added JUCE + sfizioso,
# so we must NOT add them again or CMake errors on duplicate targets.
# PROJECT_IS_TOP_LEVEL distinguishes the two. Either way the libraries below
# resolve `juce::*` and `sfizioso::sfizioso` from whoever added them.
# ---------------------------------------------------------------------------
if(PROJECT_IS_TOP_LEVEL)
    add_subdirectory(external/JUCE)

    set(SFIZZ_JACK       OFF CACHE BOOL "" FORCE)
    set(SFIZZ_RENDER     OFF CACHE BOOL "" FORCE)
    set(SFIZZ_BENCHMARKS OFF CACHE BOOL "" FORCE)
    set(SFIZZ_TESTS      OFF CACHE BOOL "" FORCE)
    set(SFIZZ_DEMOS      OFF CACHE BOOL "" FORCE)
    set(SFIZZ_DEVTOOLS   OFF CACHE BOOL "" FORCE)
    set(SFIZZ_SHARED     OFF CACHE BOOL "" FORCE)

    # Demote two warnings newer Apple Clang elevates to errors inside the
    # engine's vendored deps (atomic_queue's template-without-arg-list, Abseil's
    # deprecated __is_trivially_relocatable builtin).
    if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        add_compile_options(
            -Wno-error=missing-template-arg-list-after-template-kw
            -Wno-deprecated-builtins)
    endif()

    add_subdirectory(external/sfizioso EXCLUDE_FROM_ALL)

    if(MSVC)
        function(sfizioso_collect_subdirectory_targets directory output_var)
            get_property(_targets DIRECTORY "${directory}" PROPERTY BUILDSYSTEM_TARGETS)
            get_property(_subdirectories DIRECTORY "${directory}" PROPERTY SUBDIRECTORIES)

            foreach(_subdirectory IN LISTS _subdirectories)
                sfizioso_collect_subdirectory_targets("${_subdirectory}" _subdirectory_targets)
                list(APPEND _targets ${_subdirectory_targets})
            endforeach()

            set(${output_var} ${_targets} PARENT_SCOPE)
        endfunction()

        # The vendored sfizioso project forces /MT for MSVC, while JUCE's plugin
        # client targets default to /MD. The Windows VST3 links both into one
        # DLL, so keep sfizioso and its vendored deps on the same runtime.
        sfizioso_collect_subdirectory_targets(
            "${CMAKE_CURRENT_SOURCE_DIR}/external/sfizioso"
            _sfizioso_targets)
        foreach(_sfizioso_target IN LISTS _sfizioso_targets)
            set_target_properties(${_sfizioso_target} PROPERTIES
                MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
        endforeach()
    endif()
endif()

# ---------------------------------------------------------------------------
# Engine MPE + debug-panel options. Defined here in the foundation repo and
# applied PUBLIC on player_core so every consumer inherits the same value.
# (Moved here from the private consumer as part of the open-core split.)
# ---------------------------------------------------------------------------
option(SAMPLEMACHINE_SFIZZ_HAS_MPE
    "Vendored sfizioso engine exposes the MPE public API (noteOnMPE etc.)" ON)

if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
    set(_default_debug_midi_panel ON)
else()
    set(_default_debug_midi_panel OFF)
endif()
option(SAMPLEMACHINE_DEBUG_MIDI_PANEL
    "Compile the realtime MIDI debug panel into the player / rompler editors."
    ${_default_debug_midi_panel})

# ---------------------------------------------------------------------------
# sfzbundle — dependency-free version-2 container format and validated parser.
# The alias is the public target consumed by parent builds.
# ---------------------------------------------------------------------------
add_library(sfizioso_bundle STATIC src/sfzbundle/Parser.cpp)
add_library(sfizioso_player::sfzbundle ALIAS sfizioso_bundle)
target_include_directories(sfizioso_bundle PUBLIC src)
target_compile_features(sfizioso_bundle PUBLIC cxx_std_17)
set_target_properties(sfizioso_bundle PROPERTIES POSITION_INDEPENDENT_CODE TRUE)

# Shared, load-time instrument metadata. No engine or UI dependency.
add_library(instrument_manifest STATIC src/instrument_manifest/Manifest.cpp)
add_library(sfizioso_player::instrument_manifest ALIAS instrument_manifest)
target_include_directories(instrument_manifest PUBLIC src)
target_link_libraries(instrument_manifest PUBLIC juce::juce_core PRIVATE sfizioso_player::sfzbundle)
target_compile_definitions(instrument_manifest PRIVATE JUCE_USE_CURL=0)

# Shared presentation preparation and bridge DTOs; no Player or engine dependency.
add_library(instrument_presentation STATIC src/instrument_presentation/Presentation.cpp)
add_library(sfizioso_player::instrument_presentation ALIAS instrument_presentation)
target_include_directories(instrument_presentation PUBLIC src)
target_compile_definitions(instrument_presentation PRIVATE JUCE_USE_CURL=0)
target_link_libraries(instrument_presentation PUBLIC sfizioso_player::instrument_manifest
    PRIVATE juce::juce_graphics)
set_target_properties(instrument_manifest instrument_presentation PROPERTIES POSITION_INDEPENDENT_CODE TRUE)

# ---------------------------------------------------------------------------
# core_prefs — global user preference store (theme persistence). BSD-2-Clause.
# Consumed by the Sfizioso Player and by the private branded romplers.
# ---------------------------------------------------------------------------
add_library(core_prefs STATIC src/core_prefs/PreferenceStore.cpp)
target_include_directories(core_prefs PUBLIC src/core_prefs)
target_link_libraries(core_prefs PUBLIC juce::juce_core)
target_compile_definitions(core_prefs PRIVATE JUCE_USE_CURL=0)

# ---------------------------------------------------------------------------
# player_core — sfizioso wrapper (APVTS params + MIDI dispatch + renderBlock).
# BSD-2-Clause. Shared by the Sfizioso Player app and the private romplers.
# ---------------------------------------------------------------------------
add_library(player_core STATIC src/player_core/PlayerEngine.cpp)
target_include_directories(player_core PUBLIC src/player_core)
target_link_libraries(player_core
    PUBLIC  juce::juce_audio_processors
    PRIVATE sfizioso::sfizioso)

# player_core uses JUCE audio/plugin primitives only. juce_audio_processors pulls
# juce_gui_extra into this static target on Linux, but the browser backend is
# needed only by the app/editor target; disabling it here avoids requiring GTK /
# WebKit compile flags for the core library itself.
target_compile_definitions(player_core PRIVATE
    JUCE_USE_CURL=0
    JUCE_WEB_BROWSER=0)

if(SAMPLEMACHINE_SFIZZ_HAS_MPE)
    target_compile_definitions(player_core PUBLIC SAMPLEMACHINE_SFIZZ_HAS_MPE=1)
endif()

if(SAMPLEMACHINE_DEBUG_MIDI_PANEL)
    target_sources(player_core PRIVATE src/player_core/DebugMidiPanel.cpp)
    target_compile_definitions(player_core PUBLIC SAMPLEMACHINE_DEBUG_MIDI_PANEL=1)
    target_link_libraries(player_core PUBLIC juce::juce_gui_basics)
endif()

# ---------------------------------------------------------------------------
# Player integration and portable-bundle tests. These default on only for this
# repository's top-level build, so parent consumers do not acquire Catch2 or
# test targets.
# ---------------------------------------------------------------------------
if(SFIZIOSO_PLAYER_TESTS)
    # sfizioso's vendored dependencies force the generic BUILD_TESTING cache
    # option off; explicitly enable CTest for this repository's own target.
    enable_testing()
    include(FetchContent)

    FetchContent_Declare(
        Catch2
        GIT_REPOSITORY https://github.com/catchorg/Catch2.git
        GIT_TAG v3.5.4)
    FetchContent_MakeAvailable(Catch2)

    add_executable(sfizioso_player_tests
        tests/PlayerEngineChannelRoutingTests.cpp
        tests/PlayerEngineProgramChangeTests.cpp
        tests/PortableBundleTests.cpp
        tests/InstrumentManifestTests.cpp
        tests/SfzBundleParserTests.cpp
        src/player/PortableBundle.cpp)
    target_include_directories(sfizioso_player_tests PRIVATE src/player)
    target_link_libraries(sfizioso_player_tests
        PRIVATE
            player_core
            sfizioso_player::sfzbundle
            sfizioso_player::instrument_manifest
            sfizioso::sfizioso
            juce::juce_core
            Catch2::Catch2WithMain)
    target_compile_features(sfizioso_player_tests PRIVATE cxx_std_17)
    target_compile_definitions(sfizioso_player_tests PRIVATE
        JUCE_USE_CURL=0
        JUCE_WEB_BROWSER=0)

    list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
    include(Catch)
    catch_discover_tests(sfizioso_player_tests)
endif()

# Export the shared React UI source dir so consumers' Vite aliases + the
# add_webview_ui() EXTRA_GLOBS can locate it across the submodule boundary.
set(SFIZIOSO_UI_SHARED ${CMAKE_CURRENT_SOURCE_DIR}/src/ui-shared/src
    CACHE PATH "Sfizioso shared React UI source directory")

function(sfizioso_set_wrapper_bundle_id source_target wrapper_kind bundle_id)
    if(NOT APPLE)
        return()
    endif()

    set(wrapper_target "${source_target}_${wrapper_kind}")

    if(TARGET ${wrapper_target})
        set_target_properties(${wrapper_target} PROPERTIES
            XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "${bundle_id}")
    endif()

    get_target_property(generated_sources ${source_target} JUCE_GENERATED_SOURCES_DIRECTORY)
    if(NOT generated_sources)
        message(FATAL_ERROR "JUCE_GENERATED_SOURCES_DIRECTORY is missing for ${source_target}")
    endif()

    set(info_plist "${generated_sources}/${wrapper_target}/Info.plist")
    if(NOT EXISTS "${info_plist}")
        message(FATAL_ERROR "Expected generated Info.plist not found: ${info_plist}")
    endif()

    file(READ "${info_plist}" plist_contents)
    string(REGEX REPLACE
        "(<key>CFBundleIdentifier</key>[ \t\r\n]*<string>)[^<]+(</string>)"
        "\\1${bundle_id}\\2"
        patched_plist
        "${plist_contents}")
    file(WRITE "${info_plist}" "${patched_plist}")
endfunction()

# ---------------------------------------------------------------------------
# Sfizioso Player — the standalone / plugin application. AGPLv3.
# Built only when this repo is the top-level project; consumers take just the
# libraries above.
# ---------------------------------------------------------------------------
if(PROJECT_IS_TOP_LEVEL)
    juce_add_plugin(SfiziosoPlayer
        VERSION ${PROJECT_VERSION}
        COMPANY_NAME "Sfizioso"
        BUNDLE_ID ${SFIZIOSO_PLAYER_PLUGIN_BUNDLE_ID}
        PLUGIN_MANUFACTURER_CODE PtRl
        PLUGIN_CODE Sfzo
        FORMATS VST3 AU Standalone
        PRODUCT_NAME "Sfizioso Player"
        IS_SYNTH TRUE
        NEEDS_MIDI_INPUT TRUE
        NEEDS_MIDI_OUTPUT FALSE
        IS_MIDI_EFFECT FALSE
        EDITOR_WANTS_KEYBOARD_FOCUS FALSE
        NEEDS_WEB_BROWSER TRUE
        NEEDS_WEBVIEW2 TRUE
        COPY_PLUGIN_AFTER_BUILD TRUE)

    # Give every macOS wrapper its own bundle ID. Logic project compatibility is
    # keyed by the AU type/subtype/manufacturer triple (aumu/Sfzo/PtRl), while
    # duplicate CFBundleIdentifier values can make LaunchServices/window
    # restoration activate the standalone app when the AU is restored.
    sfizioso_set_wrapper_bundle_id(SfiziosoPlayer AU ${SFIZIOSO_PLAYER_AU_BUNDLE_ID})
    sfizioso_set_wrapper_bundle_id(SfiziosoPlayer VST3 ${SFIZIOSO_PLAYER_VST3_BUNDLE_ID})
    sfizioso_set_wrapper_bundle_id(SfiziosoPlayer Standalone
        ${SFIZIOSO_PLAYER_STANDALONE_BUNDLE_ID})

    target_sources(SfiziosoPlayer PRIVATE
        src/player/PlayerProcessor.cpp
        src/player/PlayerEditor.cpp
        src/player/PortableBundle.cpp)

    target_include_directories(SfiziosoPlayer PRIVATE src/player)

    target_compile_definitions(SfiziosoPlayer PUBLIC
        SFIZIOSO_PLAYER_RELEASE_VERSION="${SFIZIOSO_PLAYER_RELEASE_VERSION}"
        JUCE_USE_CURL=0
        JUCE_VST3_CAN_REPLACE_VST2=0
        $<$<PLATFORM_ID:Windows>:JUCE_USE_WIN_WEBVIEW2_WITH_STATIC_LINKING=1>)

    target_link_libraries(SfiziosoPlayer
        PRIVATE
            juce::juce_audio_utils
            player_core
            sfizioso_player::sfzbundle
            sfizioso_player::instrument_presentation
            sfizioso::sfizioso
            core_prefs            # SMPL-89 recent-files persistence
        PUBLIC
            juce::juce_recommended_config_flags
            juce::juce_recommended_lto_flags
            juce::juce_recommended_warning_flags)

    # Build the React UI and embed it via juce_add_binary_data.
    add_webview_ui(
        TARGET      Player
        UI_DIR      ${CMAKE_CURRENT_SOURCE_DIR}/src/player/ui
        DATA_TARGET SfiziosoPlayerUiData
        ZIP_NAME    ui_player.zip
        EXTRA_GLOBS ${SFIZIOSO_UI_SHARED}/*.ts
                    ${SFIZIOSO_UI_SHARED}/*.tsx
                    ${SFIZIOSO_UI_SHARED}/*.css)
    target_link_libraries(SfiziosoPlayer PRIVATE SfiziosoPlayerUiData)
    target_compile_definitions(SfiziosoPlayer PUBLIC JUCE_WEB_BROWSER=1)
    if(SFIZIOSO_PLAYER_TESTS)
        # This consumer deliberately has no Player application or engine linkage.
        add_executable(sfizioso_shared_presentation_tests tests/SharedPresentationTests.cpp)
        target_link_libraries(sfizioso_shared_presentation_tests PRIVATE
            sfizioso_player::instrument_presentation Catch2::Catch2WithMain)
        target_compile_definitions(sfizioso_shared_presentation_tests PRIVATE JUCE_USE_CURL=0
            SFIZIOSO_MANIFEST_EXAMPLE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/examples/manifest-instrument")
        catch_discover_tests(sfizioso_shared_presentation_tests)

        # Reuse the compiled application without recompiling JUCE interface sources.
        add_executable(sfizioso_player_presentation_tests tests/PlayerPresentationTests.cpp)
        target_link_libraries(sfizioso_player_presentation_tests PRIVATE
            $<LINK_ONLY:SfiziosoPlayer> Catch2::Catch2WithMain
            juce::juce_recommended_config_flags juce::juce_recommended_lto_flags)
        target_include_directories(sfizioso_player_presentation_tests PRIVATE
            $<TARGET_PROPERTY:SfiziosoPlayer,INCLUDE_DIRECTORIES>)
        target_compile_definitions(sfizioso_player_presentation_tests PRIVATE
            $<TARGET_PROPERTY:SfiziosoPlayer,COMPILE_DEFINITIONS>
            SFIZIOSO_MANIFEST_EXAMPLE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/examples/manifest-instrument")
        # Xcode 15's new linker fails with "invalid use of branch fixup"
        # when this test links the application's LTO archive. Keep the workaround
        # limited to the affected test/toolchain; plugin link settings are unchanged.
        if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang"
           AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 16)
            target_link_options(sfizioso_player_presentation_tests PRIVATE -Wl,-ld_classic)
        endif()
        catch_discover_tests(sfizioso_player_presentation_tests)
    endif()
endif()
