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

LrmCoreInfoIndexHelper.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2019 Adobe
All Rights Reserved.

NOTICE: All information contained herein is, and remains
the property of Adobe and its suppliers, if any. The intellectual
and technical concepts contained herein are proprietary to Adobe
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.

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

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

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

local LrAssetUtils = require "LrAssetUtils"
local LrmAssetUtils = require "LrmAssetUtils"
local LrmCatSyncStatus = require "LrmCatSyncStatus"
local WFCatTrashUtils = require 'WFCatTrashUtils'

	-- WF.oz_client.managed_catalog
local WFCatIndexUtils = require 'WFCatIndexUtils'

	-- WF.core.logger
local WFLogger = require 'WFLogger'

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

local logger = WFLogger( 'LrmCoreInfoIndexHelper' )

local derefOpt = assert( KSTableUtils.derefOpt )
local WFCatTrashUtils_originalExistsOnOz = assert( WFCatTrashUtils.originalExistsOnOz )

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

-- @mark LrmCoreInfoIndexHelper namespace

local LrmCoreInfoIndexHelper = {}

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

function LrmCoreInfoIndexHelper.getCoreInfoCommonFields()

	return {
		"captureDate", "importTimestamp", "modifiedDate", "contentType", "duration", "flag", "rating",
		"croppedWidth", "croppedHeight", "fileName", "width", "height", "apertureValue", "isoSpeedRatings", 
		"exposureTime", "fnumber", "cameraMaker", "cameraModel", "isVideo", "localUrl", "isMergedHDR",
		"hasThumbnail", "importedByUser", "syncStatus", "spaceId"
	}

end

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

function LrmCoreInfoIndexHelper.getCoreInfoCommonFieldsValue( docValue )
	
	local hasThumbnail = LrmCoreInfoIndexHelper.hasThumbnail( docValue )

	local importTimestamp
	local fileName
	local width
	local height
			
	local importSource = docValue.importSource
			
	if type( importSource ) == 'table' then

		fileName = importSource.fileName
		importTimestamp = importSource.importTimestamp
		width = importSource.originalWidth
		height = importSource.originalHeight
	
	elseif importSource then
		logger:errorf( "importSource not a table for assetId %s", docValue._id )
	end
	
	-- Capture date may have a "Z" appended to the end, signifying that the
	-- timestamp is in UTC (20 chars). Otherwise, the timestamp is treated as
	-- local time (19 chars). Either way, we just care about getting the
	-- YYYY-MM-DD from the beginning of the string.

	local captureDate = docValue.captureDate
	local modifiedDate = docValue.userUpdated or docValue.changedAtTime or captureDate
			
	local contentTypeNumber = LrmAssetUtils.getContentTypeNumber( docValue )
			
	-- Cropped width and height win.
	local croppedWidth = width
	local croppedHeight = height
	
	local develop = derefOpt( docValue, 'develop' )
	if type( develop ) == 'table' then
		if develop.croppedWidth and develop.croppedHeight then
			croppedWidth = develop.croppedWidth
			croppedHeight = develop.croppedHeight
		end
	elseif develop then
		logger:errorf( "develop not a table for assetId %s", docValue._id )
	end
			
	local isVideo = docValue.subtype == 'video' or docValue.subtype == 'deleted_video'

	local duration = WFCatIndexUtils.getDuration( docValue )
	duration = duration and math.ceil( duration )
			
	local rating = LrmAssetUtils.getDefaultAssetRating( docValue )
	local flag = LrmAssetUtils.getDefaultAssetReview( docValue )
	local xmp = LrmAssetUtils.getXmp( docValue )
			
	local localUrl = LrmAssetUtils.getLocalUrl( docValue ) -- maybe nil!

	local isMergedHDR = not not derefOpt( docValue, 'xmp', 'aux', 'IsMergedHDR' )

	local importedByUser = derefOpt( docValue, '_serverProvidedMetadata', 'importedByUser', 'id' ) -- maybe nil!

	local syncStatus = LrmCatSyncStatus.getAssetSyncStatus( docValue )

	local spaceId = docValue._spaceId

	return #LrmCoreInfoIndexHelper.getCoreInfoCommonFields(), {
		captureDate, importTimestamp, modifiedDate, contentTypeNumber, duration, flag, rating,
		croppedWidth, croppedHeight, fileName, width, height, xmp.apertureValue, xmp.isoSpeedRatings, 
		xmp.exposureTime, xmp.fnumber, xmp.cameraMake, xmp.cameraModel, isVideo, localUrl, isMergedHDR,
		hasThumbnail, importedByUser, syncStatus, spaceId
	}

end

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

function LrmCoreInfoIndexHelper.assetHasMasterOrProxy( docValue )
	
	local hasProxy = false
	local hasMaster = false

	-- If master or proxy locally

    local hasProxyLocally = LrAssetUtils.hasProxyLocally( docValue )

	if hasProxyLocally then
		hasProxy = true
	end

    local hasMasterLocally = LrAssetUtils.hasMasterLocally( docValue )

	-- INFO [pkamin 2015-07-08]: we also allow assets with referenced
	-- only masters

	local localUrl = derefOpt( docValue, '_localOnly', 'localUrl' )

	if hasMasterLocally or localUrl then
		hasMaster = true
	end

	-- if master, proxy on oz for trash asset, original asset is deleted locally, and original asset is on Oz
	-- if master, proxy on Oz

	hasProxy = hasProxy 
					or WFCatTrashUtils_originalExistsOnOz( docValue )
					or derefOpt( docValue, '_serverProvidedMetadata', 'links', '/rels/proxy_type/2560', 'href' ) ~= nil
					or derefOpt( docValue, '_localOnly', 'sourceProxyLink' ) ~= nil
						-- Assets that have been copied to a shared-with-you album but not yet uploaded may have a
						-- link to the source proxy, but not proxy itself.
					
	hasMaster = hasMaster 
					or WFCatTrashUtils_originalExistsOnOz( docValue )
					or derefOpt( docValue, '_serverProvidedMetadata', 'links', '/rels/master', 'href' ) ~= nil
					or derefOpt( docValue, '_localOnly', 'sourceOriginalLink' ) ~= nil
						-- Assets that have been copied to a shared-with-you album but not yet uploaded may have a
						-- link to the source master, but not master itself.
					
	local has2048Rendition = derefOpt( docValue, '_serverProvidedMetadata', 'links', '/rels/rendition_type/2048', 'href' ) ~= nil
		-- Most shared-With-You albums don't have a proxy/master available. Only a 2048 rendition.
		
	if hasProxy or hasMaster or has2048Rendition then
		return true
	end

	return false
	
end

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

function LrmCoreInfoIndexHelper.hasThumbnail( docValue )

	if derefOpt( docValue, '_serverProvidedMetadata', 'links', '/rels/rendition_type/thumbnail2x', 'href' ) then
		return true
	end

	if WFPlatformInfo.isAndroid then

		local thumbnail2x = derefOpt( docValue, '_localOnly', 'thumbnail2x' )
	
		if type( thumbnail2x ) == 'table' then
		
			if thumbnail2x.md5 then
				return true
			end
			
		elseif thumbnail2x then
			logger:errorf( "thumbnail2x not a table for assetId %s", docValue._id )
		end

	else

		local thumbnail2x = derefOpt( docValue, '_localOnly', 'files', 'thumbnail2x' )
		
		if type( thumbnail2x ) == 'table' then
		
			if thumbnail2x.path then
				return true
			end
			
		elseif thumbnail2x then
			logger:errorf( "thumbnail2x not a table for assetId %s", docValue._id )
		end
	
	end

	return false

end


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

return LrmCoreInfoIndexHelper
