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

LrClassicImportTest.lua

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

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

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

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

	-- WF.oz_client
local WFCatAssetTestUtils = require 'WFCatAssetTestUtils'
local WFCatMockCatalog = require 'WFCatMockCatalog'

	-- wf.core
local KSIdGenerator = require 'KSIdGenerator'
local KSPathUtils = require 'KSPathUtils'
local WFCrypto = require 'WFCrypto'
local WFFileUtils = require 'WFFileUtils'
local KSTableUtils = require 'KSTableUtils'
local WFPlatformInfo = require 'WFPlatformInfo'

if WFPlatformInfo.isMobile() then
	return {}
end

local LrClassicImport = require 'LrClassicImport'
local WFCatAssetAux = require 'WFCatAssetAux'

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

local derefOpt = assert( KSTableUtils.derefOpt )

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

local LrClassicImportTest = {}

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

local testDataPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'testdata' ),
										"import-samples" )

local testDataFolder1 = KSPathUtils.child( testDataPath, "folder-1" )

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

local function getCatalogDir( wfCatalog )

	local catalogId = wfCatalog:ozCatalogSpec():catalogId()

	local baseDir = WFCatAssetAux.getBasePath( wfCatalog )

	return baseDir, catalogId

end

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

function LrClassicImportTest.rxTestAddImagesWithC2paToCatalog( rxTest )

	rxTest:setTimeLimit( 10 )
	
	local optArgs = {
		hasC2pa = true,
	}
	
	local fakeWFCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )
	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local assetDocValue, assetInfo = WFCatAssetTestUtils.createAssetDocument( rxTest, fakeWFCatalog, assetId, optArgs )
	rxTest:waitFor( fakeWFCatalog:docStore():setDocValue( assetDocValue ) )
	
	local catalogDir = getCatalogDir( fakeWFCatalog )
	local auxDigest1 = KSIdGenerator.uuidInLowercaseHex()
	local absoluteFilePath, relativePath = WFCatAssetAux.calculatePathUsingDigest( catalogDir, auxDigest1 )

	local parentDir = KSPathUtils.parent( absoluteFilePath )
	WFFileUtils.mkdir( parentDir )
	WFFileUtils.copy( assetInfo.c2paFilePath, absoluteFilePath )
	
	rxTest:finally( function()
		WFFileUtils.delete( absoluteFilePath )
	end )
	
	local importInfo = {
		c2paIngredients = {
			digest = auxDigest1,
			state = "Signed_Valid",
			version = 1,
		}
	}
	
	local actDocValue = LrClassicImport.DEBUG_maybeAddc2paIngredients( fakeWFCatalog, importInfo, assetDocValue )
	
	rxTest:expectSingleValue( actDocValue
					:map( function( assetDocValue )
						local auxId = WFCatAssetAux.getAuxIdForDigest( assetDocValue, auxDigest1 )
						local actC2paIngredients = { 
							state = derefOpt( assetDocValue, 'importSource', 'c2paIngredients', 'state'),
							version = derefOpt( assetDocValue, 'importSource', 'c2paIngredients', 'version'),
						}
						
						local actAux = derefOpt( assetDocValue, 'aux' , auxId )
						return { actC2paIngredients, actAux }
					end ),
					{
						{
							state = "Signed_Valid",
							version = 1,
							
						},
						{
							digest = auxDigest1,
							fileSize = assetInfo.c2paFileSize,
							sha256 = assetInfo.c2paSha256,
							type = 'c2pa',
						}
					}
					)

end

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

function LrClassicImportTest.rxTestSetOriginalAndProxyInfo( rxTest )

	rxTest:setTimeLimit( 10 )

	local fakeWFCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )
	
	local assetId = KSIdGenerator.uuidInLowercaseHex()
	local assetDocValue = WFCatAssetTestUtils.createAssetDocument( rxTest, fakeWFCatalog, assetId )
	rxTest:waitFor( fakeWFCatalog:docStore():setDocValue( assetDocValue ) )
	
	-- get new original and proxy binary info
	
	local testDataPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'testdata' ), "import-samples" )
	local testDataFolder1 = KSPathUtils.child( testDataPath, "folder-1" )
	local testAssetsFolder = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'testdata' ), "assets" )
	
	local newOriginalFilePath = KSPathUtils.child( testDataFolder1, 'IMG_7763.JPG' )
	local newOriginalSha256 = WFCrypto.digestFile( newOriginalFilePath, 'SHA256' )
	local newOriginalFileSize = WFFileUtils.fileAttributes( newOriginalFilePath ).fileSize
	
	local newProxyFilePath = KSPathUtils.child( testAssetsFolder, "IMG_7763.DNG" )
	local newProxySha256 = WFCrypto.digestFile( newProxyFilePath, 'SHA256' )
	local newProxyFileSize = WFFileUtils.fileAttributes( newProxyFilePath ).fileSize
	
	rxTest:waitFor( LrClassicImport.setOriginalAndProxyInfo( fakeWFCatalog, assetId, 
									newOriginalFilePath, newOriginalFileSize, newProxyFilePath, newProxyFileSize ) )
									
	rxTest:expectSingleValue( fakeWFCatalog:docStore():docValue( assetId )
			:map( function( docValue )
			
				return derefOpt( docValue, 'importSource', 'sha256' ) == newOriginalSha256
					and derefOpt( docValue, 'importSource', 'proxySHA256' ) == newProxySha256
					and derefOpt( docValue, 'importSource', 'fileSize' ) == newOriginalFileSize
					and derefOpt( docValue, '_localOnly', 'files', 'original', 'master', 'sha256' ) == newOriginalSha256
					and derefOpt( docValue, '_localOnly', 'files', 'original', 'master', 'size' ) == newOriginalFileSize
					and derefOpt( docValue, '_localOnly', 'files', 'proxy', '2560', 'sha256' ) == newProxySha256
					and derefOpt( docValue, '_localOnly', 'files', 'proxy', '2560', 'size' ) == newProxyFileSize
					
			end )
		, true )
	

end

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

return WFRxTestCase.wrapSuite( 'LrClassicImportTest', LrClassicImportTest )
