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

LrmTrashIndexDefinitions.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2018 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 WFArguments = require 'WFArguments'

local WFPlatformInfo = require 'WFPlatformInfo'

local WFCatTrashUtils = require 'WFCatTrashUtils'

local LrmCacheIndexDefinitions = require 'LrmCacheIndexDefinitions'

local LrmCoreInfoIndexHelper = require 'LrmCoreInfoIndexHelper'

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

local derefOpt = assert( KSTableUtils.derefOpt )
local check = assert( WFArguments.check )

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

-- @mark LrmTrashIndexDefinitions namespace

local LrmTrashIndexDefinitions = {}

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

function LrmTrashIndexDefinitions.assetIsDisplayable( docValue )

	local docType = docValue.type
	local subtype = docValue.subtype

	if docType ~= 'asset' then return false end
	
	if subtype ~= 'deleted_image' and subtype ~= 'deleted_video' then 
		return false 
	end

    -- do not process the deleted asset if purgeDate is not present
	if not docValue.purgeDate then return false end

	return LrmCoreInfoIndexHelper.assetHasMasterOrProxy( docValue )

end

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

function LrmTrashIndexDefinitions.generateIndexSchema( indexStoreSchema )

	check( 'WFRxIndexStoreSchema' )

	-- table Trash

	local coreInfoCommonFields = LrmCoreInfoIndexHelper.getCoreInfoCommonFields()
	
	local fields = { "purgeDate", "originalFileSize" }
	
	if WFPlatformInfo.isAndroid then
		fields = { "purgeDate", "originalFileSize", "thumbnailMD5" }
	end
	
	for _, field in pairs( coreInfoCommonFields ) do
		fields[ #fields+1 ] = field
	end
	
	local relevantTypeSubtypeTuples = {
		{ 'asset', 'deleted_image' },
		{ 'asset', 'deleted_video' },
	}

	indexStoreSchema:addTable {

		tableName = "trashAssets",

		fields = fields,
		
		relevantTypeSubtypeTuples = relevantTypeSubtypeTuples,

		map = function( docValue, emit )

			if docValue._deleted then
				return
			end

			if not LrmTrashIndexDefinitions.assetIsDisplayable( docValue ) then
				return
			end

			local purgeDate = docValue.purgeDate
			
			local originalFileSize = derefOpt( docValue, 'importSource', 'fileSize' )
			
			-- Assets imported from Lr Classic only upload proxy to Oz. 
			-- Should only use proxy size for Lr Classic imported assets
			if WFCatTrashUtils.getProxyIsUploaded( docValue ) and not WFCatTrashUtils.getMasterIsUploaded( docValue ) then
			
				if WFPlatformInfo.isAndroid then
					originalFileSize = derefOpt( docValue, '_localOnly', 'proxy', 'size' ) 
						or LrmCacheIndexDefinitions.defaultSizeForRendition[ 'proxy' ]
				else
					originalFileSize = derefOpt( docValue, '_localOnly', 'files', 'proxy', 'fileSize' ) 
						or LrmCacheIndexDefinitions.defaultSizeForRendition[ 'proxy' ]
				end
			
			end
			

			local nCoreInfoCommonFieldsValue, coreInfoCommonFieldsValue = LrmCoreInfoIndexHelper.getCoreInfoCommonFieldsValue( docValue )
			
			if WFPlatformInfo.isAndroid then
				
				originalFileSize = originalFileSize or derefOpt( docValue, '_localOnly', 'original', 'size' ) or 0
				
				local thumbnail = derefOpt( docValue, '_localOnly', 'thumbnail2x' )
            	local thumbnailMD5 = thumbnail and thumbnail.md5
				
				emit( purgeDate, originalFileSize, thumbnailMD5, unpack( coreInfoCommonFieldsValue, 1, nCoreInfoCommonFieldsValue ) )
				
			else
				
				originalFileSize = originalFileSize or derefOpt( docValue, '_localOnly', 'files', 'master', 'fileSize' ) or 0
			
				emit( purgeDate, originalFileSize, unpack( coreInfoCommonFieldsValue, 1, nCoreInfoCommonFieldsValue ) )
			
			end

		end,

	}

	indexStoreSchema:addIndex [[CREATE INDEX trash_purgeDateCaptureDateFileNameDocId ON trashAssets( purgeDate, captureDate, fileName COLLATE i18n_collate, docId ) ]]

end

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

return LrmTrashIndexDefinitions
