cmake_minimum_required(VERSION 3.21)

project(wxWherigo LANGUAGES CXX)

include(FetchContent)

set(CMAKE_C_STANDARD 23)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(wxBUILD_SHARED OFF)

add_subdirectory(components/lua-5.1.4)
add_subdirectory(components/cartridge)
add_subdirectory(components/storage)

message(STATUS "Fetching wxWidgets...")

FetchContent_Declare(
        wxWidgets
        GIT_REPOSITORY https://github.com/wxWidgets/wxWidgets.git
        GIT_TAG v3.3.1
        GIT_SHALLOW ON
)
FetchContent_MakeAvailable(wxWidgets)

message(STATUS "Configure project....")

set(SRCS
        main/src/app.cpp
        main/src/main.cpp
        main/src/ui/game_screen.cpp
        main/src/ui/map_sim_frame.cpp
        main/src/ui/start_screen.cpp
        main/src/ui/wherigo_dialog.cpp
        main/src/lua/game_engine.cpp
        main/src/lua/media_manager.cpp
        main/src/lua/persistence.cpp
        main/src/lua/wherigo.cpp
        main/src/lua/wherigo_completion.cpp
        main/src/lua/zobject.cpp
        main/src/lua/ztimer.cpp
)

if (APPLE)
    configure_file(${CMAKE_SOURCE_DIR}/assets/Info.plist.in
                   ${CMAKE_BINARY_DIR}/Info.plist @ONLY)
    set(MACOSX_BUNDLE_ICON_FILE icon.icns)
    add_executable(${PROJECT_NAME} MACOSX_BUNDLE ${SRCS})
    set_target_properties(${PROJECT_NAME} PROPERTIES
            BUNDLE True
            MACOSX_BUNDLE_GUI_IDENTIFIER dev.mars3142.${PROJECT_NAME}
            MACOSX_BUNDLE_BUNDLE_NAME ${PROJECT_NAME}
            MACOSX_BUNDLE_BUNDLE_VERSION "0.1"
            MACOSX_BUNDLE_SHORT_VERSION_STRING "0.1"
            MACOSX_BUNDLE_ICON_FILE icon.icns
            MACOSX_BUNDLE_INFO_PLIST ${CMAKE_BINARY_DIR}/Info.plist
            INSTALL_RPATH "@executable_path"
    )
else ()
    add_executable(${PROJECT_NAME} ${SRCS})
endif ()

# Haupt-Includes
include_directories(main/include)

target_link_libraries(${PROJECT_NAME}
        PRIVATE
        wx::net
        wx::html
        wx::webview
        lua
        cartridge
        storage
)
target_include_directories(${PROJECT_NAME} PRIVATE ${wxWidgets_INCLUDE_DIRS})

# Kopiere die .dylib-Dateien ins Bundle
if (APPLE)
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            $<TARGET_FILE:lua>
            $<TARGET_FILE:cartridge>
            $<TARGET_FILE:storage>
            "$<TARGET_BUNDLE_DIR:${PROJECT_NAME}>/Contents/MacOS/"
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            ${CMAKE_SOURCE_DIR}/assets/icon.icns
            "$<TARGET_BUNDLE_DIR:${PROJECT_NAME}>/Contents/Resources/icon.icns"
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            ${CMAKE_BINARY_DIR}/Info.plist
            "$<TARGET_BUNDLE_DIR:${PROJECT_NAME}>/Contents/Info.plist"
    )
endif ()
