cmake_minimum_required(VERSION 3.20)
project(TrackDesigner VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# _USE_MATH_DEFINES is needed for M_PI on MSVC. The other two are Win32-only
# noise-reduction macros and mean nothing elsewhere.
add_compile_definitions(_USE_MATH_DEFINES)
if(WIN32)
    add_compile_definitions(WIN32_LEAN_AND_MEAN NOMINMAX)
endif()

# macOS: build for both Apple Silicon and Intel, and set a deployment floor.
if(APPLE)
    set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64" CACHE STRING "Build architectures")
    set(CMAKE_OSX_DEPLOYMENT_TARGET "12.0" CACHE STRING "Minimum macOS version")
endif()

# ── wxWidgets ──────────────────────────────────────────────────────────────────
find_package(wxWidgets CONFIG QUIET COMPONENTS core base xml adv gl)
if(NOT wxWidgets_FOUND)
    find_package(wxWidgets REQUIRED COMPONENTS core base xml adv gl)
    include(${wxWidgets_USE_FILE})
else()
    message(STATUS "Found wxWidgets via config mode")
endif()

# ── OpenGL ─────────────────────────────────────────────────────────────────────
find_package(OpenGL REQUIRED)

# ── MarklinCommon subproject ───────────────────────────────────────────────────
add_subdirectory(MarklinCommon)

# ── Sources ────────────────────────────────────────────────────────────────────
# WIN32 suppresses the console window on Windows; MACOSX_BUNDLE produces a
# proper .app on macOS. Each is ignored by the other platform.
add_executable(TrackDesigner WIN32 MACOSX_BUNDLE
    App.cpp
    MainFrame.cpp
    SplashDialog.cpp
    LayoutShapeDialog.cpp
    RectangleDialog.cpp
    ElevationWizard.cpp
    UserPaths.cpp
    LevelManagerDialog.cpp
    PrintOptionsDialog.cpp
    LayoutPrintout.cpp
    canvas/CanvasState.cpp
    canvas/Renderer.cpp
    io/LayoutXml.cpp
    panels/CanvasPanel.cpp
    panels/PalettePanel.cpp
    panels/PiecePreviewPopup.cpp
    panels/PropertiesPanel.cpp
    view3d/View3DFrame.cpp
)

# resources.rc is the Windows resource script (icon, version info). It is not
# meaningful on other platforms -- the images it used to embed are now loaded
# from the resources folder at runtime (see ResourceLoader.h).
if(WIN32)
    target_sources(TrackDesigner PRIVATE resources.rc)
endif()

target_include_directories(TrackDesigner PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/MarklinCommon
)

# ── Link ───────────────────────────────────────────────────────────────────────
if(TARGET wx::core)
    target_link_libraries(TrackDesigner PRIVATE
        MarklinCommon
        wx::core wx::base wx::xml wx::adv wx::gl
        OpenGL::GL OpenGL::GLU
    )
else()
    target_link_libraries(TrackDesigner PRIVATE
        MarklinCommon
        ${wxWidgets_LIBRARIES}
        OpenGL::GL OpenGL::GLU
    )
    target_compile_definitions(TrackDesigner PRIVATE ${wxWidgets_DEFINITIONS})
endif()

# ── Post-build: stage the runtime data ────────────────────────────────────────
add_custom_command(TARGET TrackDesigner POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${CMAKE_CURRENT_SOURCE_DIR}/MarklinCommon/data/catalog.xml
        $<TARGET_FILE_DIR:TrackDesigner>/catalog.xml
    COMMENT "Copying catalog.xml to output directory"
)

# The splash / cashier / help images used to be baked into the EXE as Win32
# RCDATA. They are now plain files loaded at runtime (see ResourceLoader.h),
# which is what made the Mac build possible -- so they have to be staged where
# wxStandardPaths::GetResourcesDir() will look for them.
if(APPLE)
    # Inside the .app bundle: Contents/Resources/resources/
    set(_res_dest "$<TARGET_BUNDLE_CONTENT_DIR:TrackDesigner>/Resources/resources")
else()
    # Beside the executable.
    set(_res_dest "$<TARGET_FILE_DIR:TrackDesigner>/resources")
endif()

add_custom_command(TARGET TrackDesigner POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E make_directory "${_res_dest}"
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${CMAKE_CURRENT_SOURCE_DIR}/resources/splash.jpg  "${_res_dest}/splash.jpg"
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${CMAKE_CURRENT_SOURCE_DIR}/resources/cashier.jpg "${_res_dest}/cashier.jpg"
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${CMAKE_CURRENT_SOURCE_DIR}/resources/help.jpg    "${_res_dest}/help.jpg"
    COMMENT "Staging images into the resources folder"
)
