--[[----------------------------------------------------------------------------

NmbLocalRenditionDigest.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2021 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

------------------------------------------------------------------------------]]

local WFConfiguration = require 'WFConfiguration'

--------------------------------------------------------------------------------

local _neverDownload = not not WFConfiguration.PixelProvider.NeverDownload

--------------------------------------------------------------------------------

-- must defer this import to avoid recursive require loops
local PixelProviderSharedBlobCache_getBestMatchInvalidRenditionForRenderInfo

--------------------------------------------------------------------------------

local function haveSuitableDownloadableInvalidRendition( 
				renderInfo, localAssetId, renderSize )

	if _neverDownload then

		return false

	end

	if not PixelProviderSharedBlobCache_getBestMatchInvalidRenditionForRenderInfo then

		PixelProviderSharedBlobCache_getBestMatchInvalidRenditionForRenderInfo =
			assert( require "PixelProviderSharedBlobCache".getBestMatchInvalidRenditionForRenderInfo,
					"missing PixelProviderSharedBlobCache.getBestMatchInvalidRenditionForRenderInfo" )

	end

	local maybeDownloadableInvalidRenditionSizeName = 
		PixelProviderSharedBlobCache_getBestMatchInvalidRenditionForRenderInfo(
				renderInfo, localAssetId, renderSize )

	return maybeDownloadableInvalidRenditionSizeName ~= nil

end

--------------------------------------------------------------------------------

local function renderInfoHasDownloadableProxy( renderInfo )
	
	local proxies = renderInfo.proxies or {}
	
	return #proxies > 0

end

--------------------------------------------------------------------------------

local function renderInfoHasDownloadableOriginal( renderInfo )

	return not not renderInfo.master

end

--------------------------------------------------------------------------------

local function canOnlyDownloadInvalidRenditions( renderInfo )

	local downloadableInvalidRenditions = renderInfo.invalid_renditions or {}

	if #downloadableInvalidRenditions == 0 then

		-- there are no invalid renditions, so we can't download
		return false

	end

	-- we have invalid downloadable renditions from this point on. Try to see
	-- if there something else we could use

	local downloadableRenditions = renderInfo.renditions or {}

	if #downloadableRenditions > 0 then

		-- we always prefer valid renditions even if low res, so we won't
		-- download invalid ones
		return false

	end

	if renderInfoHasDownloadableProxy( renderInfo ) then

		-- we can download a proxy and use that to render, so we won't download
		-- invalid renditions
		return false

	end

	if renderInfoHasDownloadableOriginal( renderInfo ) then

		-- we can download the original and use that to render, so we won't
		-- download invalid renditions
		return false

	end

	-- we have invalid renditions and they are currently the only option for
	-- pixels
	return true

end

--==============================================================================

return {

	renditionDownloadsProhibited = function() return _neverDownload end,

	haveSuitableDownloadableInvalidRendition = 
		haveSuitableDownloadableInvalidRendition,

	renderInfoHasDownloadableProxy = renderInfoHasDownloadableProxy,

	renderInfoHasDownloadableOriginal = renderInfoHasDownloadableOriginal,

	canOnlyDownloadInvalidRenditions = canOnlyDownloadInvalidRenditions,

}
