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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 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.rxlua
local WFRxEventBridge = require 'WFRxEventBridge'
 
	-- WF.core.reactive
local KSReactive = require 'KSReactive'

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

	-- WF.oz_client.catalog.avatar_server
local WFOzAvatarServer = require 'WFOzAvatarServer'

	-- WF.lrmobile
local LrmEpochModel = require 'LrmEpochModel'

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

local LrmEpochModel_currentEpochB = assert( LrmEpochModel.currentEpochB )
local LrmEpochModel_currentScrollingStoppedEpochB = assert( LrmEpochModel.currentScrollingStoppedEpochB )

local asKSEvent = WFRxEventBridge

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

local LrmLibraryRenditionModelImp = {}

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

local function lazilyInitializedAvatarServer( self )

	if not self._avatarServer then
	
		local catalog = self.catalog
		
		local avatarServer = WFOzAvatarServer {
			catalog = catalog,
			docStore = catalog:docStore(),
			indexStore = catalog:indexStore(),
		}
		
		self._avatarServer = avatarServer
		
	end
	
end

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

function LrmLibraryRenditionModelImp:assetThumbnailRenditionModel( assetId, md5Hint, alreadyInCache )
	
	if md5Hint == "" then
		md5Hint = nil
	end
	
	local thumbnailB = nil
	if assetId ~= "" then
		
		thumbnailB = self._renditionServer
		
			:call( asKSEvent )
			
			:closingHoldB( WFUnknown.value() )
			
			:closingKnownMapSwitchB( function( renditionServer )
			
				return renditionServer:thumbnailB( assetId, true, md5Hint,
														LrmEpochModel_currentEpochB(),
														LrmEpochModel_currentScrollingStoppedEpochB(), alreadyInCache )
														
			end )
		
		:closingMapB( function( rendition )
		
			if rendition == nil then
				return WFUnknown.value()
			end
			
			return rendition
		end )
	else
		thumbnailB = KSReactive.constantB( WFUnknown.value() )
	end
	
	return {
		valueB = KSReactive.tableB {
			image = thumbnailB
		},
		
		models = {
		},
		
		events = {
		},
		
		close = function( self )
--			debug.print( "del thumb" )
		end
	}
end

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

function LrmLibraryRenditionModelImp:assetPreviewRenditionModel( assetId, md5Hint, alreadyInCache )
	
	local imageB = nil
	
	if assetId ~= "" then
		imageB = self._renditionServer
		
			:call( asKSEvent )
			
			:closingHoldB( WFUnknown.value() )
			
			:closingKnownMapSwitchB( function( renditionServer )
			
				return renditionServer:previewB( assetId, false, true, false, alreadyInCache )
				
			end )
			
			:closingMapB( function( rendition )
			
				if rendition == nil then
					return WFUnknown.value()
				end
				
				return rendition
				
			end )
			
	else
		imageB = KSReactive.constantB( WFUnknown.value() )
	end
	
	return {
		valueB = KSReactive.tableB {
			image = imageB
		},
		
		models = {
		},
		
		events = {
		},
		
		close = function( self )
		
--			debug.print( "del prev" )
		end
	}
end

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

function LrmLibraryRenditionModelImp:avatarModel( feedbackuserId, avatarSize )

	lazilyInitializedAvatarServer(self)
	
	local avatarB = self._avatarServer:avatarB( feedbackuserId, avatarSize )
	
	return {
		value = avatarB,
	}
	
end

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

function LrmLibraryRenditionModelImp:makeAvatarModel( userId )

	lazilyInitializedAvatarServer(self)
	
	return {
		value = self._avatarServer:avatarB( userId, 'medium' )
	}
	
end

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

return LrmLibraryRenditionModelImp
