package("metal-cpp")
    set_kind("library", { headeronly = true })
    set_homepage("https://developer.apple.com/metal/cpp/")
    set_description("Metal-cpp is a low-overhead C++ interface for Metal that helps you add Metal functionality to graphics apps, games, and game engines that are written in C++.")
    set_license("Apache-2.0")

    set_urls("https://developer.apple.com/metal/cpp/files/metal-cpp_$(version).zip",
         {version = function (version)
            local versions = {
                ["12"]   = "macOS12_iOS15",
                ["13"]   = "macOS13_iOS16",      -- 13.0
                ["13.3"] = "macOS13.3_iOS16.4",
                ["14"]   = "macOS14_iOS17-beta", -- 14.0
                ["14.2"] = "macOS14.2_iOS17.2",
                ["15b"]  = "macOS15_iOS18-beta",
                ["15"]   = "macOS15_iOS18",      -- 15.0
                ["15.2"] = "macOS15.2_iOS18.2",
                ["26b1"] = "macOS26_iOS26-beta",
                ["26b2"] = "macOS26_iOS26-beta2"
            }
            return versions[tostring(version)]
        end})

    add_versions("12",   "a4e2d4668951b6f2595618ed8c5dc514fc94fda5487fc722b1c1ff29d7b524f7")
    add_versions("13",   "6f741894229e9c750add1afc3797274fc008c7507e2ae726370c17c34b7c6a68")
    add_versions("13.3", "0afd87ca851465191ae4e3980aa036c7e9e02fe32e7c760ac1a74244aae6023b")
    add_versions("14",   "2009a339ecbd56b36601435fe08c415749f8ad09145755472bb637b319003367")
    add_versions("14.2", "d800ddbc3fccabce3a513f975eeafd4057e07a29e905ad5aaef8c1f4e12d9ada")
    add_versions("15b",  "d0a7990f43c7ce666036b5649283c9965df2f19a4a41570af0617bbe93b4a6e5")
    add_versions("15",   "0433df1e0ab13c2b0becbd78665071e3fa28381e9714a3fce28a497892b8a184")
    add_versions("15.2", "3437e4abfbd3d45217f34772ef3502f31ba3358e5fb6ac9d0ca952a047bcfe25")
    add_versions("26b1", "3778084a9b50be7f3dd3edfb127b77b2dcef75c4c71dc23583abb4f8df8bf62d")
    add_versions("26b2", "4f0e62aac6a875616d8e86a1cb206158312e52b34de72716773b3785eeb12dc6")

    -- todo:Move metal-cpp-extensions to github
    add_resources("*", "LearnMetalCPP", "https://developer.apple.com/metal/LearnMetalCPP.zip", "a709f3c0b532d5b9e3a5db3da3a35f3b783af27eb50f23a711115c02f86a256d")

    add_includedirs(
        "include", 
        "include/metal-cpp", 
        "include/metal-cpp-extensions"
    )

    add_frameworks("Foundation", "Metal", "MetalKit")

    add_defines(
        "NS_PRIVATE_IMPLEMENTATION",
        "CA_PRIVATE_IMPLEMENTATION",
        "MTL_PRIVATE_IMPLEMENTATION",
        "MTK_PRIVATE_IMPLEMENTATION"
    )

    on_check(function (package)
        assert(package:is_plat("macosx", "iphoneos"), "package(metal-cpp) only support macosx or iOS")
    end)

    on_install("macosx", "iphoneos", function (package)
        -- Copy metal-cpp
        if not os.trycp("metal-cpp", package:installdir("include")) then
            os.vcp("*", package:installdir("include", "metal-cpp"))
        end

        -- Copy metal-cpp-extensions
        local resDir = package:resourcedir("LearnMetalCPP")
        local extDir = path.join(resDir, "/LearnMetalCPP/metal-cpp-extensions")
        os.vcp(extDir, package:installdir("include"))
    end)

    on_test("macosx", "iphoneos", function (package)
        assert(package:check_cxxsnippets({test = [[
            #include <metal-cpp/Foundation/Foundation.hpp>
            #include <metal-cpp/Metal/Metal.hpp>
            #include <Foundation/Foundation.hpp>
            #include <Metal/Metal.hpp>

            #include <MetalKit/MetalKit.hpp>
            #include <AppKit/AppKit.hpp>

            void test () {
                MTL::Device* pDevice = MTL::CreateSystemDefaultDevice();
                MTL::CommandQueue* pCommnadQueue = pDevice->newCommandQueue();

                CGRect frame = (CGRect){ {100.0, 100.0}, {512.0, 512.0} };
                MTK::View* _pMtkView = MTK::View::alloc()->init( frame, pDevice );
                _pMtkView->setColorPixelFormat( MTL::PixelFormat::PixelFormatBGRA8Unorm_sRGB );
                _pMtkView->setClearColor( MTL::ClearColor::Make( 1.0, 0.0, 0.0, 1.0 ) );
            }
        ]]}, {configs = {languages = "c++17"}}))
    end)