set(Renderive_Web_dependencies global::drogon global::spdlog global::magic_enum)
if (RENDERIVE_BUILD_TESTS)
    list(APPEND Renderive_Web_dependencies global::GTest)
endif ()
rcl_add_dependency_action_targets(Renderive_Web_env ${Renderive_Web_dependencies})
set_target_properties(Renderive_Web_env PROPERTIES FOLDER Renderive_Web)
if (CMAKE_CONFIGURATION_TYPES)
    set(Renderive_Web_assets_dir "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/webapp_gallery")
else ()
    set(Renderive_Web_assets_dir "${CMAKE_CURRENT_BINARY_DIR}/webapp_gallery")
endif ()
set(Renderive_Web_node_search_paths)
if (WIN32 AND DEFINED ENV{APPDATA})
    file(GLOB Renderive_Web_clion_node_versions LIST_DIRECTORIES true
            "$ENV{APPDATA}/JetBrains/CLion*/node/versions/*")
    list(SORT Renderive_Web_clion_node_versions COMPARE NATURAL ORDER DESCENDING)
    foreach (Renderive_Web_clion_node_version IN LISTS Renderive_Web_clion_node_versions)
        if (EXISTS "${Renderive_Web_clion_node_version}/node.exe"
                AND EXISTS "${Renderive_Web_clion_node_version}/npm.cmd")
            list(APPEND Renderive_Web_node_search_paths "${Renderive_Web_clion_node_version}")
        endif ()
    endforeach ()
endif ()
find_program(Renderive_Web_node_executable NAMES node node.exe
        HINTS ${Renderive_Web_node_search_paths} REQUIRED)
get_filename_component(Renderive_Web_node_dir "${Renderive_Web_node_executable}" DIRECTORY)
if (WIN32)
    set(Renderive_Web_npm_executable "${Renderive_Web_node_dir}/npm.cmd")
else ()
    set(Renderive_Web_npm_executable "${Renderive_Web_node_dir}/npm")
endif ()
if (NOT EXISTS "${Renderive_Web_npm_executable}")
    message(FATAL_ERROR "npm was not found next to Node.js: ${Renderive_Web_node_dir}")
endif ()
set(Renderive_Web_frontend_dir "${CMAKE_CURRENT_LIST_DIR}/../webapp_gallery")
set(Renderive_Web_frontend_install_stamp "${CMAKE_CURRENT_BINARY_DIR}/webapp_gallery_npm_ci.stamp")
set(Renderive_Web_frontend_build_stamp "${CMAKE_CURRENT_BINARY_DIR}/webapp_gallery_vite_build.stamp")
file(GLOB_RECURSE Renderive_Web_frontend_sources CONFIGURE_DEPENDS
        "${Renderive_Web_frontend_dir}/src/*"
)
set(Renderive_Web_frontend_configuration
        "${Renderive_Web_frontend_dir}/package.json"
        "${Renderive_Web_frontend_dir}/package-lock.json"
        "${Renderive_Web_frontend_dir}/vite.config.ts"
        "${Renderive_Web_frontend_dir}/tsconfig.json"
        "${Renderive_Web_frontend_dir}/tsconfig.app.json"
        "${Renderive_Web_frontend_dir}/index.html"
)
if (WIN32)
    set(Renderive_Web_node_path "${Renderive_Web_node_dir};$ENV{PATH}")
else ()
    set(Renderive_Web_node_path "${Renderive_Web_node_dir}:$ENV{PATH}")
endif ()
add_custom_command(
        OUTPUT "${Renderive_Web_frontend_install_stamp}"
        COMMAND "${CMAKE_COMMAND}" -E env
        "PATH=${Renderive_Web_node_path}"
        "${Renderive_Web_npm_executable}" ci
        COMMAND "${CMAKE_COMMAND}" -E touch "${Renderive_Web_frontend_install_stamp}"
        DEPENDS
        "${Renderive_Web_frontend_dir}/package.json"
        "${Renderive_Web_frontend_dir}/package-lock.json"
        WORKING_DIRECTORY "${Renderive_Web_frontend_dir}"
        COMMENT "Installing Renderive Gallery frontend dependencies"
        VERBATIM
)
add_custom_command(
        OUTPUT "${Renderive_Web_frontend_build_stamp}"
        COMMAND "${CMAKE_COMMAND}" -E env
        "PATH=${Renderive_Web_node_path}"
        "${Renderive_Web_npm_executable}" run build
        COMMAND "${CMAKE_COMMAND}" -E touch "${Renderive_Web_frontend_build_stamp}"
        DEPENDS "${Renderive_Web_frontend_install_stamp}"
        ${Renderive_Web_frontend_configuration}
        ${Renderive_Web_frontend_sources}
        WORKING_DIRECTORY "${Renderive_Web_frontend_dir}"
        COMMENT "Building Renderive Gallery frontend"
        VERBATIM
)
add_custom_target(Renderive_Web_Assets
        COMMAND "${CMAKE_COMMAND}" -E remove_directory "${Renderive_Web_assets_dir}"
        COMMAND "${CMAKE_COMMAND}" -E copy_directory
        "${Renderive_Web_frontend_dir}/dist"
        "${Renderive_Web_assets_dir}"
        DEPENDS "${Renderive_Web_frontend_build_stamp}"
        COMMENT "Synchronizing Renderive Gallery frontend assets"
        VERBATIM
)
library_is_installed_with_rely(Renderive_Web_dependencies_installed ${Renderive_Web_dependencies})
if (NOT Renderive_Web_dependencies_installed)
    rcl_log_append("[FATAL_ERROR] Renderive_Web dependencies are not installed. Build Renderive_Web_env first")
    return()
endif ()
function(renderive_find_web_dependencies)
    if (POLICY CMP0144)
        cmake_policy(SET CMP0144 NEW)
    endif ()
    rcl_load_dependency_environment(${Renderive_Web_dependencies})
    find_package(Drogon CONFIG REQUIRED)
    find_package(spdlog CONFIG REQUIRED)
    find_package(magic_enum CONFIG REQUIRED)
    if (RENDERIVE_BUILD_TESTS)
        find_package(GTest CONFIG REQUIRED)
    endif ()
endfunction()
renderive_find_web_dependencies()
if (NOT TARGET Renderive_render_2D)
    rcl_log_append("[FATAL_ERROR] Renderive_Web requires Renderive_render_2D")
    return()
endif ()
if (NOT TARGET Adminive::Nlohmann)
    set(Renderive_Web_saved_build_testing "${BUILD_TESTING}")
    set(BUILD_TESTING OFF)
    add_subdirectory(
            "${CMAKE_CURRENT_LIST_DIR}/../third_party/Adminive"
            "${CMAKE_CURRENT_BINARY_DIR}/third_party/Adminive"
            EXCLUDE_FROM_ALL
    )
    set(BUILD_TESTING "${Renderive_Web_saved_build_testing}")
    unset(Renderive_Web_saved_build_testing)
endif ()
set(Renderive_Web_source_dir "${CMAKE_CURRENT_LIST_DIR}/app")
append_glob_source(Renderive_Web_sources "${Renderive_Web_source_dir}")
add_library(Renderive_Web STATIC ${Renderive_Web_sources})
target_include_directories(Renderive_Web PUBLIC
        "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/..>"
)
target_compile_features(Renderive_Web PUBLIC cxx_std_20)
target_compile_definitions(Renderive_Web PRIVATE NOMINMAX)
target_link_libraries(Renderive_Web
        PUBLIC Renderive_render_2D Drogon::Drogon Adminive::MagicEnum magic_enum::magic_enum
        PRIVATE Adminive::Nlohmann spdlog::spdlog
)
set(Renderive_Web_Server_source_dir "${CMAKE_CURRENT_LIST_DIR}/server")
append_glob_source(Renderive_Web_Server_sources "${Renderive_Web_Server_source_dir}")
add_executable(Renderive_Web_Server ${Renderive_Web_Server_sources})
target_compile_features(Renderive_Web_Server PRIVATE cxx_std_20)
target_link_libraries(Renderive_Web_Server PRIVATE Renderive_Web)
#add_dependencies(Renderive_Web_Server Renderive_Web_Assets)
if (MSVC)
    target_compile_options(Renderive_Web PRIVATE /utf-8)
    target_compile_options(Renderive_Web_Server PRIVATE /utf-8)
endif ()
if (RENDERIVE_BUILD_TESTS)
    set(Renderive_Web_test_dir "${CMAKE_CURRENT_LIST_DIR}/tests")
    append_glob_source(Renderive_Web_test_sources "${Renderive_Web_test_dir}")
    foreach (Renderive_Web_test_source IN LISTS Renderive_Web_test_sources)
        if (NOT Renderive_Web_test_source MATCHES "\\.(c|cc|cpp|cxx)$")
            continue()
        endif ()
        file(RELATIVE_PATH Renderive_Web_test_name "${Renderive_Web_test_dir}" "${Renderive_Web_test_source}")
        string(MD5 Renderive_Web_test_hash "${Renderive_Web_test_name}")
        string(SUBSTRING "${Renderive_Web_test_hash}" 0 8 Renderive_Web_test_hash)
        get_filename_component(Renderive_Web_test_name "${Renderive_Web_test_source}" NAME_WE)
        string(MAKE_C_IDENTIFIER "${Renderive_Web_test_name}" Renderive_Web_test_name)
        set(Renderive_Web_test_target "Renderive_Web_${Renderive_Web_test_name}_${Renderive_Web_test_hash}")
        add_executable("${Renderive_Web_test_target}" "${Renderive_Web_test_source}")
        target_link_libraries("${Renderive_Web_test_target}" PRIVATE Renderive_Web Adminive::Nlohmann GTest::gtest_main)
        add_test(NAME "${Renderive_Web_test_target}" COMMAND "${Renderive_Web_test_target}")
    endforeach ()
endif ()