if(NOT WIN32)
  find_package(PkgConfig QUIET)
  if(PkgConfig_FOUND)
    pkg_check_modules(SDL2 QUIET IMPORTED_TARGET sdl2)
  endif()
endif()
if(TARGET PkgConfig::SDL2)
  set(DSR_SDL2_TARGET PkgConfig::SDL2)
else()
  find_package(SDL2 REQUIRED)
  set(DSR_SDL2_TARGET SDL2::SDL2)
endif()
if(WIN32 AND NOT DSR_SDL2_RUNTIME_DLL AND TARGET SDL2::SDL2)
  foreach(config RELEASE RELWITHDEBINFO DEBUG "")
    if(config)
      get_target_property(_dsr_sdl2_runtime_dll SDL2::SDL2 IMPORTED_LOCATION_${config})
    else()
      get_target_property(_dsr_sdl2_runtime_dll SDL2::SDL2 IMPORTED_LOCATION)
    endif()
    if(_dsr_sdl2_runtime_dll MATCHES "[.][dD][lL][lL]$" AND EXISTS "${_dsr_sdl2_runtime_dll}")
      set(DSR_SDL2_RUNTIME_DLL "${_dsr_sdl2_runtime_dll}" CACHE FILEPATH
          "SDL2 runtime DLL installed alongside dq9 on Windows" FORCE)
      break()
    endif()
  endforeach()
endif()

add_library(dsr_frontend STATIC src/frontend.cpp src/frontend_logic.cpp src/ui_logic.cpp src/debug_editor.cpp src/ui_toolkit.cpp src/controller_support.cpp src/launcher.cpp src/rom_path.cpp)
target_link_libraries(dsr_frontend PUBLIC dsr_headers PRIVATE ${DSR_SDL2_TARGET})
if(WIN32)
  target_link_libraries(dsr_frontend PRIVATE shell32 ole32 winmm)
endif()
dsr_target_compile_options(dsr_frontend -Wall -Wextra -Wpedantic)

add_executable(dsr_frontend_demo src/demo.cpp)
target_link_libraries(dsr_frontend_demo PRIVATE dsr_frontend)
dsr_target_compile_options(dsr_frontend_demo -Wall -Wextra -Wpedantic)
dsr_copy_sdl2_runtime(dsr_frontend_demo)

add_executable(dsr_frontend_logic_test tests/frontend_logic_test.cpp src/frontend_logic.cpp src/ui_logic.cpp)
target_link_libraries(dsr_frontend_logic_test PRIVATE dsr_headers)
target_include_directories(dsr_frontend_logic_test PRIVATE src)
dsr_target_compile_options(dsr_frontend_logic_test -Wall -Wextra -Wpedantic)
add_test(NAME frontend_logic COMMAND dsr_frontend_logic_test)

add_executable(dsr_ui_logic_test tests/ui_logic_test.cpp src/ui_logic.cpp src/debug_editor.cpp)
target_link_libraries(dsr_ui_logic_test PRIVATE dsr_headers)
target_include_directories(dsr_ui_logic_test PRIVATE src)
dsr_target_compile_options(dsr_ui_logic_test -Wall -Wextra -Wpedantic)
add_test(NAME ui_logic COMMAND dsr_ui_logic_test)

add_executable(dsr_ui_screenshot_capture_test tests/ui_screenshot_capture_test.cpp)
target_link_libraries(dsr_ui_screenshot_capture_test PRIVATE dsr_frontend)
target_include_directories(dsr_ui_screenshot_capture_test PRIVATE src)
dsr_target_compile_options(dsr_ui_screenshot_capture_test -Wall -Wextra -Wpedantic)
dsr_copy_sdl2_runtime(dsr_ui_screenshot_capture_test)

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  add_executable(dsr_ui_text_batch_test tests/ui_text_batch_test.cpp)
  target_link_libraries(dsr_ui_text_batch_test PRIVATE dsr_frontend ${DSR_SDL2_TARGET})
  target_include_directories(dsr_ui_text_batch_test PRIVATE src)
  target_link_options(dsr_ui_text_batch_test PRIVATE
      "-Wl,--wrap=SDL_RenderFillRect,--wrap=SDL_RenderFillRects")
  dsr_target_compile_options(dsr_ui_text_batch_test -Wall -Wextra -Wpedantic -UNDEBUG)
  add_test(NAME ui_text_batch COMMAND dsr_ui_text_batch_test)

  add_executable(dsr_frontend_controls_test tests/frontend_controls_test.cpp)
  target_link_libraries(dsr_frontend_controls_test PRIVATE dsr_frontend ${DSR_SDL2_TARGET})
  dsr_target_compile_options(dsr_frontend_controls_test -Wall -Wextra -Wpedantic -UNDEBUG)
  add_test(NAME frontend_controls COMMAND dsr_frontend_controls_test)

  add_executable(dsr_frontend_audio_test tests/frontend_audio_test.cpp)
  target_link_libraries(dsr_frontend_audio_test PRIVATE dsr_frontend ${DSR_SDL2_TARGET})
  target_link_options(dsr_frontend_audio_test PRIVATE
      "-Wl,--wrap=SDL_GetQueuedAudioSize,--wrap=SDL_QueueAudio,--wrap=SDL_PauseAudioDevice,--wrap=SDL_ClearQueuedAudio,--wrap=_ZNSt6chrono3_V212steady_clock3nowEv")
  dsr_target_compile_options(dsr_frontend_audio_test -Wall -Wextra -Wpedantic -UNDEBUG)
  add_test(NAME frontend_audio COMMAND dsr_frontend_audio_test)
endif()

add_executable(dsr_rom_path_test tests/rom_path_test.cpp src/rom_path.cpp)
target_link_libraries(dsr_rom_path_test PRIVATE dsr_headers)
dsr_target_compile_options(dsr_rom_path_test -Wall -Wextra -Wpedantic)
add_test(NAME rom_path COMMAND dsr_rom_path_test)

# These integration tests isolate SDL settings with XDG directories. On Windows
# user paths come from Known Folders instead, so do not touch a user's settings.
if(NOT WIN32)
  add_executable(dsr_frontend_input_test tests/frontend_input_test.cpp)
  target_link_libraries(dsr_frontend_input_test PRIVATE dsr_frontend ${DSR_SDL2_TARGET})
  dsr_target_compile_options(dsr_frontend_input_test -Wall -Wextra -Wpedantic)
  add_test(NAME frontend_input COMMAND dsr_frontend_input_test)

  add_executable(dsr_launcher_input_test tests/launcher_input_test.cpp)
  target_link_libraries(dsr_launcher_input_test PRIVATE dsr_frontend ${DSR_SDL2_TARGET})
  dsr_target_compile_options(dsr_launcher_input_test -Wall -Wextra -Wpedantic)
  add_test(NAME launcher_input COMMAND dsr_launcher_input_test)
  set_tests_properties(launcher_input PROPERTIES TIMEOUT 10)
endif()
