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

NmbArchivalTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2024 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 WFPlatformInfo = require "WFPlatformInfo"
local WFRxTestCase = require 'WFRxTestCase'

local NmbArchivalTest = {}

if WFPlatformInfo.isDesktop then

local KSPathUtils = require 'KSPathUtils'
local WFCatAlbums = require 'WFCatAlbums'
local WFCatImport = require 'WFCatImport'
local WFCatMockCatalog = require 'WFCatMockCatalog'

local NmbArchival = require 'NmbArchival'

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

local createFakeWFCatalog = WFCatMockCatalog.createFakeWFCatalog

local testDataPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'testdata' ),
										"import-samples" )
local testDataFolder = KSPathUtils.child( testDataPath, "folder-1" )
local testFilePath1 = KSPathUtils.child( testDataFolder, "IMG_7763.jpg" )
local testFilePath2 = KSPathUtils.child( testDataFolder, "IMG_9876.CR2" )

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

local function addHierarchy( rxTest, fakeWFCatalog )

	local folderLocalId, folderDocId, albumLocalId, albumDocId, assetLocalId1, assetDocId1, assetLocalId2, assetDocId2
	
	rxTest:expectOnlyCompleted(

		WFCatAlbums.createFolder( fakeWFCatalog, "Folder" )
			:tap( function( localAlbumId, docAlbumId  )
				folderLocalId = localAlbumId
				folderDocId = docAlbumId
			end )
			:takeLast( 0 )
	)

	rxTest:expectOnlyCompleted(

		WFCatAlbums.createAlbum( fakeWFCatalog, "Album", folderLocalId )
			:tap( function( localAlbumId, docAlbumId  )
				albumLocalId = localAlbumId
				albumDocId = docAlbumId
			end )
			:takeLast( 0 )
	)
	
	rxTest:expectOnlyCompleted(

		WFCatAlbums.createSmartAlbum( fakeWFCatalog, "SmartAlbum", folderLocalId, {} )
			:takeLast( 0 )
	)
	
	rxTest:expectOnlyCompleted(
		WFCatImport.addImageArrayToCatalog( fakeWFCatalog, nil, { 
			{
				filePath = testFilePath1,
				width = 2048,
				height = 1024,
				fileName = "Image1.jpg",
				fileSize = 4096,
				fileCreationDate = "2016-03-07T05:20:34Z",
			},
			{
				filePath = testFilePath2,
				width = 2048,
				height = 1024,
				fileName = "Image2.jpg",
				fileSize = 4096,
				fileCreationDate = "2017-03-07T05:20:34Z",
			},
		}, nil, {
			albumId = albumLocalId,
		} )
		:toArray()
		:tap( function( results )
			table.sort( results, function( a, b ) return a.localId < b.localId end )
			assetLocalId1 = results[ 1 ].localId
			assetDocId1 = results[ 1 ].assetId
			assetLocalId2 = results[ 2 ].localId
			assetDocId2 = results[ 2 ].assetId
		end )
		:ignoreElements()
	)
	
	return folderLocalId, folderDocId, albumLocalId, albumDocId, assetLocalId1, assetDocId1, assetLocalId2, assetDocId2

end

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

function NmbArchivalTest.rxTestHierarchy( rxTest )
	
	rxTest:setTimeLimit( 100 )
	
	local fakeWFCatalog = createFakeWFCatalog( rxTest )
	
	local folderLocalId, folderDocId, albumLocalId, albumDocId, assetLocalId1, assetDocId1, assetLocalId2, assetDocId2 = addHierarchy( rxTest, fakeWFCatalog )
	
	rxTest:expectSingleValue(
		NmbArchival.getAssetHierarchy( fakeWFCatalog, { assetLocalId1, assetLocalId2 } ),
		{
			{
				type = "asset",
				captureDate = "2016-03-07T05:20:34Z",
				fileSize = 4096,
				id = assetLocalId1,
				docId = assetDocId1,
				name = "Image1.jpg",
				subtype = "image",
			},
			{
				type = "asset",
				captureDate = "2017-03-07T05:20:34Z",
				fileSize = 4096,
				id = assetLocalId2,
				docId = assetDocId2,
				name = "Image2.jpg",
				subtype = "image",
			},
		}
	)

	rxTest:expectSingleValue(
		NmbArchival.getAssetHierarchy( fakeWFCatalog, { folderLocalId } )
			:map( function( hierarchy )
			
				local function sortRecursive( t )
				
					table.sort( t, function( a, b ) return a.id < b.id end )
					
					for k, v in pairs( t ) do
						if type( v ) == 'table' then
							t[ k ] = sortRecursive( v )
						end
					end
					
					return t
				end
				
				return sortRecursive( hierarchy )
			
			end ),
		{
			{
				type = "folder",
				id = folderLocalId,
				docId = folderDocId,
				name = "Folder",
				
				{
					type = "album",
					id = albumLocalId,
					docId = albumDocId,
					name = "Album",
					
					{
						type = "asset",
						captureDate = "2016-03-07T05:20:34Z",
						fileSize = 4096,
						id = assetLocalId1,
						docId = assetDocId1,
						name = "Image1.jpg",
						subtype = "image",
					},
					{
						type = "asset",
						captureDate = "2017-03-07T05:20:34Z",
						fileSize = 4096,
						id = assetLocalId2,
						docId = assetDocId2	,
						name = "Image2.jpg",
						subtype = "image",
					},
				},
			},
		}
	)
	
end

end

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

return WFRxTestCase.wrapSuite( 'NmbArchivalTest', NmbArchivalTest )
