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

WFOzDocIndexUtils.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2011 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.

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

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

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

local derefOpt = assert( KSTableUtils.derefOpt )

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

local WFOzDocIndexUtils = {}

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

function WFOzDocIndexUtils.assetHasThumbnail( docValue )

	-- Per discussion on bug #2942278, this filter should be applied to all
	-- indexes that drive user-visible content. (Note, for instance, that we
	-- exclude it from the debug-only upload status filter.)

	-- Exclude documents other than assets.
	-- WFDocStore uses type; OzDocStore uses _docType. We try to be compatible
	-- with both here.

	if docValue._docType then
		if docValue._docType ~= 'asset' then return false end
	else
		if docValue.type ~= 'asset' then return false end
	end
	
	-- Include if we have a local thumbnail.
	
	if derefOpt( docValue, '_localOnly', 'renditions', 'thumbnail', 'md5' ) then
		return true
	end
	
	-- Include if we can get a thumbnail from Oz.
	
	if derefOpt( docValue, '_serverProvidedMetadata', 'links',
				 '/rels/rendition_type/144h', 'href' )
	then
		return true
	end
	
	-- Include if we can get a 2xthumbnail from Oz.
	
	if derefOpt( docValue, '_serverProvidedMetadata', 'links',
				 '/rels/rendition_type/thumbnail2x', 'href' )
	then
		return true
	end
	
	-- No thumbnail? Pretend it doesn't exist yet.
	
	return false

end

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

function WFOzDocIndexUtils.coreInfoFromDocValue( docValue )

	local width = docValue.width
	local height = docValue.height
	
	-- Cropped width and height win.
	
	local develop = docValue.develop
	
	if develop and develop.croppedWidth and develop.croppedHeight then
		width = develop.croppedWidth
		height = develop.croppedHeight
	end
	
	-- Local only thumbnail wins.
	-- No, because this causes a lot of extra computation as large
	-- grid view results are re-sent when one thumbnail is loaded
	-- arahn 05/25/2013
	local localOnly = docValue._localOnly
	local thumbnail = KSTableUtils.derefOpt( localOnly, 'renditions', 'thumbnail' )
		or KSTableUtils.derefOpt( localOnly, 'thumbnail' )
	
	if width == nil and height == nil and thumbnail and thumbnail.referenceWidth and thumbnail.referenceHeight then
		width = thumbnail.referenceWidth
		height = thumbnail.referenceHeight
	end

	local aspectRatio

	if ( docValue._docType == 'asset' or docValue.type == 'asset' )
		and not docValue._deleted
		and type( width ) == 'number' and width > 0
		and type( height ) == 'number' and height > 0
	then
		aspectRatio = width / height
	end
	
	local thumbnailMD5 = thumbnail and thumbnail.md5
	
	local favoritedCount = 0
    local favoritedUsers = ""
	local favorites = docValue and docValue.favorites

	if favorites then
		for k, v in pairs( favorites ) do
			if v then
				favoritedCount = favoritedCount + 1
				if favoritedUsers ~= "" then
					favoritedUsers = favoritedUsers .. ','
				end
				favoritedUsers = favoritedUsers .. k
			end
		end
	end

	return aspectRatio, thumbnailMD5, favoritedCount, favoritedUsers

end

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

return WFOzDocIndexUtils
