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

WFOzTestUtils.lua

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

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.

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

local KSIdGenerator = require 'KSIdGenerator'

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

local WFOzTestUtils = {}

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

WFOzTestUtils.v1 = {}

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

WFOzTestUtils.v2 = {}

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

function WFOzTestUtils.v1.removeFragileFieldsFromUser( t )

	if t then
	
		for k, v in pairs( t ) do
	
			if k == 'entitlement' then

				t[ k ] = nil
			
			elseif k == 'usage' then
			
				t[ k ] = nil
			
			elseif k == 'raw_upload' then
			
				t[ k ] = nil
			
			end
		
			if type( v ) == 'table' then
		
				WFOzTestUtils.v1.removeFragileFieldsFromUser( v )
			
			end
			
		end
	
	end
	
	return t

end

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

function WFOzTestUtils.v1.removeFragileFieldsFromCatalog( t )

	if t then
	
		for k, v in pairs( t ) do
	
			if k == 'storage' then

				t[ k ] = nil
			
			elseif k == 'shaHash' then
			
				t[ k ] = nil
			
			end
		
			if type( v ) == 'table' then
		
				WFOzTestUtils.v1.removeFragileFieldsFromCatalog( v )
			
			end
			
		end
	
	end
	
	return t

end

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

function WFOzTestUtils.v1.removeFragileFieldsFromAvatarsLinks( t )

	if t then
	
		for k, v in pairs( t ) do
	
			if string.find( k, "/rels/avatars/" ) then

				v.size = "ZEROED"
				v.href = nil
			
			end
			
		end
	
	end
	
	return t

end

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

function WFOzTestUtils.v2.makeBaseValidAssetDocument( optId )
			
	local minimalAssetDoc = {
		_id = optId,
		type = 'asset',
		subtype = 'image',
		captureDate = '2012-08-15T15:22:29',
		_localOnly = { 
			files = { 
				original = { 
					master = {
						relativePath = 'some/path',
						volumeGuid = '<guid>'
					}
				} 
			} 
		},
		importSource = {
			originalWidth = 1,
			originalHeight = 1,
			fileName = "default_image_path.jpg",
			sha256 = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c',
			importedBy = "98db98bfd554470dab2f862a4b853c40",
			importTimestamp = '2012-08-15T15:22:29Z',
		}
	}
	
	return minimalAssetDoc
	
end

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

function WFOzTestUtils.v2.makeAdderOfAsset( rxTest, docStore, optAssetModFn )

	local assetCounter = 1
	
	return function( ... )
		
		local defaultId = string.format( '%032x', assetCounter )
			
		local docValue = WFOzTestUtils.v2.makeBaseValidAssetDocument( defaultId )
		
		if type( optAssetModFn ) == 'function' then
			docValue = optAssetModFn( docValue, ... )
		end
		
		local revId
		rxTest:expectSingleValue( 
			docStore:setDocValue( docValue )
				:tap( function( newRevId )
					revId = newRevId
				end ), ':ignore:' )
		
		assetCounter = assetCounter + 1

		return docValue._id, revId
		
	end
	
end

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

function WFOzTestUtils.v2.makeAdderOfNAssets( rxTest, docStore, n )

	return function( )
		
		local assetDocs = {}
		for i = 1, n, 1 do
		
			local defaultId = KSIdGenerator.uuidInLowercaseHex()
				
			local docValue = WFOzTestUtils.v2.makeBaseValidAssetDocument( defaultId )
			
			assetDocs[ #assetDocs + 1 ] = docValue
		
		end
		
		docStore:bulkSetDocValue( assetDocs )
			:call( require 'WFRxRunUntilDone' )

	end

end

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

function WFOzTestUtils.v2.makeAdderOfAlbum( rxTest, docStore, optAlbumModFn )

	local albumCounter = 1

	return function( ... )
		
		local defaultId = string.format( 'a1b1111100000000%016x', albumCounter )
		
		local docValue = {
				type = 'album',
				subtype = 'collection',
				_id = defaultId,
				name = string.format( "Album %d", albumCounter ),
			}
			
		if type( optAlbumModFn ) == 'function' then
			docValue = optAlbumModFn( docValue, ... )
		end
		
		local revId
		rxTest:expectSingleValue( 
			docStore:setDocValue( docValue )
				:tap( function( newRevId )
					revId = newRevId
				end ), ':ignore:' )

		albumCounter = albumCounter + 1

		return docValue._id, revId

	end
	
end

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

function WFOzTestUtils.v2.makeAdderOfProjectAlbum( rxTest, docStore )

	local albumCounter = 1

	return function( ... )
		
		local defaultId = string.format( 'a1b1111200000000%016x', albumCounter )
		
		local docValue = {
				type = 'album',
				subtype = 'project',
				_id = defaultId,
				name = string.format( "Album %d", albumCounter ),
			}
			
		
		local revId
		rxTest:expectSingleValue( 
			docStore:setDocValue( docValue )
				:tap( function( newRevId )
					revId = newRevId
				end ), ':ignore:' )

		albumCounter = albumCounter + 1

		return docValue._id, revId

	end
	
end

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

function WFOzTestUtils.v2.makeAdderOfAssetToAlbum( rxTest, docStore )

	local albumAssetCounter = 1

	return function( albumId, assetId )

		local id = string.format( 'a55e7a1b11110000%016x', albumAssetCounter )
		local revId
		rxTest:expectSingleValue(

			docStore:setDocValue {
				type = 'album_asset',
				_id = id,
				album = {
					id = albumId,
				},
				asset = {
					id = assetId,
				},
			}
			:tap( function( newRevId )
				revId = newRevId
			end ),

			':ignore:' )

		albumAssetCounter = albumAssetCounter + 1

		return id, revId

	end
end

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

function WFOzTestUtils.v2.observeDocValueIsEmpty( rxTest, docStore, docId )
	
	return rxTest:observe( docStore:ongoingDocValue( docId )
		:map( function( doc )
			return not not ( doc and doc._empty == true )
		end ) )

end

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

return WFOzTestUtils
