cmake_minimum_required(VERSION 3.16.3)

# Set the project name and version
project(Lunarium VERSION 0.1.0)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# specify the opengl version
set(OpenGL_MAJOR_VERSION 4)
set(OpenGL_MINOR_VERSION 5)

# Option to build without the editor
set(BUILD_NO_EDITOR 0)
option(NO_EDITOR "Building with editor" OFF)
if (NO_EDITOR)
    message(STATUS "Building without the editor")
    set(BUILD_NO_EDITOR 1)
endif ()

# Log level option
set(LOG_LEVEL 0)
option(LOG_LEVEL_ERROR "Log errors only" OFF)
option(LOG_LEVEL_INFO "Log errors, warnings and info" ON)
option(LOG_LEVEL_DEBUG "Log Everything (except internal graphics msgs)" OFF)
option(LOG_LEVEL_GINTERNAL_ERR "Log internal graphics error messages" OFF)
option(LOG_LEVEL_GINTERNAL_DEBUG "Log all internal graphics messages" OFF)

if (LOG_LEVEL_ERROR)
    set(LOG_LEVEL 1)
endif ()

if (LOG_LEVEL_INFO)
    set(LOG_LEVEL 2)
endif ()

if (LOG_LEVEL_DEBUG)
    set(LOG_LEVEL 3)
endif ()

if (LOG_LEVEL_GINTERNAL_ERR)
    set(LOG_LEVEL 4)
endif ()

if (LOG_LEVEL_GINTERNAL_DEBUG)
    set(LOG_LEVEL 5)
endif ()

message (STATUS "Setting log level to " ${LOG_LEVEL})

configure_file(LunariumConfig.h.in LunariumConfig.h)

# Source Files
set(LUNARIUM_SRC
"src/main.cpp"
"src/core/core.cpp"
"src/core/state.cpp"
"src/core/version.cpp"
"src/core/types.cpp"
"src/core/core_console.cpp"
"src/core/run_mode.cpp"
"src/utils/stb/stb_image_write.cpp"
"src/utils/stb/stb_image.cpp" 
"src/utils/args.cpp" 
"src/utils/binary_file_buffer.cpp"
"src/utils/frame_counter.cpp" 
"src/utils/helpers.cpp" 
"src/utils/high_resolution_timer.cpp" 
"src/utils/logger.cpp" 
"src/utils/op_res.cpp"
"src/utils/uuid.cpp"
"src/platform/window.cpp"
"src/platform/terminal.cpp"
"src/renderer/render_context.cpp"
"src/renderer/renderer2D.cpp"
"src/renderer/text_renderer.cpp"
"src/renderer/vertex_buffer.cpp"
"src/renderer/index_buffer.cpp"
"src/renderer/orthographic_camera.cpp"
"src/renderer/texture.cpp"
"src/renderer/shader.cpp"
"src/renderer/frame_buffer.cpp"
"src/renderer/orthographic_camera.cpp"
"src/gui/gui.cpp"
"src/gui/imgui_ext.cpp"
"src/gui/panel.cpp"
"src/gui/panel_manager.cpp"
"src/gui/console.cpp"
"src/internal_data/data_manager.cpp"
"src/assets/asset_manager.cpp"
"src/assets/types/asset.cpp"
"src/assets/types/image.cpp"
"src/assets/loaders/asset_index.cpp"
"src/input/keyboard.cpp"
"src/input/input_manager.cpp"
"src/scripting/wren_state.cpp"
"src/scripting/wren_script.cpp"
"src/scripting/coreAPI.cpp"
"src/world/world.cpp"
"src/scripting/world_api.cpp"
"src/scripting/component_api.cpp"
"src/world/entity.cpp"
)

# add the executable
add_executable(${PROJECT_NAME} ${LUNARIUM_SRC})

#       DOWNLOAD ALL SUBMODULES
find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
# Update submodules as needed
    option(GIT_SUBMODULE "Check submodules during build" ON)
    if(GIT_SUBMODULE)
        message(STATUS "Submodule update")
        execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
                        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                        RESULT_VARIABLE GIT_SUBMOD_RESULT)
        if(NOT GIT_SUBMOD_RESULT EQUAL "0")
            message(FATAL_ERROR "git submodule update --init failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
        endif()
    endif()
endif()

#       CHECK THAT ALL SUBMODULES EXIST
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/external/glfw/CMakeLists.txt")
    message(FATAL_ERROR "GLFW submodule was not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()

if(NOT EXISTS "${PROJECT_SOURCE_DIR}/external/glm/CMakeLists.txt")
    message(FATAL_ERROR "GLM submodule was not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()

if(NOT EXISTS "${PROJECT_SOURCE_DIR}/external/freetype/CMakeLists.txt")
    message(FATAL_ERROR "FREETYPE submodule was not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()

if(NOT EXISTS "${PROJECT_SOURCE_DIR}/external/box2d/CMakeLists.txt")
    message(FATAL_ERROR "BOX2D submodule was not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()

# add GLFW
add_subdirectory(external/glfw)

# add glad2
add_subdirectory(external/glad/src)

# add GLM
add_subdirectory(external/glm)

# add dearimgui
add_subdirectory(external/dearimgui)

# add freetype
add_subdirectory(external/freetype)

# add box2d
add_subdirectory(external/box2d)

# add wren
add_subdirectory(external/wren)

# add nativefiledialog
add_subdirectory(external/nativefiledialog-extended)

# add run mode tester
add_subdirectory(src/run_modes/testbed)

# add run mode editor
if (NOT NO_EDITOR)
    add_subdirectory(src/run_modes/editor)
endif()


# add run mode game
add_subdirectory(src/run_modes/game)

target_include_directories(${PROJECT_NAME} 
    PUBLIC "${PROJECT_BINARY_DIR}" 
    PUBLIC src
    PUBLIC src/internal_libs
    PUBLIC external
    PUBLIC external/glfw/include
    PUBLIC external/glm
    PUBLIC external/dearimgui
    PUBLIC external/glad/include
    PUBLIC external/freetype/include
    PUBLIC external/box2d/include
    PUBLIC external/entt
    PUBLIC external/nativefiledialog-extended/src/include
    PUBLIC external/wren/include
)

target_link_directories(${PROJECT_NAME}
    PRIVATE external/glfw/src
    PRIVATE external/glm
    PRIVATE external/dearimgui
    PRIVATE src/run_modes/tester
    PRIVATE external/glad/src
    PRIVATE external/freetype/src
    PRIVATE external/box2d/bin
    PRIVATE external/nativefiledialog-extended
    PRIVATE external/wren
)

target_link_libraries(${PROJECT_NAME} box2d glfw glad glm dearimgui freetype nfd testbed wren)

if (NOT NO_EDITOR)
    target_link_libraries(${PROJECT_NAME} editor)
endif()

if(WIN32)
    target_link_libraries(${PROJECT_NAME} opengl32.lib)
elseif(UNIX)
    target_link_libraries(${PROJECT_NAME} X11 GL)
endif()
