# SPDX-License-Identifier: GPL-3.0-only
add_library(bmh_modern_ui_core STATIC catalog.cpp crest_contrast.cpp engine_dto.cpp engine_live.cpp engine_statistics.cpp engine_toto.cpp engine_stadium.cpp engine_transfer.cpp engine_achievements.cpp engine_news.cpp engine_balance.cpp engine_widgets.cpp engine_indoor.cpp engine_settings.cpp engine_managers.cpp model.cpp demo_career.cpp demo_national_ui.cpp gui_assets.cpp desktop.cpp demo_sports.cpp demo_calendar.cpp demo_reports.cpp demo_almanac.cpp demo_finance.cpp demo_environment.cpp demo_advertising.cpp demo_toto.cpp demo_lineup.cpp demo_live.cpp demo_post_match.cpp demo_transfer.cpp demo_career_reports.cpp demo_rankings.cpp demo_stadium.cpp demo_statistics.cpp demo_news.cpp news_catalog.cpp news_journal.cpp demo_settings.cpp ui_preferences.cpp demo_widgets.cpp demo_files.cpp career_files.cpp demo_opening.cpp)
target_include_directories(bmh_modern_ui_core PUBLIC ../..)
target_link_libraries(bmh_modern_ui_core PUBLIC bmh_asset_io)
add_library(bmh_modern_ui_mods STATIC mod_library.cpp)
target_link_libraries(bmh_modern_ui_mods PUBLIC bmh_modern_ui_core bmh_launcher)
add_executable(modern_mods_ui_tests ../../../tests/modern_mods_ui_tests.cpp)
target_link_libraries(modern_mods_ui_tests PRIVATE bmh_modern_ui_mods)
if(MSVC)
    target_compile_options(bmh_modern_ui_mods PRIVATE /W4 /utf-8)
    target_compile_options(modern_mods_ui_tests PRIVATE /W4 /utf-8)
else()
    target_compile_options(bmh_modern_ui_mods PRIVATE -Wall -Wextra -Wpedantic)
endif()
add_test(NAME modern_mods_ui_tests COMMAND modern_mods_ui_tests)
add_library(bmh_modern_ui_select_menu STATIC select_menu.cpp)
target_link_libraries(bmh_modern_ui_select_menu PUBLIC bmh_modern_ui_core RmlUi::Core)
add_executable(modern_select_menu_tests ../../../tests/modern_select_menu_tests.cpp)
target_link_libraries(modern_select_menu_tests PRIVATE bmh_modern_ui_select_menu)
if(MSVC)
    target_compile_options(bmh_modern_ui_select_menu PRIVATE /W4 /utf-8)
    target_compile_options(modern_select_menu_tests PRIVATE /W4 /utf-8)
else()
    target_compile_options(bmh_modern_ui_select_menu PRIVATE -Wall -Wextra -Wpedantic)
    target_compile_options(modern_select_menu_tests PRIVATE -Wall -Wextra -Wpedantic)
endif()
add_test(NAME modern_select_menu_tests COMMAND modern_select_menu_tests "${CMAKE_BINARY_DIR}/assets/modern")
find_package(Threads REQUIRED)
add_library(bmh_modern_ui_engine STATIC engine_bridge.cpp)
target_link_libraries(bmh_modern_ui_engine PUBLIC bmh_modern_ui_core PRIVATE bmh_modern_session)
if(MSVC)
    target_compile_options(bmh_modern_ui_engine PRIVATE /W4 /utf-8 /bigobj)
else()
    target_compile_options(bmh_modern_ui_engine PRIVATE -Wall -Wextra)
endif()
add_library(bmh_modern_ui STATIC view.cpp ticket_popover.cpp)
target_link_libraries(bmh_modern_ui PUBLIC bmh_modern_ui_core PRIVATE Threads::Threads bmh_modern_ui_engine bmh_modern_ui_mods bmh_modern_ui_select_menu bmh_rmlui bmh_launcher nfd::nfd ${BMH_SDL_TARGET})
if(MSVC)
    target_compile_options(bmh_modern_ui_core PRIVATE /W4 /utf-8)
    target_compile_options(bmh_modern_ui PRIVATE /W4 /utf-8 /bigobj)
else()
    target_compile_options(bmh_modern_ui_core PRIVATE -Wall -Wextra -Wpedantic)
    target_compile_options(bmh_modern_ui PRIVATE -Wall -Wextra -Wpedantic)
endif()
set(modern_assets "${CMAKE_BINARY_DIR}/assets/modern")
FetchContent_GetProperties(rmlui)
file(MAKE_DIRECTORY "${modern_assets}")
foreach(asset modern.rml theme.rcss ticket.rcss sports.rcss calendar.rcss reports.rcss report_details.rcss report_pickers.rcss indoor.rcss planning.rcss almanac.rcss finance.rcss bank.rcss contracts.rcss amount.rcss environment.rcss advertising.rcss toto.rcss lineup.rcss live.rcss transfer.rcss career_reports.rcss rankings.rcss stadium.rcss statistics.rcss news.rcss news-catalog.json settings.rcss widgets.rcss files.rcss front.rcss entry.rcss opening.rcss select_menu.rcss career.rcss national.rcss postmatch.rcss notebook.rcss pages.rcss)
    configure_file("${CMAKE_SOURCE_DIR}/assets/modern/${asset}" "${modern_assets}/${asset}" COPYONLY)
endforeach()

foreach(component career_reports rankings career_files statistics_projection toto_projection stadium_projection transfer_projection achievements_projection news_projection balance_projection widgets_projection indoor_projection settings_projection)
    add_executable(modern_${component}_tests ../../../tests/modern_${component}_tests.cpp)
    target_link_libraries(modern_${component}_tests PRIVATE bmh_modern_ui_core)
    if(MSVC)
        target_compile_options(modern_${component}_tests PRIVATE /W4 /utf-8)
    else()
        target_compile_options(modern_${component}_tests PRIVATE -Wall -Wextra -Wpedantic)
    endif()
    if(component STREQUAL "news_projection")
        add_test(NAME modern_${component}_tests COMMAND modern_${component}_tests "${CMAKE_SOURCE_DIR}/assets/modern/news-catalog.json")
    else()
        add_test(NAME modern_${component}_tests COMMAND modern_${component}_tests)
    endif()
endforeach()
file(GLOB modern_icons CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/assets/modern/icons/*.svg")
file(MAKE_DIRECTORY "${modern_assets}/icons")
foreach(icon ${modern_icons})
    get_filename_component(icon_name "${icon}" NAME)
    configure_file("${icon}" "${modern_assets}/icons/${icon_name}" COPYONLY)
endforeach()
# the project's own pictures with their catalogue
file(GLOB modern_art CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/assets/modern/art/*")
file(MAKE_DIRECTORY "${modern_assets}/art")
foreach(art ${modern_art})
    get_filename_component(art_name "${art}" NAME)
    configure_file("${art}" "${modern_assets}/art/${art_name}" COPYONLY)
endforeach()
# the display serif and the chalk hand (SIL Open Font License, THIRD_PARTY_NOTICES.md)
file(GLOB modern_fonts CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/assets/modern/fonts/*")
file(MAKE_DIRECTORY "${modern_assets}/fonts")
foreach(font ${modern_fonts})
    get_filename_component(font_name "${font}" NAME)
    configure_file("${font}" "${modern_assets}/fonts/${font_name}" COPYONLY)
endforeach()
foreach(weight Regular Bold Italic BoldItalic)
    configure_file("${rmlui_SOURCE_DIR}/Samples/assets/LatoLatin-${weight}.ttf"
        "${modern_assets}/LatoLatin-${weight}.ttf" COPYONLY)
endforeach()
set(BMH_MODERN_GUI_ASSETS "" CACHE PATH "Optional externally supplied Modern GUI resource directory")
if(BMH_MODERN_GUI_ASSETS)
    if(NOT EXISTS "${BMH_MODERN_GUI_ASSETS}/gui-assets.json")
        message(FATAL_ERROR "BMH_MODERN_GUI_ASSETS does not contain gui-assets.json")
    endif()
    file(COPY "${BMH_MODERN_GUI_ASSETS}/" DESTINATION "${modern_assets}/gui")
endif()
install(DIRECTORY "${modern_assets}/" DESTINATION assets/modern COMPONENT Runtime)
add_executable(modern_ui_core_tests ../../../tests/modern_ui_core_tests.cpp)
target_link_libraries(modern_ui_core_tests PRIVATE bmh_modern_ui_core)
if(MSVC)
    target_compile_options(modern_ui_core_tests PRIVATE /utf-8)
endif()
add_test(NAME modern_ui_core_tests COMMAND modern_ui_core_tests)
add_executable(modern_planning_tests ../../../tests/modern_planning_tests.cpp)
target_link_libraries(modern_planning_tests PRIVATE bmh_modern_ui_core)
if(MSVC)
    target_compile_options(modern_planning_tests PRIVATE /utf-8)
endif()
add_test(NAME modern_planning_tests COMMAND modern_planning_tests)
add_executable(modern_almanac_ui_tests ../../../tests/modern_almanac_ui_tests.cpp)
target_link_libraries(modern_almanac_ui_tests PRIVATE bmh_modern_ui_core)
if(MSVC)
    target_compile_options(modern_almanac_ui_tests PRIVATE /utf-8)
endif()
add_test(NAME modern_almanac_ui_tests COMMAND modern_almanac_ui_tests)
add_executable(modern_finance_ui_tests ../../../tests/modern_finance_ui_tests.cpp)
target_link_libraries(modern_finance_ui_tests PRIVATE bmh_modern_ui_core)
if(MSVC)
    target_compile_options(modern_finance_ui_tests PRIVATE /utf-8)
endif()
add_test(NAME modern_finance_ui_tests COMMAND modern_finance_ui_tests)
add_executable(modern_reports_tests ../../../tests/modern_reports_tests.cpp)
target_link_libraries(modern_reports_tests PRIVATE bmh_modern_ui_core)
if(MSVC)
    target_compile_options(modern_reports_tests PRIVATE /utf-8)
endif()
add_test(NAME modern_reports_tests COMMAND modern_reports_tests)
add_executable(modern_ticket_document_tests ../../../tests/modern_ticket_document_tests.cpp ticket_popover.cpp)
target_link_libraries(modern_ticket_document_tests PRIVATE bmh_modern_ui_core RmlUi::Core)
if(MSVC)
    target_compile_options(modern_ticket_document_tests PRIVATE /utf-8)
endif()
foreach(theme midnight silver matchday)
    add_test(NAME modern_ticket_${theme} COMMAND modern_ticket_document_tests "${modern_assets}" ${theme})
endforeach()
add_executable(modern_control_layout_tests ../../../tests/modern_control_layout_tests.cpp)
target_link_libraries(modern_control_layout_tests PRIVATE bmh_modern_ui_core RmlUi::Core)
if(MSVC)
    target_compile_options(modern_control_layout_tests PRIVATE /utf-8)
endif()
add_test(NAME modern_control_layout_tests COMMAND modern_control_layout_tests "${modern_assets}")

foreach(component environment advertising toto lineup transfer stadium)
    add_executable(modern_${component}_ui_tests ../../../tests/modern_${component}_ui_tests.cpp)
    target_link_libraries(modern_${component}_ui_tests PRIVATE bmh_modern_ui_core)
    if(MSVC)
        target_compile_options(modern_${component}_ui_tests PRIVATE /utf-8)
    endif()
    add_test(NAME modern_${component}_ui_tests COMMAND modern_${component}_ui_tests)
endforeach()

foreach(component statistics news settings desktop files entry opening career live national post_match)
    add_executable(modern_${component}_ui_tests ../../../tests/modern_${component}_ui_tests.cpp)
    target_link_libraries(modern_${component}_ui_tests PRIVATE bmh_modern_ui_core)
    if(MSVC)
        target_compile_options(modern_${component}_ui_tests PRIVATE /utf-8)
    endif()
    if(component STREQUAL "post_match")
        add_test(NAME modern_${component}_ui_tests COMMAND modern_${component}_ui_tests "${CMAKE_SOURCE_DIR}/tests/synthetic/modern_post_match_eleven.json")
    else()
        add_test(NAME modern_${component}_ui_tests COMMAND modern_${component}_ui_tests)
    endif()
endforeach()

add_executable(modern_news_catalog_tests ../../../tests/modern_news_catalog_tests.cpp)
target_link_libraries(modern_news_catalog_tests PRIVATE bmh_modern_ui_core)
if(MSVC)
    target_compile_options(modern_news_catalog_tests PRIVATE /utf-8)
endif()
add_test(NAME modern_news_catalog_tests COMMAND modern_news_catalog_tests "${CMAKE_SOURCE_DIR}/assets/modern/news-catalog.json" "${CMAKE_SOURCE_DIR}/tests/synthetic/modern_news_catalog_reference.json")

# Pure DTO tests need no proprietary content; the opt-in probe accepts a user's world.
add_executable(modern_engine_dto_tests ../../../tests/modern_engine_dto_tests.cpp)
target_link_libraries(modern_engine_dto_tests PRIVATE bmh_modern_ui_core)
add_test(NAME modern_engine_dto_tests COMMAND modern_engine_dto_tests)
add_executable(modern_engine_bridge_probe ../../../tests/modern_engine_bridge_probe.cpp)
target_link_libraries(modern_engine_bridge_probe PRIVATE bmh_modern_ui_engine bmh_modern_session)
add_executable(modern_live_dump ../../../tests/modern_live_dump.cpp)
target_link_libraries(modern_live_dump PRIVATE bmh_modern_ui_engine bmh_modern_session)
if(MSVC)
    target_compile_options(modern_engine_dto_tests PRIVATE /utf-8)
    target_compile_options(modern_engine_bridge_probe PRIVATE /utf-8 /bigobj)
    target_compile_options(modern_live_dump PRIVATE /utf-8 /bigobj)
endif()

add_executable(modern_managers_projection_tests "${CMAKE_SOURCE_DIR}/tests/modern_managers_projection_tests.cpp")
target_link_libraries(modern_managers_projection_tests PRIVATE bmh_modern_ui_core)
add_test(NAME modern_managers_projection_tests COMMAND modern_managers_projection_tests)
