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

WFCatIdUtilsTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2017 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.

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

	-- WF.core.self_test
local WFRxTestCase = require 'WFRxTestCase'

	-- WF.core.utils
local _ = require 'WFArguments_uuid'

	-- WF.oz_client.managed_catalog
local WFCatAlbums = require 'WFCatAlbums'
local WFCatIdUtils = require 'WFCatIdUtils'
local WFCatMockCatalog = require 'WFCatMockCatalog'

local WFOzTestUtils = require 'WFOzTestUtils'

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

local createFakeWFCatalog = WFCatMockCatalog.createFakeWFCatalog


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

local WFCatIdUtilsTest = {}

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

local function doTwice( rxTest, fn )

	fn() -- Should cause the query to fire.
	
	local count = WFCatIdUtils.debug_getQueryCount()
	
	fn() -- Should used the cached data.

	rxTest:expectEqual( count, WFCatIdUtils.debug_getQueryCount() )

end

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

function WFCatIdUtilsTest.rxTestUUIDs( rxTest )

	rxTest:setTimeLimit( 10 )

	WFCatIdUtils.debug_clearCaches()

	local wfCatalog = createFakeWFCatalog( rxTest )
	local docStore = wfCatalog:docStore()
	
	local albumName = 'testAlbum'
	local albumLocalId
	local albumGlobalId

	rxTest:expectOnlyCompleted( WFCatAlbums.createAlbum( wfCatalog, albumName )
		:tap( function( localAlbumId, globalAlbumId )
			albumLocalId = localAlbumId
			albumGlobalId = globalAlbumId
		end )
		:takeLast( 0 )
	)

	doTwice( rxTest, function() 
		rxTest:expectSingleValue(
		WFCatIdUtils.albumUuidForMaybeLocalId( wfCatalog, albumGlobalId ),
		albumGlobalId ) 
	end )

	doTwice( rxTest, function()
		rxTest:expectSingleValue(
		WFCatIdUtils.albumUuidForMaybeLocalId( wfCatalog, albumLocalId ),
		albumGlobalId )
	end )
	
	local addAssetFn = WFOzTestUtils.v2.makeAdderOfAsset( rxTest, docStore,
		function( baseDocValue, date)
			-- set date to what we want
			baseDocValue.captureDate = date
			return baseDocValue
		end )

	local globalIds, id = {}
	local idsAndTypes = {}
	
	id = addAssetFn( '2001' )
	table.insert( globalIds, id )
	table.insert( idsAndTypes, {
		id,
		'image'
	} )
	id = addAssetFn( '2002' )
	table.insert( globalIds, id )
	table.insert( idsAndTypes, {
		id,
		'image'
	} )
	id = addAssetFn( '2003' )
	table.insert( globalIds, id )
	table.insert( idsAndTypes, {
		id,
		'image'
	} )
	
	local localIds = {}
	for _, globalId in ipairs( globalIds ) do
		rxTest:expectSingleValue(
			docStore:docValue( globalId )
				:map( function( docValue )
					table.insert( localIds, docValue._localId )
					return docValue._localId
				end ), ':ignore:' )
	end
	
	doTwice( rxTest, function()
		rxTest:expectSingleValue(
		WFCatIdUtils.getOzAssetIdsForLocalIds( wfCatalog, globalIds ),
		globalIds
	) end )
	
	doTwice( rxTest, function()
		rxTest:expectSingleValue(
		WFCatIdUtils.getOzAssetIdsForLocalIds( wfCatalog, localIds ),
		globalIds
	) end )
	
	doTwice( rxTest, function()
		rxTest:expectSingleValue(
		WFCatIdUtils.getOzAssetIdsAndTypeForIds( wfCatalog, globalIds ),
		idsAndTypes
	) end )
	
	doTwice( rxTest, function()
		rxTest:expectSingleValue(
		WFCatIdUtils.getOzAssetIdsAndTypeForIds( wfCatalog, localIds ),
		idsAndTypes
	) end )
	
	local albumName2 = 'Foo'
	local albumLocalId2
	local albumGlobalId2
	
	rxTest:expectOnlyCompleted( WFCatAlbums.createAlbum( wfCatalog, albumName2 )
		:tap( function( localAlbumId, globalAlbumId )
			albumLocalId2 = localAlbumId
			albumGlobalId2 = globalAlbumId
		end )
		:takeLast( 0 )
	)

	doTwice( rxTest, function() 
		rxTest:expectSingleValue(
		WFCatIdUtils.albumUuidForMaybeLocalId( wfCatalog, albumGlobalId2 ),
		albumGlobalId2 ) 
	end )

	doTwice( rxTest, function()
		rxTest:expectSingleValue(
		WFCatIdUtils.albumUuidForMaybeLocalId( wfCatalog, albumLocalId2 ),
		albumGlobalId2 )
	end )

	local globalIds2 = {}
	local idsAndTypes2 = {}
	
	id = addAssetFn( '2001' )
	table.insert( globalIds2, id )
	table.insert( idsAndTypes2, {
		id,
		'image'
	} )
	id = addAssetFn( '2002' )
	table.insert( globalIds2, id )
	table.insert( idsAndTypes2, {
		id,
		'image'
	} )
	id = addAssetFn( '2003' )
	table.insert( globalIds2, id )
	table.insert( idsAndTypes2, {
		id,
		'image'
	} )
	
	local localIds2 = {}
	
	for _, globalId in ipairs( globalIds2 ) do
		rxTest:expectSingleValue(
			docStore:docValue( globalId )
				:map( function( docValue )
					table.insert( localIds2, docValue._localId )
					return docValue._localId
				end ), ':ignore:' )
	end
	
	doTwice( rxTest, function()
		rxTest:expectSingleValue(
		WFCatIdUtils.getOzAssetIdsForLocalIds( wfCatalog, globalIds2 ),
		globalIds2
	) end )
	
	doTwice( rxTest, function()
		rxTest:expectSingleValue(
		WFCatIdUtils.getOzAssetIdsForLocalIds( wfCatalog, localIds2 ),
		globalIds2
	) end )
	
	doTwice( rxTest, function()
		rxTest:expectSingleValue(
		WFCatIdUtils.getOzAssetIdsAndTypeForIds( wfCatalog, globalIds2 ),
		idsAndTypes2
	) end )
	
	doTwice( rxTest, function()
		rxTest:expectSingleValue(
		WFCatIdUtils.getOzAssetIdsAndTypeForIds( wfCatalog, localIds2 ),
		idsAndTypes2
	) end )
	
	doTwice( rxTest, function()
		rxTest:expectSingleValue(
		WFCatIdUtils.getOzAlbumIdsForLocalId( wfCatalog, { albumLocalId, albumLocalId2 } ),
		{ albumGlobalId, albumGlobalId2 }
	) end )
	
	doTwice( rxTest, function()
		rxTest:expectSingleValue(
		WFCatIdUtils.getOzAlbumIdsForLocalId( wfCatalog, { albumGlobalId, albumGlobalId2 } ),
		{ albumGlobalId, albumGlobalId2 }
	) end )
	
	print( localIds2 )
	
end

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

return WFRxTestCase.wrapSuite( WFCatIdUtilsTest )
