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

PixelProviderPerClientTaskCache.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2016 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
local KSTableUtils = require 'KSTableUtils'
local WFArguments = require 'WFArguments'

	-- Rx
local Rx = require "Rx"

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

local WFArguments_check = assert( WFArguments.check,
										"missing WFArguments.check" )

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

local KSTableUtils_derefOpt = assert( KSTableUtils.derefOpt,
										"KSTableUtils.derefOpt does not exist" )

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

local PixelProviderPerClientTaskCachePrototype = {

	_cacheData = false,
		-- a strong lua table of strong lua tables. The outer strong table uses
		-- localAssetId for the keys. The inner lua tables use the clientId
		-- and the values are the cache data

}

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

function PixelProviderPerClientTaskCachePrototype:__init()

	WFArguments_check( 'PixelProviderPerClientTaskCache' )

	self._cacheData = {}

end

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

function PixelProviderPerClientTaskCachePrototype:peekCacheItem( clientId, 
															localAssetId )

	WFArguments_check( 'PixelProviderPerClientTaskCache', 'string', 'number' )

	return KSTableUtils_derefOpt( self._cacheData, localAssetId, clientId )

end

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

function PixelProviderPerClientTaskCachePrototype:popCacheItem( clientId, 
														localAssetId )

	WFArguments_check( 'PixelProviderPerClientTaskCache', 'string', 'number' )

	local item = self:peekCacheItem( clientId, localAssetId )

	if item ~= nil then

		self._cacheData[ localAssetId ][ clientId ] = nil

	end

	return item

end

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

function PixelProviderPerClientTaskCachePrototype:destroyCacheItem( clientId, 
															localAssetId )

	WFArguments_check( 'PixelProviderPerClientTaskCache', 'string', 'number' )

	self:popCacheItem( clientId, localAssetId )

	if not next( self._cacheData[ localAssetId ] or {} ) then

		self._cacheData[ localAssetId ] = nil

	end

end

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

function PixelProviderPerClientTaskCachePrototype:destroyAllLocalAssetCacheItems( 
															localAssetId )


	WFArguments_check( 'PixelProviderPerClientTaskCache', 'number' )

	for clientId in pairs( self._cacheData[ localAssetId ] or {} ) do

		self:destroyCacheItem( clientId, localAssetId )

	end

end

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

function PixelProviderPerClientTaskCachePrototype:destroyAll()


	WFArguments_check( 'PixelProviderPerClientTaskCache' )

	for localAssetId in pairs( self._cacheData ) do

		self:destroyAllLocalAssetCacheItems( localAssetId )

	end

	self._cacheData = {}

end

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

function PixelProviderPerClientTaskCachePrototype:pushCacheItem( 
											clientId, localAssetId, item, 
											... )

	WFArguments_check( 'PixelProviderPerClientTaskCache', 'string', 'number' )

	assert( select( '#', ... ) == 0, 
			"multiple items not allowed in pushCacheItem" )

	-- destroy any existing cache info
	self:destroyCacheItem( clientId, localAssetId )

	if item == nil then

		error( "Cannot push nil item" )	

	end

	local assetIdCache = self._cacheData[ localAssetId ] or {}

	self._cacheData[ localAssetId ] = assetIdCache 

	assetIdCache[ clientId ] = item

	return true

end

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

local PixelProviderPerClientTaskCache =
		Rx.Class( "PixelProviderPerClientTaskCache",
					PixelProviderPerClientTaskCachePrototype )

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

local function makePerClientLocalAssetSimpleDataCache()

	return PixelProviderPerClientTaskCache()
	
end

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

local PixelProviderPerLocalAssetTaskCachePrototype = {

	_cacheData = false,
		-- a strong lua table of cache data, the keys are localAssetId

}

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

function PixelProviderPerLocalAssetTaskCachePrototype:__init()

	WFArguments_check( 'PixelProviderPerLocalAssetTaskCache' )

	self._cacheData = {}

end

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

function PixelProviderPerLocalAssetTaskCachePrototype:peekCacheItem( localAssetId )

	WFArguments_check( 'PixelProviderPerLocalAssetTaskCache', 'number' )

	return self._cacheData[ localAssetId ]

end

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

function PixelProviderPerLocalAssetTaskCachePrototype:popCacheItem( localAssetId )

	WFArguments_check( 'PixelProviderPerLocalAssetTaskCache', 'number' )

	local item = self:peekCacheItem( localAssetId )

	if item ~= nil then

		self._cacheData[ localAssetId ] = nil

	end

	return item

end

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

function PixelProviderPerLocalAssetTaskCachePrototype:destroyCacheItem( 
															localAssetId )

	WFArguments_check( 'PixelProviderPerLocalAssetTaskCache', 'number' )

	self:popCacheItem( localAssetId )

end

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

function PixelProviderPerLocalAssetTaskCachePrototype:pushCacheItem( 
											localAssetId, item, ... )

	WFArguments_check( 'PixelProviderPerLocalAssetTaskCache', 'number' )

	assert( select( '#', ... ) == 0, 
			"multiple items not allowed in pushCacheItem" )

	-- destroy any existing cache info
	self:destroyCacheItem( localAssetId )

	if item == nil then

		error( "Cannot push nil item" )	

	end

	self._cacheData[ localAssetId ] = item

	return true

end

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

function PixelProviderPerLocalAssetTaskCachePrototype:destroyAll()


	WFArguments_check( 'PixelProviderPerLocalAssetTaskCache' )

	for localAssetId in pairs( self._cacheData ) do

		self:destroyCacheItem( localAssetId )

	end

	self._cacheData = {}

end

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

local PixelProviderPerLocalAssetTaskCache =
		Rx.Class( "PixelProviderPerLocalAssetTaskCache",
					PixelProviderPerLocalAssetTaskCachePrototype )

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

local function makeLocalAssetSimpleDataCache()

	return PixelProviderPerLocalAssetTaskCache()

end

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


return {

	makePerClientLocalAssetSimpleDataCache = makePerClientLocalAssetSimpleDataCache,

	makeLocalAssetSimpleDataCache = makeLocalAssetSimpleDataCache,

}
