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

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

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

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

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

-- @mark WFOzAvatarServerUtils

local WFOzAvatarServerUtils = {}

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

local check = assert( WFArguments.check )

local cacheEpochSize = 20 * 1024 * 1024

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

function WFOzAvatarServerUtils.cacheEpochB( docStore, indexStore )

	local maxEpochB = indexStore:queryB {
		sql = 'SELECT max( epoch ) FROM avatarCacheReferences',
		calm = 10,
	}
		:closingMapB( function( rows )
			return rows and rows[ 1 ] and rows[ 1 ][ 1 ] or 1
		end )
	
	local epochSizeB = maxEpochB:closingMapSwitchB( function( maxEpoch )
	
		return indexStore:queryB {
			sql = 'SELECT epoch, sum( size ) FROM avatarCacheReferences WHERE epoch = ?',
			calm = 10,
			maxEpoch,
		}
		
	end )
	
	:closingMapB( function( rows )
		local row1 = rows and rows[ 1 ]
		if row1 then
			return ( row1[ 1 ] or 1 ), ( row1[ 2 ] or 0 )
		else
			return 1, 0
		end
	end )
	
	:addCloseTarget( maxEpochB )
		
	local resultB = epochSizeB:closingMapB( function( epoch, size )
		
		if size >= cacheEpochSize then
			return epoch + 1
		else
			return epoch
		end
	
	end )
	
	docStore:addCloseTarget( resultB )
	
	return resultB:autoCloseRelayB()
										
end

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

function WFOzAvatarServerUtils.touchAvatar( docStore, cacheEpochB,
												  userId, avatarType, optDocValue )
	
	if not optDocValue then
	
		docStore:docValueOnceP( 'feedbackuser', userId )
			:whenResolvedDo( function( status, docValue )
			
				if status == 'data' then
					WFOzAvatarServerUtils.touchRendition( docStore, cacheEpochB, userId,
															 avatarType, docValue )
				end

			end ):runUntilDone()
			
		return
		
	end
		
	local avatarInfo = KSTableUtils.derefOpt( optDocValue, '_localOnly', 'avatars', avatarType )
										
	local epochNow = cacheEpochB:valueNow()
	cacheEpochB:close()
	
	if not epochNow then
		return
	end
	
	if avatarInfo and avatarInfo.epoch == epochNow then
		return
	end
	
	local overlay = {
		epoch = epochNow
	}
							
	docStore:applyOverlayToDocumentP {
		
		docType = 'feedbackuser',
		docId = userId,
		
		overlay = {
			_localOnly = {
				avatars = overlay,
			}
		},
	}
											
end



return WFOzAvatarServerUtils
