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

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.

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

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

	--WF.core.utils
local KSIdGenerator = require 'KSIdGenerator'
local KSPathUtils = require 'KSPathUtils'
local KSTableUtils = require 'KSTableUtils'
local WFFileUtils = require 'WFFileUtils'
local WFPlatformInfo = require 'WFPlatformInfo'
local WFTableMerge = require 'WFTableMerge'

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

	-- WF.oz_client.utils
local OzFilePaths

if WFPlatformInfo.isiOS then
	
	OzFilePaths = require 'OzFilePaths'

end

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

local derefUniversal = assert( WFCatAssetUtils.derefUniversal )
local universalPath = assert( WFCatAssetUtils.universalPath )

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

local WFCatAssetUtilsTest = {}

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


local _wfCatalog

local function createFakeWFCatalogForTestSuite( rxTest )

	if not _wfCatalog then

		local optArgs = {
			keepOpenAfterTestFinishes = true
		}
		
		if WFPlatformInfo.isMobile() then
			optArgs.indexDefinitions = 'LrmCatalogIndexDefinitions.lua'
		end

		_wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest , nil, nil, optArgs )
	
	end
	
	return _wfCatalog
	
end

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

local function buildDocValue( rxTest, overrides )

	local assetId = KSIdGenerator.uuidInLowercaseHex()
	
	local wfCatalog = createFakeWFCatalogForTestSuite( rxTest )
	
	local docValue, expectedData = WFCatAssetTestUtils.createAssetDocument( rxTest, wfCatalog, assetId, overrides )

	return docValue, expectedData, wfCatalog

end

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

function WFCatAssetUtilsTest.rxTestGetFullOriginalPathFromDocValue( rxTest )

	local docValue, expectedData = buildDocValue( rxTest )

	local result = WFCatAssetUtils.getFullOriginalPathFromDocValue( docValue )
	
	local expected = expectedData.originalFilePath
	
	rxTest:expectEqual( expected, result )

	local overrides = {
		docSubtype = "preset"
	}

	docValue, expectedData = buildDocValue( rxTest, overrides )

	result = WFCatAssetUtils.getFullOriginalPathFromDocValue( docValue )

	expected = expectedData.presetFilePath

	rxTest:expectEqual( expected, result )

	if WFPlatformInfo.isDesktop then
	
		-- Desktop: If the original is on an offline volume, this should return nil.
	
		overrides = {
			localOriginalIsOnDisconnectedVolume = true
		}
		
		docValue, expectedData = buildDocValue( rxTest, overrides )
		
		result = WFCatAssetUtils.getFullOriginalPathFromDocValue( docValue )
	
		expected = nil
	
		rxTest:expectEqual( expected, result ) 
	
	end

end

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

function WFCatAssetUtilsTest.rxTestGetOriginalRelativePathFromDocValue( rxTest )

	local docValue, expectedData = buildDocValue( rxTest )

	local result = WFCatAssetUtils.getOriginalRelativePathFromDocValue( docValue )
	
	local expected
	
	if WFPlatformInfo.isDesktop then
	
		local relativePath = WFFileUtils.removeVolumeFromPath( expectedData.originalFilePath )
		expected = relativePath
	
	else
	
		expected = expectedData.originalFilePath
	
	end
	
	rxTest:expectEqual( expected, result ) 

end

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

function WFCatAssetUtilsTest.rxTestGetOriginalVolumeGuidFromDocValue( rxTest )

	local docValue, expectedData = buildDocValue( rxTest )

	local result = WFCatAssetUtils.getOriginalVolumeGuidFromDocValue( docValue )
	
	local expected
	
	if WFPlatformInfo.isDesktop then
	
		local volumeGuid = WFFileUtils.getVolumeGuidForPath( expectedData.originalFilePath )
		expected = volumeGuid
		
	else
	
		expected = nil
	
	end
	
	rxTest:expectEqual( expected, result ) 

end

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

function WFCatAssetUtilsTest.rxTestGetOriginalSizeFromDocValue( rxTest )

	local docValue, expectedData = buildDocValue( rxTest )

	local result = WFCatAssetUtils.getOriginalSizeFromDocValue( docValue )

	local expected = expectedData.originalFileSize

	rxTest:expectEqual( expected, result )

	local overrides = {
		docSubtype = "preset"
	}

	docValue, expectedData = buildDocValue( rxTest, overrides )

	result = WFCatAssetUtils.getOriginalSizeFromDocValue( docValue )

	expected = expectedData.presetFileSize

	rxTest:expectEqual( expected, result )

end

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

function WFCatAssetUtilsTest.rxTestGetOriginalContentIdFromDocValue( rxTest )

	local docValue, expectedData = buildDocValue( rxTest )

	local result = WFCatAssetUtils.getOriginalContentIdFromDocValue( docValue )

	local expected = expectedData.originalContentId

	rxTest:expectEqual( expected, result )

	local overrides = {
		docSubtype = "preset"
	}

	docValue, expectedData = buildDocValue( rxTest, overrides )

	result = WFCatAssetUtils.getOriginalContentIdFromDocValue( docValue )

	expected = expectedData.presetContentId

	rxTest:expectEqual( expected, result )

end

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

function WFCatAssetUtilsTest.rxTestGetFullProxyPathFromDocValue( rxTest )

	local docValue, expectedData = buildDocValue( rxTest )

	local result = WFCatAssetUtils.getFullProxyPathFromDocValue( docValue )
	
	local expected = expectedData.proxyFilePath
	
	rxTest:expectEqual( expected, result ) 

	if WFPlatformInfo.isDesktop then
	
		-- Desktop: If the original is on an offline volume, this should return nil.
	
		local overrides = {
			localProxyIsOnDisconnectedVolume = true
		}
		
		docValue, expectedData = buildDocValue( rxTest, overrides )
		
		result = WFCatAssetUtils.getFullProxyPathFromDocValue( docValue )
	
		expected = nil
	
		rxTest:expectEqual( expected, result ) 
	
	end

end

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

function WFCatAssetUtilsTest.rxTestGetProxyRelativePathFromDocValue( rxTest )

	local docValue, expectedData = buildDocValue( rxTest )

	local result = WFCatAssetUtils.getProxyRelativePathFromDocValue( docValue )
	
	local expected
	
	if WFPlatformInfo.isDesktop then
	
		local relativePath = WFFileUtils.removeVolumeFromPath( expectedData.proxyFilePath )
		expected = relativePath
	
	else
	
		expected = expectedData.proxyFilePath
	
	end
	
	rxTest:expectEqual( expected, result ) 

end

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

function WFCatAssetUtilsTest.rxTestGetProxyVolumeGuidFromDocValue( rxTest )

	local docValue, expectedData = buildDocValue( rxTest )

	local result = WFCatAssetUtils.getProxyVolumeGuidFromDocValue( docValue )
	
	local expected
	
	if WFPlatformInfo.isDesktop then
	
		local volumeGuid = WFFileUtils.getVolumeGuidForPath( expectedData.proxyFilePath )
		expected = volumeGuid
		
	else
	
		expected = nil
	
	end
	
	rxTest:expectEqual( expected, result ) 

end

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

function WFCatAssetUtilsTest.rxTestGetProxySizeFromDocValue( rxTest )

	local docValue, expectedData = buildDocValue( rxTest )

	local result = WFCatAssetUtils.getProxySizeFromDocValue( docValue )
	
	local expected = expectedData.proxyFileSize
	
	rxTest:expectEqual( expected, result ) 
	
end

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

function WFCatAssetUtilsTest.rxTestGetProxyContentIdFromDocValue( rxTest )

	local docValue, expectedData = buildDocValue( rxTest )

	local result = WFCatAssetUtils.getProxyContentIdFromDocValue( docValue )
	
	local expected = expectedData.proxyContentId
	
	if WFPlatformInfo.isAndroid then
	
		expected = nil
		
	end
	
	rxTest:expectEqual( expected, result ) 

end

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

function WFCatAssetUtilsTest.rxTestGetLocalExternalDevelopSettingsPath( rxTest )

	local docValue, expectedData = buildDocValue( rxTest )

	local result = WFCatAssetUtils.getLocalExternalDevelopSettingsPath( docValue )
	
	local expected = expectedData.xmpDevelopSettingsRelPath
	
	rxTest:expectEqual( expected, result ) 

end

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

function WFCatAssetUtilsTest.rxTestGetLocalExternalDevelopSettingsSize( rxTest )

	local docValue, expectedData = buildDocValue( rxTest )

	local result = WFCatAssetUtils.getLocalExternalDevelopSettingsSize( docValue )
	
	local expected = expectedData.xmpDevelopSettingsFileSize
	
	if WFPlatformInfo.isAndroid then
		expected = nil
	end
	
	rxTest:expectEqual( expected, result ) 

end

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

function WFCatAssetUtilsTest.rxTestGetLocalExternalDevelopSettingsContentId( rxTest )

	local docValue, expectedData = buildDocValue( rxTest )

	local result = WFCatAssetUtils.getLocalExternalDevelopSettingsContentId( docValue )

	local expected = expectedData.developSettingsSha256
	
	rxTest:expectEqual( expected, result ) 

end

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

function WFCatAssetUtilsTest.rxTestAbsolutePathToDevelopSettings( rxTest )

	local _, _, wfCatalog = buildDocValue( rxTest )

	local relativePath = 'foo/zar/bar'
	
	local absolutePath = WFCatAssetUtils.absolutePathToDevelopSettings( wfCatalog:ozCatalogId(), relativePath )
	
	if WFPlatformInfo.isDesktop then
	
		rxTest:expectEqual( 'foo/zar/bar', absolutePath ) 

	elseif WFPlatformInfo.isAndroid then
	
		local documentsDir = KSPathUtils.getStandardFilePath( 'carouselDocuments' )
		
		local externalXmpAbsolutePath = KSPathUtils.child( documentsDir, relativePath )
	
		rxTest:expectEqual( externalXmpAbsolutePath, absolutePath ) 
	
	elseif WFPlatformInfo.isiOS then
	
		local externalXmpAbsolutePath = OzFilePaths.getAbsolutePathInCatalog( wfCatalog:ozCatalogId(), relativePath )
	
		rxTest:expectEqual( externalXmpAbsolutePath, absolutePath ) 
	
	end

end

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

function WFCatAssetUtilsTest.rxTestHasLocalOriginal( rxTest )

	-- has local original
	
	local docValue = buildDocValue( rxTest )

	rxTest:expectEqual( true, WFCatAssetUtils.hasLocalOriginal( docValue ) ) 

	-- has no local original
	
	local overrides = {
		noLocalOriginal = true
	}
		
	docValue = buildDocValue( rxTest, overrides )
	
	rxTest:expectEqual( false, WFCatAssetUtils.hasLocalOriginal( docValue ) ) 

end

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

function WFCatAssetUtilsTest.rxTestHasLocalProxy( rxTest )

	-- has local proxy
	
	local docValue = buildDocValue( rxTest )

	local result = WFCatAssetUtils.hasLocalProxy( docValue )
	rxTest:expectEqual( true, result ) 

	-- has no local proxy
	
	local overrides = {
		noLocalProxy = true
	}
		
	docValue = buildDocValue( rxTest, overrides )
	
	rxTest:expectEqual( false, WFCatAssetUtils.hasLocalProxy( docValue ) ) 

end

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

function WFCatAssetUtilsTest.rxTestHasLocalOriginalOrProxy( rxTest )

	local docValue = buildDocValue( rxTest )

	local result = WFCatAssetUtils.hasLocalOriginalOrProxy( docValue )
	rxTest:expectEqual( true, result ) 

	-- has no local proxy
	
	local overrides = {
		noLocalProxy = true
	}
		
	docValue = buildDocValue( rxTest, overrides )
	
	rxTest:expectEqual( true, WFCatAssetUtils.hasLocalOriginalOrProxy( docValue ) ) 
	
	-- has no local original
	
	overrides = {
		noLocalOriginal = true,
	}
		
	docValue = buildDocValue( rxTest, overrides )
	
	rxTest:expectEqual( true, WFCatAssetUtils.hasLocalOriginalOrProxy( docValue ) ) 

	-- has no local original or local proxy
	
	overrides = {
		noLocalOriginal = true,
		noLocalProxy = true
	}
		
	docValue = buildDocValue( rxTest, overrides )
	
	rxTest:expectEqual( false, WFCatAssetUtils.hasLocalOriginalOrProxy( docValue ) ) 

end

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

function WFCatAssetUtilsTest.rxTestDoesAssetDocumentHaveLocalBinary( rxTest )

	-- asset doc w/ local original and proxy
	
	local docValue = buildDocValue( rxTest )

	local result = WFCatAssetUtils.doesAssetDocumentHaveLocalBinary( docValue )
	rxTest:expectEqual( true, result ) 

	-- asset doc w/ no local original and no local proxy
	
	local overrides = {
		noLocalOriginal = true,
		noLocalProxy = true
	}
		
	docValue = buildDocValue( rxTest, overrides )
	
	rxTest:expectEqual( false, WFCatAssetUtils.doesAssetDocumentHaveLocalBinary( docValue ) ) 

	-- style doc w/ local original
	
	overrides = {
		docSubtype = 'profile'
	}
		
	docValue = buildDocValue( rxTest, overrides )
	
	rxTest:expectEqual( true, WFCatAssetUtils.doesAssetDocumentHaveLocalBinary( docValue ) ) 

	-- style doc w/ no local original
	
	overrides = {
		docSubtype = 'profile',
		noLocalOriginal = true,
	}
		
	docValue = buildDocValue( rxTest, overrides )
	
	rxTest:expectEqual( false, WFCatAssetUtils.doesAssetDocumentHaveLocalBinary( docValue ) ) 

	-- dcp doc w/ local original
	
	overrides = {
		docSubtype = 'camera_profile'
	}
		
	docValue = buildDocValue( rxTest, overrides )
	
	rxTest:expectEqual( true, WFCatAssetUtils.doesAssetDocumentHaveLocalBinary( docValue ) ) 

	-- dcp doc w/ no local original
	
	overrides = {
		docSubtype = 'camera_profile',
		noLocalOriginal = true,
	}
		
	docValue = buildDocValue( rxTest, overrides )
	
	rxTest:expectEqual( false, WFCatAssetUtils.doesAssetDocumentHaveLocalBinary( docValue ) ) 

end

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

function WFCatAssetUtilsTest.rxTestIsValidOriginalOnOz( rxTest )

	-- asset doc w/ original on Oz

	local docValue = buildDocValue( rxTest )

	rxTest:expectEqual( true, WFCatAssetUtils.isValidOriginalOnOz( docValue ) )

	-- asset doc w/ no original on Oz

	local overrides = {
		noMasterLink = true,
	}

	docValue = buildDocValue( rxTest, overrides )

	rxTest:expectEqual( false, WFCatAssetUtils.isValidOriginalOnOz( docValue ) )

	-- asset doc w/ invalid original link on Oz

	overrides = {
		invalidMasterLink = true,
	}

	docValue = buildDocValue( rxTest, overrides )

	rxTest:expectEqual( false, WFCatAssetUtils.isValidOriginalOnOz( docValue ) )

	-- asset doc w/ corrupt original link on Oz

	overrides = {
		corruptMaster = true,
	}

	docValue = buildDocValue( rxTest, overrides )

	rxTest:expectEqual( false, WFCatAssetUtils.isValidOriginalOnOz( docValue ) )

end

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

function WFCatAssetUtilsTest.rxTestGetUniversal( rxTest )

	local docValue, expectedData = buildDocValue( rxTest )

	rxTest:expectEqual(
		WFCatAssetUtils.getProxyContentIdFromDocValue( docValue ),
		derefUniversal( docValue, 'files', 'proxy', '2560', 'contentId' ) )

	rxTest:expectEqual(
		WFCatAssetUtils.getProxyRelativePathFromDocValue( docValue ),
		derefUniversal( docValue, 'files', 'proxy', '2560', 'relativePath' ) )

	rxTest:expectEqual(
		WFCatAssetUtils.getProxySizeFromDocValue( docValue ),
		derefUniversal( docValue, 'files', 'proxy', '2560', 'fileSize' ) )

	rxTest:expectEqual(
		WFCatAssetUtils.getOriginalRelativePathFromDocValue( docValue ),
		derefUniversal( docValue, 'files', 'original', 'master', 'relativePath' ) )

	rxTest:expectEqual(
		WFCatAssetUtils.getOriginalSizeFromDocValue( docValue ),
		derefUniversal( docValue, 'files', 'original', 'master', 'fileSize' ) )

    if not WFPlatformInfo.isAndroid then
		-- Android never uses content Id for storing any type of rendition
        rxTest:expectEqual(
            WFCatAssetUtils.getOriginalContentIdFromDocValue( docValue ),
            derefUniversal( docValue, 'files', 'original', 'master', 'contentId' ) )
    end

	rxTest:expectEqual(
		WFCatAssetUtils.getLocalExternalDevelopSettingsPath( docValue ),
		derefUniversal( docValue, 'files', 'xmp_develop', 'relativePath' ) )

	rxTest:expectEqual(
		WFCatAssetUtils.getLocalExternalDevelopSettingsSize( docValue ),
		derefUniversal( docValue, 'files', 'xmp_develop', 'fileSize' ) )

	rxTest:expectEqual(
		WFCatAssetUtils.getLocalExternalDevelopSettingsContentId( docValue ),
		derefUniversal( docValue, 'files', 'xmp_develop', 'contentId' ) )

	if WFPlatformInfo.isiOS then
	
		rxTest:expectEqual(
			WFCatAssetUtils.getOriginalRelativePathFromDocValue( docValue ),
			derefUniversal( docValue, 'files', 'original', 'latestMaster', 'relativePath' ) )
	
		rxTest:expectEqual(
			expectedData.thumbnailFilePath,
			derefUniversal( docValue, 'files', 'renditions', 'thumbnail2x', 'relativePath' ) )

		rxTest:expectEqual(
			expectedData.previewFilePath,
			derefUniversal( docValue, 'files', 'renditions', '2048', 'relativePath' ) )
	
		rxTest:expectEqual(
			expectedData.thumbnailContentId,
			derefUniversal( docValue, 'files', 'renditions', 'thumbnail2x', 'contentId' ) )

		rxTest:expectEqual(
			expectedData.thumbnailFileSize,
			derefUniversal( docValue, 'files', 'renditions', 'thumbnail2x', 'fileSize' ) )

		rxTest:expectEqual(
			expectedData.previewContentId,
			derefUniversal( docValue, 'files', 'renditions', '2048', 'contentId' ) )

		rxTest:expectEqual(
			expectedData.previewFileSize,
			derefUniversal( docValue, 'files', 'renditions', '2048', 'fileSize' ) )

	end

end

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

local function testUniversalPath_iOS( rxTest )

	rxTest:expectEqual( unpack { '_localOnly', 'files', 'thumbnail' },
		universalPath( 'files', 'renditions', 'thumbnail' )
	)

	rxTest:expectEqual( unpack { '_localOnly', 'files', 'thumbnail2' },
		universalPath( 'files', 'renditions', 'thumbnail2x' )
	)

	rxTest:expectEqual( unpack { '_localOnly', 'files', 'preview' },
		universalPath( 'files', 'renditions', '2048' )
	)

	rxTest:expectEqual( unpack { '_localOnly', 'files', 'proxy' },
		universalPath( 'files', 'proxy', '2560' )
	)

	rxTest:expectEqual( unpack { '_localOnly', 'files', 'master' },
		universalPath( 'files', 'original', 'master' )
	)

	rxTest:expectEqual( unpack { '_localOnly', 'files', 'xmp_develop' },
		universalPath( 'files', 'xmp_develop' )
	)

	-- unknown paths

	rxTest:expectEqual( nil,
		universalPath( 'files', 'foo_develop' )
	)

	rxTest:expectEqual( nil,
		universalPath( 'desktop', 'foo' )
	)

end

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

local function testUniversalPath_Android( rxTest )

	rxTest:expectEqual( unpack { '_localOnly', 'thumbnail' },
		universalPath( 'files', 'renditions', 'thumbnail' )
	)

	rxTest:expectEqual( unpack { '_localOnly', 'thumbnail2' },
		universalPath( 'files', 'renditions', 'thumbnail2x' )
	)

	rxTest:expectEqual( unpack { '_localOnly', 'preview' },
		universalPath( 'files', 'renditions', '2048' )
	)

	rxTest:expectEqual( unpack { '_localOnly', 'proxy' },
		universalPath( 'files', 'proxy', '2560' )
	)

	rxTest:expectEqual( unpack { '_localOnly', 'original' },
		universalPath( 'files', 'original', 'master' )
	)

	rxTest:expectEqual( unpack { '_localOnly', 'files', 'xmp_develop' },
		universalPath( 'files', 'xmp_develop' )
	)

	-- unknown paths

	rxTest:expectEqual( nil,
		universalPath( 'files', 'foo_develop' )
	)

	rxTest:expectEqual( nil,
		universalPath( 'desktop', 'foo' )
	)

end

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

local function testUniversalPath_Desktop( rxTest )

	rxTest:expectEqual( unpack { '_localOnly', 'thumbnail' },
		universalPath( 'files', 'renditions', 'thumbnail' )
	)

	rxTest:expectEqual( unpack { '_localOnly', 'thumbnail2' },
		universalPath( 'files', 'renditions', 'thumbnail2x' )
	)

	rxTest:expectEqual( unpack { '_localOnly', 'preview' },
		universalPath( 'files', 'renditions', '2048' )
	)

	rxTest:expectEqual( unpack { '_localOnly', 'proxy' },
		universalPath( 'files', 'proxy', '2560' )
	)

	rxTest:expectEqual( unpack { '_localOnly', 'original' },
		universalPath( 'files', 'original', 'master' )
	)

	rxTest:expectEqual( unpack { '_localOnly', 'files', 'xmp_develop' },
		universalPath( 'files', 'xmp_develop' )
	)

	-- unknown paths

	rxTest:expectEqual( nil,
		universalPath( 'files', 'foo_develop' )
	)

	rxTest:expectEqual( nil,
		universalPath( 'desktop', 'foo' )
	)

end

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

function WFCatAssetUtilsTest.rxTestUniversalPath( rxTest )

	if WFPlatformInfo.isAndroid then

		testUniversalPath_Android( rxTest )

	elseif WFPlatformInfo.isiOS then
	
		testUniversalPath_iOS( rxTest )
	
	elseif WFPlatformInfo.isiOS then
	
		testUniversalPath_Desktop( rxTest )

	end

end

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

local function testRenditionInfo_Android( rxTest, fakeWFCatalog, docValue, expectedData )

	local renditions = WFCatAssetUtils.getLocalRenditionInfo( fakeWFCatalog, docValue )

	rxTest:expectEqual( 2, #renditions )

	for _, renditionInfo in ipairs( renditions ) do
	
		if renditionInfo.renditionName == 'preview' then
			
			rxTest:expectEqual( expectedData.previewMd5, renditionInfo.renditionInfo.md5 )
			
		elseif renditionInfo.renditionName == 'thumbnail2x' then
		
			rxTest:expectEqual( expectedData.thumbMd5, renditionInfo.renditionInfo.md5 )
			
		end
	
	end
	
end

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

local function testRenditionInfo_iOS( rxTest, fakeWFCatalog, docValue, expectedData )

	local renditions = WFCatAssetUtils.getLocalRenditionInfo( fakeWFCatalog, docValue )

	rxTest:expectEqual( 2, #renditions )

	for _, renditionInfo in ipairs( renditions ) do
	
		if renditionInfo.renditionName == 'preview' then
			
			rxTest:expectEqual( expectedData.previewFilePath, renditionInfo.renditionRelativePath )
			
			rxTest:expectEqual( expectedData.previewContentId, renditionInfo.renditionContentId )
			
			rxTest:expectEqual( expectedData.previewFileSize, renditionInfo.renditionSize )
			
		elseif renditionInfo.renditionName == 'thumbnail2x' then
		
			rxTest:expectEqual( expectedData.thumbnailFilePath, renditionInfo.renditionRelativePath )
			
			rxTest:expectEqual( expectedData.thumbnailContentId, renditionInfo.renditionContentId )
			
			rxTest:expectEqual( expectedData.thumbnailFileSize, renditionInfo.renditionSize )
		
		end
	
	end

end

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

local function testRenditionInfo_Desktop( rxTest, fakeWFCatalog, docValue, expectedData )

	local renditions = WFCatAssetUtils.getLocalRenditionInfo( fakeWFCatalog, docValue )

	rxTest:expectEqual( 2, #renditions )

	for _, renditionInfo in ipairs( renditions ) do
	
		if renditionInfo.longEdge == 2048 then
			
			rxTest:expectEqual( expectedData.previewFilePath, renditionInfo.renditionPath )
			
		elseif renditionInfo.longEdge == 320 then
		
			rxTest:expectEqual( expectedData.thumbnailFilePath, renditionInfo.renditionPath )
		
		end
	
	end

end

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

function WFCatAssetUtilsTest.rxTestRenditionInfo( rxTest )

	local docValue, expectedData, wfCatalog = buildDocValue( rxTest )

	if WFPlatformInfo.isAndroid then

		testRenditionInfo_Android( rxTest, wfCatalog, docValue, expectedData )

	elseif WFPlatformInfo.isiOS then
	
		testRenditionInfo_iOS( rxTest, wfCatalog, docValue, expectedData )
	
	elseif WFPlatformInfo.isDesktop then
	
		testRenditionInfo_Desktop( rxTest, wfCatalog, docValue, expectedData )

	end
end

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

local function createDocValueWithServerProvidedLinks( rxTest, isLocalCopy, serverProvidedMetadataOverlay )

	local docValue, _ = buildDocValue( rxTest )

	-- Remove all _serverProvidedMetadata (buildDocValue adds a /rels/master)
	KSTableUtils.setNested( docValue, nil, '_serverProvidedMetadata' )

	-- Set docValue.develop.sha256 to match sha in the _loca.Only file section
	-- as mobile depends on it.
	local xmpLocalFileInfo = KSTableUtils.derefOpt( docValue, '_localOnly', 'files', 'xmp_develop' )
	local cameraRawSha256 = xmpLocalFileInfo and (
			xmpLocalFileInfo.sha256 -- desktop, android
			or xmpLocalFileInfo.contentId -- iOS
		)
	KSTableUtils.setNested( docValue, cameraRawSha256, 'develop', 'xmpCameraRaw', 'sha256' )

	if isLocalCopy then

		docValue = WFTableMerge.applyOverlay( docValue, {
			_localOnly = {
				-- remove all the local file paths as copy doesn't rely on anything being local
				files = { _delete = true }, -- Desktop and iOS
				proxy = { _delete = true }, -- android
				original = { _delete = true }, -- android

				-- add the flag that we check indicating the asset was copied locally
				sourceAssetId = KSIdGenerator.uuidInLowercaseHex(),
			},
		} )

	end
	
	if serverProvidedMetadataOverlay then
		docValue = WFTableMerge.applyOverlay( docValue, serverProvidedMetadataOverlay )
	end

	return docValue

end

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

function WFCatAssetUtilsTest.rxTestBinaryUploadPending( rxTest )

	local function addLink( overlay, linkType )
		KSTableUtils.setNested( overlay, { href = '/url/goes/here' }, '_serverProvidedMetadata', 'links', linkType )
		return overlay
	end

	-- original, xmpDevelop pending upload
	do
		local doc = createDocValueWithServerProvidedLinks( rxTest, false )
		rxTest:expectEqual( true, WFCatAssetUtils.binaryUploadPending( doc ) )

		if WFPlatformInfo.isDesktop then
			local copy = createDocValueWithServerProvidedLinks( rxTest, true )
			rxTest:expectEqual( true, WFCatAssetUtils.binaryUploadPending( copy ) )
		end
	end

	-- original pending upload
	do
		local spm = addLink( {}, '/rels/xmp/develop' )

		local doc = createDocValueWithServerProvidedLinks( rxTest, false, spm )
		rxTest:expectEqual( true, WFCatAssetUtils.binaryUploadPending( doc ) )

		if WFPlatformInfo.isDesktop then
			local copy = createDocValueWithServerProvidedLinks( rxTest, true, spm )
			rxTest:expectEqual( true, WFCatAssetUtils.binaryUploadPending( copy ) )
		end
	end

	-- external develop settings pending upload
	do
		local spm = addLink( {}, '/rels/master' )

		local doc = createDocValueWithServerProvidedLinks( rxTest, false, spm )
		rxTest:expectEqual( true, WFCatAssetUtils.binaryUploadPending( doc ) )

		if WFPlatformInfo.isDesktop then
			local copy = createDocValueWithServerProvidedLinks( rxTest, true, spm )
			rxTest:expectEqual( true, WFCatAssetUtils.binaryUploadPending( copy ) )
		end
	end
	
	-- external sidecar pending upload
	do
		local spm = addLink( {}, '/rels/master' )
		addLink( spm, '/rels/xmp/develop' )

		local doc = createDocValueWithServerProvidedLinks( rxTest, false, spm )
		-- desktop currently supports external sidecar upload, mobile doesn't yet
		rxTest:expectEqual( WFPlatformInfo.isDesktop and true or false, WFCatAssetUtils.binaryUploadPending( doc ) )

		if WFPlatformInfo.isDesktop then
			local copy = createDocValueWithServerProvidedLinks( rxTest, true, spm )
			rxTest:expectEqual( true, WFCatAssetUtils.binaryUploadPending( copy ) )
		end
	end

	-- everything uploaded
	do
		local spm = addLink( {}, '/rels/master' )
		addLink( spm, '/rels/xmp/develop' )
		addLink( spm, '/rels/xmp' )

		local doc = createDocValueWithServerProvidedLinks( rxTest, false, spm )
		rxTest:expectEqual( false, WFCatAssetUtils.binaryUploadPending( doc ) )

		if WFPlatformInfo.isDesktop then
			local copy = createDocValueWithServerProvidedLinks( rxTest, true, spm )
			rxTest:expectEqual( false, WFCatAssetUtils.binaryUploadPending( copy ) )
		end
	end


	if WFPlatformInfo.isDesktop then

		-- proxy only asset pending upload
		do
			local spm = addLink( {}, '/rels/xmp/develop' )
			KSTableUtils.setNested( spm, { _delete = true }, '_localOnly', 'files', 'original' ) -- desktop
			KSTableUtils.setNested( spm, { _delete = true }, '_localOnly', 'files', 'master' ) -- iOS
			KSTableUtils.setNested( spm, { _delete = true }, '_localOnly', 'original' ) -- android

			local doc = createDocValueWithServerProvidedLinks( rxTest, false, spm )
			rxTest:expectEqual( true, WFCatAssetUtils.binaryUploadPending( doc ) )

			local copy = createDocValueWithServerProvidedLinks( rxTest, true, spm )
			rxTest:expectEqual( true, WFCatAssetUtils.binaryUploadPending( copy ) )
		end

		-- xmp sidecar pending upload
		do
			local spm = addLink( {}, '/rels/master' )
			addLink( spm, '/rels/xmp/develop' )

			local doc = createDocValueWithServerProvidedLinks( rxTest, false, spm )
			rxTest:expectEqual( true, WFCatAssetUtils.binaryUploadPending( doc ) )

			local copy = createDocValueWithServerProvidedLinks( rxTest, true, spm )
			rxTest:expectEqual( true, WFCatAssetUtils.binaryUploadPending( copy ) )
		end

	end

end


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

function WFCatAssetUtilsTest.rxTestZZ_CloseSuite( rxTest )

	-- Funky naming means this runs last in the suite.

	rxTest:finally( function()
	
		if _wfCatalog then
		
			_wfCatalog:close()
			_wfCatalog = nil
	
		end
	
	end )
	
end

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

return WFRxTestCase.wrapSuite( WFCatAssetUtilsTest )
