cmake_minimum_required(VERSION 4.2)
project(ColorRace)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# --- CPM.cmake Bootstrap ---
set(CPM_DOWNLOAD_VERSION 0.40.2)
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
if(NOT EXISTS ${CPM_DOWNLOAD_LOCATION})
    file(DOWNLOAD
            https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
            ${CPM_DOWNLOAD_LOCATION}
    )
endif()
include(${CPM_DOWNLOAD_LOCATION})

# --- OpenGL (systemseitig, kein CPM nötig) ---
find_package(OpenGL REQUIRED)

# --- GLFW ---
CPMAddPackage(
        NAME glfw
        GITHUB_REPOSITORY glfw/glfw
        GIT_TAG 3.4
        OPTIONS
        "GLFW_BUILD_EXAMPLES OFF"
        "GLFW_BUILD_TESTS OFF"
        "GLFW_BUILD_DOCS OFF"
        "GLFW_INSTALL OFF"
)

# --- GLM (header-only) ---
CPMAddPackage(
        NAME glm
        GITHUB_REPOSITORY g-truc/glm
        GIT_TAG 1.0.1
)

# --- tinyobjloader ---
CPMAddPackage(
        NAME tinyobjloader
        GITHUB_REPOSITORY tinyobjloader/tinyobjloader
        GIT_TAG v2.0.0rc13
)

# --- stb_image (kein CMake-Projekt, nur Header) ---
CPMAddPackage(
        NAME stb
        GITHUB_REPOSITORY nothings/stb
        GIT_TAG master
        DOWNLOAD_ONLY YES
)
add_library(stb_image STATIC third_party/stb_image_impl.cpp)
target_include_directories(stb_image PUBLIC ${stb_SOURCE_DIR})

# --- Dear ImGui (Core + GLFW/OpenGL3 Backend) ---
CPMAddPackage(
        NAME imgui
        GITHUB_REPOSITORY ocornut/imgui
        GIT_TAG v1.91.5
        DOWNLOAD_ONLY YES
)

CPMAddPackage(
        NAME spdlog
        GITHUB_REPOSITORY gabime/spdlog
        GIT_TAG v1.15.3
)

# --- GoogleTest ---
CPMAddPackage(
        NAME googletest
        GITHUB_REPOSITORY google/googletest
        GIT_TAG v1.15.2
        OPTIONS
        "INSTALL_GTEST OFF"
        "gtest_force_shared_crt ON"
)

enable_testing()

add_library(imgui STATIC
        ${imgui_SOURCE_DIR}/imgui.cpp
        ${imgui_SOURCE_DIR}/imgui_draw.cpp
        ${imgui_SOURCE_DIR}/imgui_tables.cpp
        ${imgui_SOURCE_DIR}/imgui_widgets.cpp
        ${imgui_SOURCE_DIR}/imgui_demo.cpp
        ${imgui_SOURCE_DIR}/backends/imgui_impl_glfw.cpp
        ${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp
)
target_include_directories(imgui PUBLIC ${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends)
target_link_libraries(imgui PUBLIC glfw glad)

# --- GLAD (manuell generiert, siehe README/Hinweis unten) ---
add_library(glad STATIC third_party/glad/src/glad.c)
target_include_directories(glad PUBLIC third_party/glad/include)

# --- OpenAL-Soft ---
CPMAddPackage(
        NAME openal-soft
        GITHUB_REPOSITORY kcat/openal-soft
        GIT_TAG 1.24.3
        OPTIONS
        "LIBTYPE STATIC"
        "ALSOFT_UTILS OFF"
        "ALSOFT_EXAMPLES OFF"
        "ALSOFT_TESTS OFF"
        "ALSOFT_INSTALL OFF"
)

# --- dr_libs (dr_wav.h, dr_mp3.h – header-only) ---
CPMAddPackage(
        NAME dr_libs
        GITHUB_REPOSITORY mackron/dr_libs
        GIT_TAG master
        DOWNLOAD_ONLY YES
)

message(STATUS "dr_libs_SOURCE_DIR = ${dr_libs_SOURCE_DIR}")

add_subdirectory(external/NetworkCore)

# Engine als statische Lib
add_library(Engine STATIC
        engine/src/core/Window.h
        engine/src/core/Window.cpp
        engine/src/core/Application.cpp
        engine/src/core/Application.h
        engine/src/core/EngineConfig.h
        engine/src/utils/openglWrapper/GLStateCache.cpp
        engine/src/utils/openglWrapper/GLStateCache.h
        engine/src/utils/openglWrapper/GLRenderState.cpp
        engine/src/utils/openglWrapper/GLRenderState.h
        engine/src/utils/openglWrapper/openglObjects/Attribute.cpp
        engine/src/utils/openglWrapper/openglObjects/Attribute.h
        engine/src/utils/openglWrapper/openglObjects/Vbo.cpp
        engine/src/utils/openglWrapper/openglObjects/Vbo.h
        engine/src/utils/openglWrapper/openglObjects/Vao.cpp
        engine/src/utils/openglWrapper/openglObjects/Vao.h
        engine/src/utils/openglWrapper/shader/ShaderProgram.cpp
        engine/src/utils/openglWrapper/shader/ShaderProgram.h
        engine/src/renderer/primitives/SphereFactory.cpp
        engine/src/renderer/primitives/SphereFactory.h
        engine/src/utils/openglWrapper/shader/Uniform.cpp
        engine/src/utils/openglWrapper/shader/Uniform.h
        engine/src/utils/openglWrapper/shader/UniformValue.cpp
        engine/src/utils/openglWrapper/shader/UniformValue.h
        engine/src/layer/Scene.cpp
        engine/src/layer/Scene.h
        engine/src/loader/AssetLoader.cpp
        engine/src/loader/AssetLoader.h
        engine/src/loader/assets/AssetTypes.h
        engine/src/loader/assets/AssetRequests.h
        engine/src/loader/assets/AssetRequests.cpp
        engine/src/loader/assets/RawAssetData.h
        engine/src/loader/assets/AssetFormatters.h
        engine/src/loader/textures/TextureUploader.cpp
        engine/src/loader/textures/TextureUploader.h
        engine/src/loader/textures/TextureImporter.cpp
        engine/src/loader/textures/TextureImporter.h
        engine/src/loader/textures/Texture2D.cpp
        engine/src/loader/textures/Texture2D.h
        engine/src/loader/models/ModelImporter.cpp
        engine/src/loader/models/ModelImporter.h
        engine/src/loader/models/ModelUploader.cpp
        engine/src/loader/models/ModelUploader.h
        engine/src/loader/models/Material.cpp
        engine/src/loader/models/Material.h
        engine/src/loader/assets/LoadedAssets.h
        engine/src/loader/models/Model.cpp
        engine/src/loader/models/Model.h
        engine/src/loader/models/MeshSection.cpp
        engine/src/loader/models/MeshSection.h
        engine/src/loader/assets/AssetManager.cpp
        engine/src/loader/assets/AssetManager.h
        engine/src/renderer/entities/PointLight.cpp
        engine/src/renderer/entities/PointLight.h
        engine/src/core/inputsOutputs/inputs/MouseButton.h
        engine/src/core/inputsOutputs/inputs/Mouse.cpp
        engine/src/core/inputsOutputs/inputs/Mouse.h
        engine/src/core/inputsOutputs/inputs/Keyboard.cpp
        engine/src/core/inputsOutputs/inputs/Keyboard.h
        engine/src/core/inputsOutputs/context/InputContextStack.cpp
        engine/src/core/inputsOutputs/context/InputContextStack.h
        engine/src/core/inputsOutputs/controller/CameraController.cpp
        engine/src/core/inputsOutputs/controller/CameraController.h
        engine/src/game/GameState.cpp
        engine/src/game/GameState.h
        engine/src/game/GameMode.cpp
        engine/src/game/GameMode.h
        engine/src/game/TurnBasedGameMode.cpp
        engine/src/game/TurnBasedGameMode.h
        engine/src/ecs/standardComponents/PickableComponent.h
        engine/src/ecs/systems/PickingSystem.cpp
        engine/src/ecs/systems/PickingSystem.h
        engine/src/ui/ImGuiContext.cpp
        engine/src/ui/ImGuiContext.h
        engine/src/renderer/shader/BoundingDebugShader.cpp
        engine/src/renderer/shader/BoundingDebugShader.h
        engine/src/renderer/DebugRenderer.cpp
        engine/src/renderer/DebugRenderer.h
        engine/src/layer/DebugLayer.cpp
        engine/src/layer/DebugLayer.h
        engine/src/loader/models/AABB.cpp
        engine/src/loader/models/AABB.h
        engine/src/loader/models/Ray.h
        engine/src/ecs/standardComponents/HighlightComponent.h
        third_party/audio_impl.cpp
        engine/src/core/audio/AudioDevice.cpp
        engine/src/core/audio/AudioDevice.h
        engine/src/core/audio/SoundBuffer.cpp
        engine/src/core/audio/SoundBuffer.h
        engine/src/core/audio/SoundSource.cpp
        engine/src/core/audio/SoundSource.h
        engine/src/loader/sounds/SoundUploader.cpp
        engine/src/loader/sounds/SoundUploader.h
        engine/src/core/animation/components/TransformAnimationComponent.h
        engine/src/core/animation/AnimationSystem.cpp
        engine/src/core/animation/AnimationSystem.h
        engine/src/renderer/shader/StaticShader.cpp
        engine/src/renderer/shader/StaticShader.h
        engine/src/renderer/entities/Camera.cpp
        engine/src/renderer/entities/Camera.h
        engine/src/ecs/EntityRegistry.cpp
        engine/src/ecs/EntityRegistry.h
        engine/src/ecs/Entity.cpp
        engine/src/ecs/Entity.h
        engine/src/ecs/ComponentPool.cpp
        engine/src/ecs/ComponentPool.h
        engine/src/ecs/standardComponents/TransformComponent.h
        engine/src/ecs/standardComponents/MeshComponent.h
        engine/src/ecs/EntitySystem.cpp
        engine/src/ecs/EntitySystem.h
        engine/src/renderer/RenderSystem.cpp
        engine/src/renderer/RenderSystem.h
        engine/src/renderer/RenderQueue.cpp
        engine/src/renderer/RenderQueue.h
        engine/src/renderer/MeshRenderSystem.cpp
        engine/src/renderer/MeshRenderSystem.h
        engine/src/renderer/MeshRenderer.cpp
        engine/src/renderer/MeshRenderer.h
        engine/src/layer/Layer.cpp
        engine/src/layer/Layer.h
        engine/src/core/inputsOutputs/context/InputContext.cpp
        engine/src/core/inputsOutputs/context/InputContext.h
        engine/src/layer/SceneManager.cpp
        engine/src/layer/SceneManager.h
        engine/src/core/events/EventBus.cpp
        engine/src/core/events/EventBus.h
        engine/src/core/audio/ecs/PlaySoundComponent.h
        engine/src/core/animation/events/AnimationMarkerEvent.h
        engine/src/core/animation/components/BlockingAnimationComponent.h
        engine/src/core/audio/ecs/AudioSystem.cpp
        engine/src/core/audio/ecs/AudioSystem.h
        engine/src/core/audio/ecs/BlockingSoundComponent.h
        engine/src/renderer/shader/GuiShader.cpp
        engine/src/renderer/shader/GuiShader.h
        engine/src/renderer/primitives/QuadFactory.cpp
        engine/src/renderer/primitives/QuadFactory.h
        engine/src/loader/models/SimpleTexturedMesh.cpp
        engine/src/loader/models/SimpleTexturedMesh.h
        engine/src/loader/models/Mesh.cpp
        engine/src/loader/models/Mesh.h
        engine/src/renderer/GuiRenderer.cpp
        engine/src/renderer/GuiRenderer.h
        engine/src/renderer/ui/GUITexture.cpp
        engine/src/renderer/ui/GUITexture.h
        engine/src/core/events/ApplicationEvents.h
        engine/src/core/events/EventChannels.cpp
        engine/src/core/events/EventChannels.h
)
target_include_directories(Engine PUBLIC engine/src ${dr_libs_SOURCE_DIR})
target_link_libraries(Engine PUBLIC OpenGL::GL glfw glad glm::glm imgui spdlog::spdlog tinyobjloader stb_image OpenAL::OpenAL NetworkCore)

# --- GameLib: Spiel-Logik, unabhängig von main.cpp testbar ---
add_library(GameLib STATIC
        game/src/ColorRaceApp.cpp
        game/src/ColorRaceApp.h
        game/src/gameLayer/GameLayer.cpp
        game/src/gameLayer/GameLayer.h
        game/src/GameScene.cpp
        game/src/GameScene.h
        game/src/ludo/LudoGameState.cpp
        game/src/ludo/LudoGameState.h
        game/src/ludo/LudoTypes.h
        game/src/ludo/LudoGameMode.cpp
        game/src/ludo/LudoGameMode.h
        game/src/ludo/BoardLayout.cpp
        game/src/ludo/BoardLayout.h
        game/src/ludo/PiecePresenter.cpp
        game/src/ludo/PiecePresenter.h
        game/src/ludo/LudoAiController.cpp
        game/src/ludo/LudoAiController.h
        game/src/ludo/ecs/highlight/HighlightSystem.cpp
        game/src/ludo/ecs/highlight/HighlightSystem.h
        game/src/AudioLayer.cpp
        game/src/AudioLayer.h
        game/src/ludo/DiceSource.cpp
        game/src/ludo/DiceSource.h
        game/src/ludo/PresentationGameState.cpp
        game/src/ludo/PresentationGameState.h
        game/src/gameLayer/GameRenderer.cpp
        game/src/gameLayer/GameRenderer.h
        game/src/gameLayer/gameController/LudoGameController.cpp
        game/src/gameLayer/gameController/LudoGameController.h
        game/src/gameLayer/LudoHud.cpp
        game/src/gameLayer/LudoHud.h
        game/src/gameLayer/GameInput.cpp
        game/src/gameLayer/GameInput.h
        game/src/gameLayer/gameController/LudoGameControllerContext.h
        game/src/ludo/gameMode/LudoRules.cpp
        game/src/ludo/gameMode/LudoRules.h
        game/src/ludo/gameMode/PiecePathBuilder.cpp
        game/src/ludo/gameMode/PiecePathBuilder.h
        game/src/ludo/gameMode/PathNode.h
        game/src/mainMenu/MainMenuScene.cpp
        game/src/mainMenu/MainMenuScene.h
)
target_include_directories(GameLib PUBLIC game/src)
target_link_libraries(GameLib PUBLIC Engine)

add_subdirectory(server)

# --- Executable ---
add_executable(ColorRace
        game/src/main.cpp
        game/src/mainMenu/layer/MainMenuUiLayer.cpp
        game/src/mainMenu/layer/MainMenuUiLayer.h
        game/src/multiplayer/LudoGameClient.cpp
        game/src/multiplayer/LudoGameClient.h
        game/src/multiplayer/LudoNetworkEvents.h
        game/src/multiplayer/lobby/LobbyScene.cpp
        game/src/multiplayer/lobby/LobbyScene.h
        game/src/multiplayer/lobby/LobbyUiLayer.cpp
        game/src/multiplayer/lobby/LobbyUiLayer.h
)

target_link_libraries(ColorRace PRIVATE
        Engine
        GameLib
)

# --- Tests ---
add_executable(ColorRaceTests
        tests/LoduGameMode.cpp
        tests/helpers/SequenceDiceSource.cpp
        tests/helpers/SequenceDiceSource.h
)

target_link_libraries(ColorRaceTests PRIVATE
        GameLib
        GTest::gtest
        GTest::gtest_main
)

include(GoogleTest)
gtest_discover_tests(ColorRaceTests)