cmake_minimum_required(VERSION 3.28 FATAL_ERROR)

if (NOT APPLE)
    message(STATUS "MoltenVK is only available on Apple platforms")
    return()
endif ()

option(MOLTENVK_USE_STATIC "Prefer static linked MoltenVK over dynamic linked version" ON)
option(MOLTENVK_DOWNLOAD "Use FetchContent to download MoltenVK if it failed to find a MoltenVK/Vulkan SDK install" ON)

# Check if environment variable MOLTENVK_SDK is set
if (NOT DEFINED ENV{MOLTENVK_SDK} AND DEFINED ENV{VULKAN_SDK})
    message(STATUS "Checking for MoltenVK in Vulkan_SDK path")
    cmake_path(SET MoltenVK_path NORMALIZE "$ENV{VULKAN_SDK}/../MoltenVK/")
elseif (DEFINED ENV{MOLTENVK_SDK})
    cmake_path(SET MoltenVK_path NORMALIZE "$ENV{MOLTENVK_SDK}/")
endif ()
if (NOT IS_DIRECTORY ${MoltenVK_path})
    message(STATUS "MoltenVK_path '${MoltenVK_path}' does not exist!")
    set(MoltenVK_path)
endif ()
if (MOLTENVK_DOWNLOAD AND NOT DEFINED MoltenVK_path)
    message(STATUS "Downloading MoltenVK")
    include(FetchContent)
    FetchContent_Declare(moltenvkall
                         URL_HASH SHA256=4252fdd71d511b47a12bc859fe3a9f43e20e77006e5d7eeb9c3732447de1b7b5
                         URL https://github.com/KhronosGroup/MoltenVK/releases/download/v1.2.6/MoltenVK-all.tar
    )
    FetchContent_MakeAvailable(moltenvkall)
    set(MoltenVK_path "${moltenvkall_SOURCE_DIR}/MoltenVK/")
endif ()

if (NOT DEFINED MoltenVK_path)
    message(WARNING "Unable to find path to MoltenVK!")
elseif (IS_DIRECTORY ${MoltenVK_path})
    if (IS_DIRECTORY "${MoltenVK_path}/include")
        set(Vulkan_INCLUDE_DIR "${MoltenVK_path}/include")
        message(STATUS "Found MoltenVK includes at: ${Vulkan_INCLUDE_DIR}")
    endif()

    # Check for framework
    set(LEGACY_MoltenVK_XC_path "${MoltenVK_path}/MoltenVK.xcframework")
    set(STATIC_MoltenVK_XC_path "${MoltenVK_path}/static/MoltenVK.xcframework")
    set(DYNAMIC_MoltenVK_XC_path "${MoltenVK_path}/dynamic/MoltenVK.xcframework")
    if (IS_DIRECTORY "${LEGACY_MoltenVK_XC_path}")
        set(MoltenVK_XC_path ${LEGACY_MoltenVK_XC_path})
    elseif (IS_DIRECTORY "${STATIC_MoltenVK_XC_path}" AND MOLTENVK_USE_STATIC)
        set(MoltenVK_XC_path ${STATIC_MoltenVK_XC_path})
    elseif (IS_DIRECTORY "${DYNAMIC_MoltenVK_XC_path}")
        set(MoltenVK_XC_path ${DYNAMIC_MoltenVK_XC_path})
    endif ()
    if (DEFINED MoltenVK_XC_path)
        message("Found MoltenVK xcframework at: ${MoltenVK_XC_path}")
        set(Vulkan_LIBRARIES ${MoltenVK_XC_path})
    endif ()
else ()
    message(WARNING "Provided path ${MoltenVK_path} does not exist!")
endif ()

include(FindPackageHandleStandardArgs)
#TODO handle version
find_package_handle_standard_args(MoltenVK
                                  DEFAULT_MSG
                                  Vulkan_INCLUDE_DIR
                                  Vulkan_LIBRARIES
)

if(MoltenVK_FOUND)
    set(MoltenVK_INCLUDE_DIRS ${Vulkan_INCLUDE_DIR})
    set(MoltenVK_LIBRARIES ${Vulkan_LIBRARIES})
else()
    set(MoltenVK_INCLUDE_DIRS)
    set(MoltenVK_LIBRARIES)
endif()

mark_as_advanced(MoltenVK_path LEGACY_MoltenVK_XC_path STATIC_MoltenVK_XC_path DYNAMIC_MoltenVK_XC_path MoltenVK_XC_path)
