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

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

NOTICE: All information contained herein is, and remains 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 are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

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

	-- wichitafoundation.core.utils
local KSTableUtils = require 'KSTableUtils'

	-- Lrmobile_models
local LrmWFCapabilities = require 'LrmWFCapabilities'

local derefOpt = assert( KSTableUtils.derefOpt )

local ozLinkNameForOzRenditionName =
	assert( LrmWFCapabilities.ozLinkNameForRenditionNameTable )
	
--==============================================================================

local LrmOzRenditionServerUtils = {}

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

local function isAssetMetadataUploadedToOz( docValue )

	local spm = docValue._serverProvidedMetadata
	
	if spm then
	
		local revId = docValue._revId
		
		local revIds = spm.revision_ids
		
		if revIds then
		
			local cnt = #revIds
			
			for i = cnt, 1, -1 do
				
				local ozRevId = revIds[ i ]
				if revId == ozRevId then
					return true
				end
				
			end
			
		end
		
	end
	
	return false
	
end

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

function LrmOzRenditionServerUtils.shouldDownloadOzRendition( docValue, ozRenditionName )


	-- If we're still waiting for docValue, punt for now.

	if not docValue then
		return false
	end

	-- Is local version current? If so, nothing else matters.
	
	local localDescription = derefOpt( docValue, '_localOnly', ozRenditionName )

	local localRenditionIsForRev = localDescription and localDescription.revId
	
	-- Extract the revision ID from the server link for this rendition.
	
	local ozLinkName = ozLinkNameForOzRenditionName[ ozRenditionName ]

	local serverRevisionId = derefOpt( docValue,
													'_serverProvidedMetadata',
													'links',
													ozLinkName,
													'href' )

	local serverRevisionInvalid = derefOpt( docValue,
													'_serverProvidedMetadata',
													'links',
													ozLinkName,
													'invalid' )
			
	if not serverRevisionId then
		return false
	end
	
	if serverRevisionId then
		-- TODO [arahn 2013-05-29]: WHY are we splitting apart the URL like this?
		-- we could just use the renditionUrl in its entirety to decide
		-- if the local rendition matches.  see StopTheRenditionInsanity elsewhere
		-- in this file.
		serverRevisionId = serverRevisionId:match( '([0-9a-f]+)%/renditions%/([0-9a-f]+)$' )
	end
	
	if localDescription
		and localDescription.revId
		and localDescription.md5
	then

		if docValue
			and ( ( localRenditionIsForRev ~= serverRevisionId ) and isAssetMetadataUploadedToOz( docValue ) )
			and ( not serverRevisionInvalid )
		then
			return true
		end

		return false

	else
	end

	-- Local isn't current: Download version from Oz unless Oz thinks it's invalid.
	
	local serverLinks = derefOpt( docValue, '_serverProvidedMetadata', 'links' )
	local serverDescription = derefOpt( serverLinks, ozLinkName )
	
	local result = serverDescription and not serverDescription.invalid

	return result

end

return LrmOzRenditionServerUtils
