cmake_minimum_required(VERSION 3.16)
project(Onyx VERSION 0.1.0 LANGUAGES C CXX)

# Build type (for single-config generators like Make/Ninja)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the build type (Debug or Release)" FORCE)
endif()

# Language standards
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)











include_directories(${CMAKE_BINARY_DIR}/generated)

# Optional message
message(STATUS "Generated Onyx version: ${FULL_VERSION}")


# --- Output directories for multi-config builds ---
foreach(OUTPUTCONFIG IN ITEMS Debug Release)
  string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG_UPPER)
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG_UPPER} ${CMAKE_BINARY_DIR}/bin/${OUTPUTCONFIG})
  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG_UPPER} ${CMAKE_BINARY_DIR}/lib/${OUTPUTCONFIG})
  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG_UPPER} ${CMAKE_BINARY_DIR}/lib/${OUTPUTCONFIG})
endforeach()

# --- External Libraries ---
include(FetchContent)

# GLFW
FetchContent_Declare(
        glfw
        GIT_REPOSITORY https://github.com/glfw/glfw.git
        GIT_TAG latest
)
FetchContent_MakeAvailable(glfw)

# Assimp
FetchContent_Declare(
        assimp
        GIT_REPOSITORY https://github.com/assimp/assimp.git
        GIT_TAG master
)
set(ASSIMP_NO_EXPORT ON CACHE BOOL "" FORCE)
set(ASSIMP_BUILD_ASSIMP_TOOLS OFF CACHE BOOL "" FORCE)
set(ASSIMP_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(ASSIMP_BUILD_SAMPLES OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(assimp)

# ImGui (Docking)
FetchContent_Declare(
        imgui
        GIT_REPOSITORY https://github.com/ocornut/imgui.git
        GIT_TAG docking
)
FetchContent_MakeAvailable(imgui)

# Required packages
find_package(yaml-cpp REQUIRED)
find_package(GLEW REQUIRED)

# --- ImGui Static Library ---
add_library(ImGui STATIC
        ${imgui_SOURCE_DIR}/imgui.cpp
        ${imgui_SOURCE_DIR}/imgui_draw.cpp
        ${imgui_SOURCE_DIR}/imgui_demo.cpp
        ${imgui_SOURCE_DIR}/imgui_tables.cpp
        ${imgui_SOURCE_DIR}/imgui_widgets.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_compile_definitions(ImGui PUBLIC
        IMGUI_IMPL_OPENGL_LOADER_GLEW
)

# --- Core Engine Library ---
file(GLOB_RECURSE CORE_SOURCES CONFIGURE_DEPENDS
        src/core/*.cpp
        src/core/*.h
)

add_library(Core STATIC
        ${CORE_SOURCES}
        src/core/systems/MACROS.h
        src/core/renderer/Renderer.cpp
        src/core/renderer/Renderer.h
        src/core/systems/AssetManager.cpp
        src/core/systems/AssetManager.h
        src/core/systems/Asset.cpp
        src/core/systems/Asset.h
        src/core/systems/assets/Texture2D.cpp
        src/core/systems/assets/Texture2D.h
)

target_include_directories(Core PUBLIC src/core)

# Add _DEBUG define only for debug builds
target_compile_definitions(Core PRIVATE
        $<$<CONFIG:Debug>:_DEBUG>
)

# --- Editor Executable ---
file(GLOB_RECURSE APP_SOURCES CONFIGURE_DEPENDS
        src/editor/*.cpp
        src/editor/*.h
)

add_executable(Editor ${APP_SOURCES}
        main.cpp
        src/editor/Editor.cpp
        src/editor/Editor.h
        src/editor/Windows/LoggerWindow.cpp
        src/editor/Windows/LoggerWindow.h
        src/editor/Windows/Viewport.cpp
        src/editor/Windows/Viewport.h
)

target_include_directories(Editor PRIVATE
        src/editor
        src/core
)

target_link_libraries(Editor PRIVATE
        Core
        ImGui
        glfw
        assimp::assimp
        GLEW::GLEW
        ${CMAKE_DL_LIBS}
)

target_compile_definitions(Editor PRIVATE
        $<$<CONFIG:Debug>:_DEBUG>
)

# --- Install ---
install(TARGETS Editor RUNTIME DESTINATION bin)
