# --------------------------------------------------------------
# GLOBAL CONFIG
# --------------------------------------------------------------
set_property(GLOBAL PROPERTY USE_FOLDERS On)
set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "Modules/Misc")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Binaries/)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Binaries/Lib/)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/Binaries/Lib/)
# --------------------------------------------------------------
# --------------------------------------------------------------
# --------------------------------------------------------------

# --------------------------------------------------------------
# Defines a library module
# --------------------------------------------------------------
macro(ListPlatformFiles PlatformName OutPlatformFiles)
    file(GLOB_RECURSE ${OutPlatformFiles} 
    ${CMAKE_CURRENT_LIST_DIR}/Sources/Platform/${PlatformName}/**.hpp
    ${CMAKE_CURRENT_LIST_DIR}/Sources/Platform/${PlatformName}/**.cpp
        ${CMAKE_CURRENT_LIST_DIR}/Sources/Platform/${PlatformName}/**.c
        ${CMAKE_CURRENT_LIST_DIR}/Sources/Platform/${PlatformName}/**.h
        LIST_DIRECTORIES TRUE)
endmacro()

macro(DefineLibrary ModName Folder)
    file(GLOB_RECURSE MODULE_FILES ${CMAKE_CURRENT_LIST_DIR}/Sources/**.cpp 
        ${CMAKE_CURRENT_LIST_DIR}/Sources/**.hpp 
        ${CMAKE_CURRENT_LIST_DIR}/Sources/**.h 
        ${CMAKE_CURRENT_LIST_DIR}/Sources/**.c 
        ${CMAKE_CURRENT_LIST_DIR}/Sources/**.inl)

    source_group(TREE ${CMAKE_CURRENT_LIST_DIR} FILES ${MODULE_FILES})
    add_library(${ModName} STATIC ${MODULE_FILES})
    
    target_include_directories(${ModName} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/Sources/)
    target_compile_features(${ModName} PUBLIC cxx_std_20)
    target_compile_definitions(${ModName} PRIVATE BUILD_LIBRARY)

    if(WIN32)
        target_compile_definitions(${ModName} PUBLIC BUILD_WINDOWS)
    else()

    endif()

    set_target_properties(${ModName} PROPERTIES 
        FOLDER ${Folder}
        MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

    if(MSVC)
        target_compile_options(${ModName} PRIVATE /MP)
    endif()
endmacro()

# --------------------------------------------------------------
# Defines a runtime module
# --------------------------------------------------------------
macro(DefineExecutable ModName Folder)
    file(GLOB_RECURSE MODULE_FILES ${CMAKE_CURRENT_LIST_DIR}/Sources/**.cpp 
        ${CMAKE_CURRENT_LIST_DIR}/Sources/**.hpp 
        ${CMAKE_CURRENT_LIST_DIR}/Sources/**.h 
        ${CMAKE_CURRENT_LIST_DIR}/Sources/**.c 
        ${CMAKE_CURRENT_LIST_DIR}/Sources/**.inl)

    if(WIN32)
        add_executable(${ModName} WIN32 ${MODULE_FILES})
    else()
        add_executable(${ModName} ${MODULE_FILES})
    endif()
    source_group(TREE ${CMAKE_CURRENT_LIST_DIR} FILES ${MODULE_FILES})

    target_include_directories(${ModName} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/Sources/)
    target_compile_features(${ModName} PUBLIC cxx_std_20)
    target_compile_definitions(${ModName} PRIVATE BUILD_EXECUTABLE)
    
    set_target_properties(${ModName} PROPERTIES 
        FOLDER ${Folder}
        MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
        VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

    if(MSVC)
        target_compile_options(${ModName} PRIVATE /MP)
    endif()
endmacro()

# --------------------------------------------------------------
# Add dependencies
# --------------------------------------------------------------
macro(AddPublicModuleDependencies ModName DepName)
    target_link_libraries(${ModName} PUBLIC ${DepName} ${ARGN})
endmacro()

macro(AddPrivateModuleDependencies ModName DepName)
    target_link_libraries(${ModName} PUBLIC ${DepName} ${ARGN})
endmacro()

macro(AddInterfaceModuleDependencies ModName DepName)
    target_link_libraries(${ModName} PUBLIC ${DepName} ${ARGN})
endmacro()


# --------------------------------------------------------------
# Define third party module and configure it
# --------------------------------------------------------------
macro(AddThirdPartyModule ModName)
    add_subdirectory(${ModName})
    set_target_properties(${ModName} PROPERTIES 
        FOLDER Modules/ThirdParty
        MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endmacro()
