# ---- launcher interface ---------------------------------------------
# The fonts and the motifs are compiled into the binary by a CMake script, run
# on the building machine whatever the target (Android's too), so that
# the-oracles-project.exe needs no file beside it; the SDL-free half (fonts, motifs,
# transitions, layout) is a library of its own, for the tests.
set(ORACLES_THIRD_PARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../third_party)
set(ORACLES_UI_ASSETS
    ${ORACLES_THIRD_PARTY_DIR}/fonts/marcellus/Marcellus-Regular.ttf
    ${ORACLES_THIRD_PARTY_DIR}/fonts/alegreya-sans/AlegreyaSans-Regular.ttf
    ${ORACLES_THIRD_PARTY_DIR}/fonts/alegreya-sans/AlegreyaSans-Medium.ttf
    ${ORACLES_THIRD_PARTY_DIR}/fonts/alegreya-sans/AlegreyaSans-Italic.ttf
    ${ORACLES_THIRD_PARTY_DIR}/fonts/jetbrains-mono/JetBrainsMono-Regular.ttf
    ${CMAKE_CURRENT_SOURCE_DIR}/ui/motifs/ages.svg
    ${CMAKE_CURRENT_SOURCE_DIR}/ui/motifs/seasons.svg
    ${CMAKE_CURRENT_SOURCE_DIR}/ui/motifs/fan.svg)
# NAME=PATH pairs joined by "|", which the script turns back into a list: a ";" would split the command's argument.
set(ORACLES_UI_ASSET_ARGUMENTS "")
foreach(asset ${ORACLES_UI_ASSETS})
    get_filename_component(asset_name ${asset} NAME)
    string(APPEND ORACLES_UI_ASSET_ARGUMENTS "${asset_name}=${asset}|")
endforeach()
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/oracles_ui_assets.c
    COMMAND ${CMAKE_COMMAND} -DOUT=${CMAKE_CURRENT_BINARY_DIR}/oracles_ui_assets.c "-DASSETS=${ORACLES_UI_ASSET_ARGUMENTS}"
            -P ${CMAKE_CURRENT_SOURCE_DIR}/ui/ui_embed.cmake
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ui/ui_embed.cmake ${ORACLES_UI_ASSETS}
    COMMENT "Embedding the launcher's fonts and motifs"
    VERBATIM)
add_library(oracles_ui STATIC ui/ui_font.c ui/ui_gsub.c ui/ui_anim.c ui/ui_motif.c
    ui/ui_assets.c ui/ui_impl.c ui/ui_layout.c ui/ui_home_nav.c ui/ui_home_layout.c
    ui/ui_page_nav.c ui/ui_page_display.c ui/ui_page_text.c ui/ui_page_text_layout.c ui/ui_page_mods.c ui/ui_page_cheats.c ui/ui_page_layout.c ui/ui_page_save_layout.c ui/ui_page_display_layout.c ui/ui_page_mods_layout.c ui/ui_controls_nav.c ui/ui_controls_layout.c
    ${CMAKE_CURRENT_BINARY_DIR}/oracles_ui_assets.c icon/window_icon.c)
# The application's icon (launcher/icon/, generated by tools/make_icons.py): the window's, in RGBA.
target_include_directories(oracles_ui PUBLIC ui icon)
# Display's sizes are the Enhanced view's (compositor.h, header only).
target_include_directories(oracles_ui PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../engine/enhanced)
target_include_directories(oracles_ui SYSTEM PUBLIC ${ORACLES_THIRD_PARTY_DIR}/stb ${ORACLES_THIRD_PARTY_DIR}/nanosvg)
oracles_set_warnings(oracles_ui)
if(NOT MSVC)
    # The vendored implementations are compiled as upstream ships them; their warnings are not ours.
    set_source_files_properties(ui/ui_impl.c PROPERTIES COMPILE_OPTIONS "-w")
endif()
if(NOT WIN32)
    target_link_libraries(oracles_ui PUBLIC m)
endif()

# ---- launcher ---------------------------------------------------------------
if(ORACLES_BUILD_LAUNCHER)
    # The launcher's drawing on SDL's renderer.
    add_library(oracles_ui_sdl STATIC ui/ui_draw.c ui/ui_home.c ui/ui_page.c ui/ui_controls.c)
    target_link_libraries(oracles_ui_sdl PUBLIC oracles_ui ${ORACLES_SDL_TARGET})
    target_compile_definitions(oracles_ui_sdl PUBLIC "ORACLES_VERSION=\"${ORACLES_VERSION}\"" "ORACLES_BUILD=\"${ORACLES_BUILD}\"")
    oracles_set_warnings(oracles_ui_sdl)
    # The update check (update_check.h), with each platform's HTTP client: WinHTTP, NSURLSession, the system's libcurl
    # loaded at run time (nothing linked), Android's through the activity.
    if(APPLE)
        add_library(oracles_update STATIC update_check.c macos/update_fetch.m)
        target_link_libraries(oracles_update PRIVATE "-framework Foundation")
        # -Wpedantic holds the C sources to ISO C, which Objective-C is not.
        set_source_files_properties(macos/update_fetch.m PROPERTIES COMPILE_OPTIONS "-Wno-pedantic")
    else()
        add_library(oracles_update STATIC update_check.c update_fetch.c)
    endif()
    target_include_directories(oracles_update PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
    target_link_libraries(oracles_update PUBLIC ${ORACLES_SDL_TARGET})
    if(WIN32)
        target_link_libraries(oracles_update PRIVATE winhttp)
    endif()
    target_compile_definitions(oracles_update PUBLIC "ORACLES_PRODUCT_VERSION=\"${ORACLES_PRODUCT_VERSION}\""
                               PRIVATE "ORACLES_UPDATE_REPOSITORY=\"${ORACLES_UPDATE_REPOSITORY}\"")
    oracles_set_warnings(oracles_update)
    set(ORACLES_LAUNCHER_SOURCES main.c session.c session_options.c session_route.c session_live.c session_pause.c session_report.c home.c home_input.c pause.c home_games.c home_files.c home_save.c save_transfer.c home_choices.c home_screenshot.c file_dialog.c sdl_backend.c sdl_window.c sdl_input.c sdl_present.c safe_area.c ${ORACLES_HOST_DIR}/null_backend.c ${ORACLES_SESSION_DIR}/route.c diagnostics.c settings.c ${ORACLES_SESSION_DIR}/hotkey_lines.c hotkeys_session.c ${ORACLES_SESSION_DIR}/route_effects.c ${ORACLES_SESSION_DIR}/hotbar_model.c ${ORACLES_SESSION_DIR}/state_refusal.c session_state.c state_display.c ${ORACLES_SESSION_DIR}/mod_session.c ${ORACLES_SESSION_DIR}/mod_folder.c ${ORACLES_SESSION_DIR}/language_session.c touch_controls.c touch_sdl.c)
    if(ANDROID)
        # Android runs no executable: SDL's activity (android) loads libmain.so and calls its main, SDL_main.
        add_library(oracles SHARED ${ORACLES_LAUNCHER_SOURCES})
        set_target_properties(oracles PROPERTIES OUTPUT_NAME main)
        target_link_libraries(oracles PRIVATE log)   # the standard error forwarded to the system's log (main.c)
    else()
        add_executable(oracles ${ORACLES_LAUNCHER_SOURCES})
        set_target_properties(oracles PROPERTIES OUTPUT_NAME the-oracles-project)
    endif()
    target_include_directories(oracles PRIVATE ${ORACLES_SESSION_DIR})
    if(ORACLES_DEFAULT_CORE STREQUAL "mgba")
        target_compile_definitions(oracles PRIVATE ORACLES_DEFAULT_CORE_KIND=ORACLES_CORE_MGBA)
    else()
        target_compile_definitions(oracles PRIVATE ORACLES_DEFAULT_CORE_KIND=ORACLES_CORE_SAMEBOY)
    endif()
    target_link_libraries(oracles PRIVATE oracles_host oracles_guest oracles_render oracles_enhanced oracles_gameplay oracles_mods oracles_language oracles_ui_sdl oracles_update ${ORACLES_SDL_TARGET})
    oracles_set_warnings(oracles)
    if(WIN32)
        # The UTF-8 code page (launcher/windows/oracles.manifest): paths outside ASCII open as they are kept.
        set(ORACLES_MANIFEST ${CMAKE_CURRENT_SOURCE_DIR}/windows/oracles.manifest)
        # The application's icon, which Explorer, the taskbar and Start show (launcher/icon/the-oracles-project.ico).
        set(ORACLES_ICON_ICO ${CMAKE_CURRENT_SOURCE_DIR}/icon/the-oracles-project.ico)
        configure_file(windows/oracles.rc.in ${CMAKE_CURRENT_BINARY_DIR}/oracles.rc @ONLY)
        target_sources(oracles PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/oracles.rc)
        set_property(SOURCE ${CMAKE_CURRENT_BINARY_DIR}/oracles.rc APPEND PROPERTY OBJECT_DEPENDS ${ORACLES_MANIFEST} ${ORACLES_ICON_ICO})
    endif()
    if(APPLE)
        # The Retina display's pixels (launcher/macos/Info.plist), embedded: the executable is not an app bundle.
        set(ORACLES_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/macos/Info.plist)
        target_link_options(oracles PRIVATE "LINKER:-sectcreate,__TEXT,__info_plist,${ORACLES_INFO_PLIST}")
        set_property(TARGET oracles APPEND PROPERTY LINK_DEPENDS ${ORACLES_INFO_PLIST})
    endif()
    # The fonts are embedded, and their licence goes with them: each OFL.txt beside the executable;
    # so does SameBoy's, the core compiled in, whose Expat licence asks that its notice go with copies; so do mGBA's and
    # inih's, with the notice that says where their source is and the one change made to mGBA, in source form (the
    # Mozilla Public License asks both of a binary); so does Lua's, the mods' runtime compiled in, whose MIT licence asks
    # that its notice go with copies.
    set(ORACLES_LICENCES
        ${ORACLES_THIRD_PARTY_DIR}/fonts/marcellus/OFL.txt Marcellus-OFL.txt
        ${ORACLES_THIRD_PARTY_DIR}/fonts/alegreya-sans/OFL.txt AlegreyaSans-OFL.txt
        ${ORACLES_THIRD_PARTY_DIR}/fonts/jetbrains-mono/OFL.txt JetBrainsMono-OFL.txt
        ${ORACLES_THIRD_PARTY_DIR}/sameboy/LICENSE SameBoy-LICENSE.txt
        ${ORACLES_THIRD_PARTY_DIR}/mgba/LICENSE mGBA-LICENSE.txt
        ${ORACLES_THIRD_PARTY_DIR}/mgba/NOTICE.txt mGBA-NOTICE.txt
        ${ORACLES_THIRD_PARTY_DIR}/mgba/boot-rom.patch mGBA-boot-rom.patch
        ${ORACLES_THIRD_PARTY_DIR}/mgba/core/src/third-party/inih/LICENSE.txt inih-LICENSE.txt
        ${ORACLES_THIRD_PARTY_DIR}/lua/LICENSE Lua-LICENSE.txt)
    if(ORACLES_SDL3_BUILT)
        list(APPEND ORACLES_LICENCES ${sdl3_SOURCE_DIR}/LICENSE.txt SDL3-LICENSE.txt)   # the library built here goes with it
    endif()
    while(ORACLES_LICENCES)
        list(POP_FRONT ORACLES_LICENCES licence_source licence_name)
        add_custom_command(TARGET oracles POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different ${licence_source} $<TARGET_FILE_DIR:oracles>/${licence_name})
        install(FILES ${licence_source} DESTINATION bin RENAME ${licence_name})
    endwhile()
    install(TARGETS oracles RUNTIME DESTINATION bin LIBRARY DESTINATION lib)
endif()
