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

NmbCollaboratorTest.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 WFRxTestCase = require 'WFRxTestCase'

local KSData = require 'KSData'
local KSIdGenerator = require 'KSIdGenerator'
local NmbCollaborator = require 'NmbCollaborator'
local WFCatMockCatalog = require 'WFCatMockCatalog'
local WFFileUtils = require 'WFFileUtils'
local WFRxRunUntilDone = require 'WFRxRunUntilDone'

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

local NmbCollaboratorTest = {}

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

local function createFeedbackUser( i )
	return {
		_id = KSIdGenerator.uuidInLowercaseHex(),
		type = 'feedbackuser',
		
		_serverProvidedMetadata = {
			full_name = string.format( 'Full Name %s', tostring( i ) ),
			email = string.format( 'person_%s@gmail.com', tostring( i ) ),
			links = {
				[ '/rels/avatars/medium' ] = {
					href = '/v2/dummy_avatar_medium',
				},
				[ '/rels/avatars/large' ] = {
					href = '/v2/dummy_avatar_large',
				},
			},
		},
	}
end


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

local MockSynchronizerMethods = {}

function MockSynchronizerMethods:isSyncRunningNow()
	
	return true

end

local MockSynchronizer = Rx.Class( 'WFCatSynchronizer', MockSynchronizerMethods )

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

local expectedResponses = {
	{
		url = '/v2/dummy_avatar_medium',
		response = KSData.make( 'dummy_medium_avatar_content' ),
		headers = { status_code = '200' },
	},
	{
		url = '/v2/dummy_avatar_large',
		response = KSData.make( 'dummy_large_avatar_content' ),
		headers = { status_code = '200' },
	},
}

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

function NmbCollaboratorTest.rxTestAvatar( rxTest )

	rxTest:setTimeLimit( 10 )
	
	local catalog = WFCatMockCatalog.createFakeWFCatalog( rxTest, nil, expectedResponses )
	local docStore = catalog:docStore()
	
	local feedbackUsers = {
		createFeedbackUser( 1 ),
	}
	
	docStore:bulkSetDocValue( feedbackUsers ):call( WFRxRunUntilDone )
	
	local mediumPath
	
	local getAvatar = rxTest:observe(
		NmbCollaborator.getAvatar( catalog, MockSynchronizer(), feedbackUsers[ 1 ]._id, 'medium' )
			:map( function( path )
				if path and WFFileUtils.exists( path ) and string.find( path, 'medium' ) then
					mediumPath = path
					return 'medium'
				else
					return false
				end
			end )
	)
	
	getAvatar:expectDataWithinTime( 5, 'medium' )
	
	-- If we request for a smaller avatar than what we already have, we just get what we have.
	-- No downloads
	local smallAvatar = rxTest:observe(
		NmbCollaborator.getAvatar( catalog, MockSynchronizer(), feedbackUsers[ 1 ]._id, 'small' )
	)
	
	smallAvatar:expectDataWithinTime( 5, mediumPath )
	
	-- If we request for a larger avatar than what we already have, we first get the 
	-- lower quality one, which is then replaced by the downloaded high-quality avatar
	local largeAvatar = rxTest:observe(
		NmbCollaborator.getAvatar( catalog, MockSynchronizer(), feedbackUsers[ 1 ]._id, 'large' )
			:map( function( path )
				if path == mediumPath then
					return 'medium'
				end
				
				if path and 
					string.find( path, 'large' ) and 
					WFFileUtils.exists( path ) and 
					WFFileUtils.exists( mediumPath ) == false then
					return 'large'
				else
					return false
				end
			end )
	)
	
	largeAvatar:expectDataWithinTime( 5, 'medium' )
	largeAvatar:expectDataWithinTime( 5, 'large' )
	
end

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

return WFRxTestCase.wrapSuite( 'NmbCollaboratorTest', NmbCollaboratorTest )
