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

NmbCollaborator.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.

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

local Rx = require 'Rx'
local Observable = Rx.Observable

local KSIdGenerator = require 'KSIdGenerator'
local KSPathUtils = require 'KSPathUtils'
local KSTableUtils = require 'KSTableUtils'
local WFAsyncFileUtils = require "WFAsyncFileUtils"
local WFCatCollaborator = require "WFCatCollaborator"
local WFFileUtils = require 'WFFileUtils'

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

local derefOpt = KSTableUtils.derefOpt

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

-- @mark WFCatCollaborator namespace

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

local NmbCollaborator = {
	WFCatCollaborator,
}

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

-- Return an identifier to the persisted avatar (path), the resolution and the link it was downloaded from
function NmbCollaborator:_getLocalAvatarInfo( feedbackuserDoc )

	local localAvatars = derefOpt( feedbackuserDoc, '_localOnly', 'universal', 'files', 'avatar' ) or {}
	
	for localAvatarResolution, localAvatarInfo in pairs( localAvatars ) do
	
		local localAvatarPath = derefOpt( localAvatarInfo, 'path' )
		local localAvatarLink = derefOpt( localAvatarInfo, 'downloadedLink' )
	
		if localAvatarPath and WFFileUtils.exists( localAvatarPath ) == 'file' then
			return localAvatarPath, localAvatarResolution, localAvatarLink
		end
		
	end
	
	return nil
	
end
	
--------------------------------------------------------------------------------

-- Return a boolean indicating whether or not the avatar should be downloaded
function NmbCollaborator:_canDownloadAvatar( wfCatSynchronizer )
	return wfCatSynchronizer:isSyncRunningNow()
end
	
--------------------------------------------------------------------------------

-- Save the avatar binary given the bits and the link it was downloaded from
-- Return an observable that fires an overlay to be added to the feedbackuser document
function NmbCollaborator:_saveAvatar( catalog, response, downloadedResolution, downloadedLink )

	local catFileSpec = catalog:fileSpec()
	local avatarFolder = catFileSpec:catalogPath()

	avatarFolder = KSPathUtils.child( avatarFolder, 'collaborators' )
	WFFileUtils.mkdir( avatarFolder )
	
	local filePath = downloadedResolution .. '_' .. KSIdGenerator.uuidInLowercaseHex()
	
	filePath = KSPathUtils.child( avatarFolder, filePath )
	
	return WFAsyncFileUtils.saveToFile( response, filePath )
		:map( function()
		
			return {
				_localOnly = {
					universal = {
						files = {
							avatar = {
								[ downloadedResolution ] = {
									path = filePath,
									downloadedLink = downloadedLink,
								}
							}
						}
					}
				}
			}, downloadedResolution

		end )
end
	
--------------------------------------------------------------------------------

-- Delete the persisted avatar and return an observable that fires the overlay
-- to be added to the feedbackuser document
function NmbCollaborator:_deleteLocalAvatar( feedbackuserDoc )

	local existingAvatarInfo = derefOpt( feedbackuserDoc, '_localOnly', 'universal', 'files', 'avatar' )
	
	local overlay = {
		_localOnly = {
			universal = {
				files = {
					avatar = {}
				}
			}
		}
	}
	
	local overlayAvatar = overlay._localOnly.universal.files.avatar
	
	for existingResolution in pairs( existingAvatarInfo or {} ) do
		
		local existingPath = derefOpt( feedbackuserDoc, '_localOnly', 'universal', 'files', 'avatar', existingResolution, 'path' )
		
		if existingPath then
			pcall( WFFileUtils.deleteFile, existingPath )
		end
		
		overlayAvatar[ existingResolution ] = { _delete = true }
		
	end

	return Observable.just( overlay )
end

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

local NmbCollaboratorClass = Rx.Class 'NmbCollaborator'( NmbCollaborator )

local s_nmbCollaboratorInstance = NmbCollaboratorClass()

local getAvatar = function( ... )
	return s_nmbCollaboratorInstance:getAvatar( ... )
end

return {
	getAvatar = getAvatar,
}
