# SPDX-License-Identifier: GPL-3.0-only
include(FetchContent)
set(OPENBMH_EDITOR_VERSION "0.5.4")
file(WRITE "${CMAKE_BINARY_DIR}/editor-version.json" "{\"version\": \"${OPENBMH_EDITOR_VERSION}\"}\n")
set(JSON_BuildTests OFF CACHE BOOL "" FORCE)
FetchContent_Declare(editor_json GIT_REPOSITORY https://github.com/nlohmann/json.git
    GIT_TAG v3.12.0 GIT_SHALLOW TRUE)
FetchContent_Declare(editor_http GIT_REPOSITORY https://github.com/yhirose/cpp-httplib.git
    GIT_TAG v0.63.0 GIT_SHALLOW TRUE SOURCE_SUBDIR no-cmake)
set(SDL2IMAGE_INSTALL OFF CACHE BOOL "" FORCE)
set(SDL2IMAGE_SAMPLES OFF CACHE BOOL "" FORCE)
set(SDL2IMAGE_TESTS OFF CACHE BOOL "" FORCE)
set(SDL2IMAGE_VENDORED ON CACHE BOOL "" FORCE)
set(SDL2IMAGE_DEPS_SHARED OFF CACHE BOOL "" FORCE)
set(SDL2IMAGE_BACKEND_STB ON CACHE BOOL "" FORCE)
set(SDL2IMAGE_BACKEND_IMAGEIO OFF CACHE BOOL "" FORCE)
set(SDL2IMAGE_BACKEND_WIC OFF CACHE BOOL "" FORCE)
foreach(codec AVIF BMP GIF JXL LBM PCX PNM QOI SVG TIF XCF XPM XV)
    set(SDL2IMAGE_${codec} OFF CACHE BOOL "" FORCE)
endforeach()
set(SDL2IMAGE_PNG ON CACHE BOOL "" FORCE)
set(SDL2IMAGE_JPG ON CACHE BOOL "" FORCE)
set(SDL2IMAGE_WEBP ON CACHE BOOL "" FORCE)
if(MSVC)
    set(WEBP_ENABLE_SIMD ON CACHE BOOL "Use WebP's runtime CPU selection" FORCE)
endif()
set(SDL2IMAGE_PNG_SAVE OFF CACHE BOOL "" FORCE)
set(SDL2IMAGE_JPG_SAVE OFF CACHE BOOL "" FORCE)
FetchContent_Declare(editor_images GIT_REPOSITORY https://github.com/libsdl-org/SDL_image.git
    GIT_TAG release-2.8.8 GIT_SHALLOW TRUE GIT_SUBMODULES external/libwebp)
set(LUNASVG_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
FetchContent_Declare(editor_svg GIT_REPOSITORY https://github.com/sammycage/lunasvg.git
    GIT_TAG v3.5.0 GIT_SHALLOW TRUE)
set(PUGIXML_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(PUGIXML_BUILD_SHARED_AND_STATIC_LIBS OFF CACHE BOOL "" FORCE)
FetchContent_Declare(editor_xml GIT_REPOSITORY https://github.com/zeux/pugixml.git
    GIT_TAG v1.15 GIT_SHALLOW TRUE)
FetchContent_MakeAvailable(editor_json editor_http editor_images editor_svg editor_xml)
if(MSVC)
    # The pinned WebP CMake propagates /arch:AVX into SSE2 source files, even
    # though their runtime gate only checks SSE2. Compile its explicit SSE
    # intrinsics at the default x64 baseline, preserving CPU dispatch and all
    # upstream sources. Disabling SIMD is broken in this pinned MSVC build.
    get_target_property(webp_source_directory webpdsp SOURCE_DIR)
    get_target_property(webp_dsp_sources webpdsp SOURCES)
    foreach(source IN LISTS webp_dsp_sources)
        if(source MATCHES "_sse(2|41)\\.c$")
            set_source_files_properties("${source}" DIRECTORY "${webp_source_directory}"
                PROPERTIES COMPILE_FLAGS "")
        endif()
    endforeach()
endif()
file(WRITE "${CMAKE_BINARY_DIR}/editor-dependencies.json" "{\n")
foreach(component editor_json editor_http editor_images editor_svg editor_xml)
    file(TO_CMAKE_PATH "${${component}_SOURCE_DIR}" component_path)
    file(APPEND "${CMAKE_BINARY_DIR}/editor-dependencies.json" "\"${component}\": \"${component_path}\"")
    if(NOT component STREQUAL "editor_xml")
        file(APPEND "${CMAKE_BINARY_DIR}/editor-dependencies.json" ",")
    endif()
    file(APPEND "${CMAKE_BINARY_DIR}/editor-dependencies.json" "\n")
endforeach()
file(APPEND "${CMAKE_BINARY_DIR}/editor-dependencies.json" "}\n")

add_library(bmh_editor_core STATIC archive.cpp world.cpp session.cpp comparison.cpp regulations.cpp images.cpp options.cpp mcp.cpp)
target_compile_definitions(bmh_editor_core PUBLIC OPENBMH_EDITOR_VERSION="${OPENBMH_EDITOR_VERSION}")
target_include_directories(bmh_editor_core PUBLIC .. ${editor_http_SOURCE_DIR})
target_link_libraries(bmh_editor_core PUBLIC nlohmann_json::nlohmann_json bmh_content
    PRIVATE bmh_miniz ${BMH_SDL_TARGET} SDL2_image::SDL2_image-static lunasvg::lunasvg pugixml::pugixml)
find_package(Threads REQUIRED)
target_link_libraries(bmh_editor_core PUBLIC Threads::Threads)
if(WIN32)
    target_link_libraries(bmh_editor_core PRIVATE ws2_32 crypt32)
endif()
add_executable(openbmh_editor main.cpp ui.cpp)
set_target_properties(openbmh_editor PROPERTIES OUTPUT_NAME OpenBMHEditor
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
target_link_libraries(openbmh_editor PRIVATE bmh_editor_core bmh_rmlui nfd::nfd ${BMH_SDL_TARGET})
if(MSVC)
    target_compile_options(bmh_editor_core PRIVATE /W4 /utf-8)
    target_compile_options(openbmh_editor PRIVATE /utf-8)
else()
    target_compile_options(bmh_editor_core PRIVATE -Wall -Wextra -Wpedantic)
endif()
if(WIN32)
    set_target_properties(openbmh_editor PROPERTIES WIN32_EXECUTABLE TRUE)
endif()
set(editor_assets "${CMAKE_BINARY_DIR}/assets/editor")
FetchContent_GetProperties(rmlui)
file(MAKE_DIRECTORY "${editor_assets}")
foreach(asset editor.rml theme.rcss)
    configure_file("${CMAKE_SOURCE_DIR}/assets/editor/${asset}" "${editor_assets}/${asset}" COPYONLY)
endforeach()
foreach(weight Regular Bold)
    configure_file("${rmlui_SOURCE_DIR}/Samples/assets/LatoLatin-${weight}.ttf"
        "${editor_assets}/LatoLatin-${weight}.ttf" COPYONLY)
endforeach()
install(TARGETS openbmh_editor RUNTIME DESTINATION . COMPONENT Editor)
install(DIRECTORY "${editor_assets}/" DESTINATION assets/editor COMPONENT Editor)
add_executable(editor_core_tests ../../tests/editor_core_tests.cpp)
target_link_libraries(editor_core_tests PRIVATE bmh_editor_core)
if(MSVC)
    target_compile_options(editor_core_tests PRIVATE /utf-8)
endif()
add_test(NAME editor_core_tests COMMAND editor_core_tests)
add_executable(editor_comparison_tests ../../tests/editor_comparison_tests.cpp)
target_link_libraries(editor_comparison_tests PRIVATE bmh_editor_core)
if(MSVC)
    target_compile_options(editor_comparison_tests PRIVATE /utf-8)
endif()
add_test(NAME editor_comparison_tests COMMAND editor_comparison_tests)
add_executable(editor_regulations_tests ../../tests/editor_regulations_tests.cpp)
target_link_libraries(editor_regulations_tests PRIVATE bmh_editor_core)
if(MSVC)
    target_compile_options(editor_regulations_tests PRIVATE /utf-8)
endif()
add_test(NAME editor_regulations_tests COMMAND editor_regulations_tests)
add_test(NAME editor_version COMMAND openbmh_editor --version)
set_tests_properties(editor_version PROPERTIES PASS_REGULAR_EXPRESSION "OpenBMH Editor ${OPENBMH_EDITOR_VERSION}")
