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

ADOBE CONFIDENTIAL
------------------
Copyright 2021 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 KSTableUtils = require 'KSTableUtils'
local WFPlatformInfo = require 'WFPlatformInfo'

	-- WF.oz_client.managed_catalog
local WFCatMockCatalog = require 'WFCatMockCatalog'
local WFCatVersionTestUtils = require 'WFCatVersionTestUtils'

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

local KSTableUtils_derefOpt = assert( KSTableUtils.derefOpt )
local KSTableUtils_setNested = assert( KSTableUtils.setNested )

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

local WFCatVersionTestUtilsTest = {}

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

local function createFakeWFCatalog( rxTest )

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

	return WFCatMockCatalog.createFakeWFCatalog( rxTest , nil, nil, optArgs )

end

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

function WFCatVersionTestUtilsTest.rxTestVerifyGeneratedDoc( rxTest )

	local wfCatalog = createFakeWFCatalog( rxTest )

	local docId = KSIdGenerator.uuidInLowercaseHex()

	local optOverrides = {
		hasMasks = true
	}

	local docValue, expectedData = WFCatVersionTestUtils.createVersionDocument( rxTest, wfCatalog, docId, optOverrides )

	-- Extract some fields to validate:
	
	local id = docValue._id
	docValue._id = "<guid>"
	rxTest:expectEqual( true, not not id )
	
	local assetId = KSTableUtils_derefOpt( docValue, 'asset', 'id' )
	KSTableUtils_setNested( docValue, "<guid>", 'asset', 'id' )
	rxTest:expectEqual( true, not not assetId )
	
	local name = docValue.name
	docValue.name = "<string>"
	rxTest:expectEqual( true, not not name )

	docValue._newRevId = '<guid>'
	KSTableUtils_setNested( docValue, { '<guid>' }, '_serverProvidedMetadata', 'revision_ids' )
	
	local xmpDevelopCreateHref = KSTableUtils_derefOpt( docValue, '_serverProvidedMetadata', 'links', '/rels/xmp_develop_create', 'href' )
	KSTableUtils_setNested( docValue, "<href>", '_serverProvidedMetadata', 'links', '/rels/xmp_develop_create', 'href' )
	rxTest:expectEqual( true, not not xmpDevelopCreateHref )
	
	local expectedFormFactor
	
	if WFPlatformInfo.isMobile() then
		expectedFormFactor = 'phone'
	else
		expectedFormFactor = 'desktop'
	end

	local localOnly
	
	if WFPlatformInfo.isDesktop then
	
		localOnly = {
			files = {
				xmp_develop = {
					fileSize = expectedData.xmpDevelopSettingsFileSize,
					path = expectedData.xmpDevelopSettingsRelPath,
					sha256 = expectedData.developSettingsSha256,
				}
			}
		}

	elseif WFPlatformInfo.isiOS then
	
		localOnly = {
			files = {
				thumbnail2x = {
					path = expectedData.thumbnailFilePath,
					contentId = expectedData.thumbnailContentId,
					fileSize = expectedData.thumbnailFileSize,
				},
				xmp_develop = {
					fileSize = expectedData.xmpDevelopSettingsFileSize,
					path = expectedData.xmpDevelopSettingsRelPath,
					contentId = expectedData.developSettingsSha256,
				}
			}
		}
	
	else
	
		KSTableUtils_setNested( docValue, "<guid>", '_localOnly', 'thumbnail2x', 'revId' )
	
		localOnly = {
			thumbnail2x = {
				md5 = expectedData.thumbMd5,
				referenceHeight = 960,
				referenceWidth = 1280,
				epoch = 1,
				revId = "<guid>",
				size = expectedData.thumbnailFileSize
			},
			files = {
				xmp_develop = {
					fileSize = expectedData.xmpDevelopSettingsFileSize,
					path = expectedData.xmpDevelopSettingsRelPath,
					sha256 = expectedData.developSettingsSha256,
				}
			}
		}
	
	end

	localOnly.files.aux = {
		[ expectedData.mask1Fingerprint ] = {
			relativePath = expectedData.mask1RelativeFilePath,
		},
		[ expectedData.mask2Fingerprint ] = {
			relativePath = expectedData.mask2RelativeFilePath,
		},
	}

	local expectedDocValue = {
		_id = "<guid>",
		_newRevId = "<guid>",
		_localOnly = localOnly,
		_serverProvidedMetadata = {
			links = {
				["/rels/rendition_type/thumbnail2x"] = {
					href = "/href/bogus/thumbnail2x",
				},
				["/rels/xmp/develop"] = {
					href = "/href/bogus/xmp_develop",
				},
				["/rels/xmp_develop_create"] = {
					href = "<href>",
					templated = true,
				},
			},
			revision_ids = { "<guid>" },
		},
		asset = {
			id = "<guid>",
		},
		createdManually = false,
		develop = {
			croppedHeight = 1000,
			croppedWidth = 1000,
			masks = {
				[ expectedData.mask1AuxId ] = {
					digest = expectedData.mask1Fingerprint,
				},
				[ expectedData.mask2AuxId ] = {
					digest = expectedData.mask2Fingerprint,
				},
			},
			processingModel = "lightroom",
			userOrientation = 1,
			xmpCameraRaw = {
				sha256 = expectedData.developSettingsSha256,
			},
		},
		importedOnDevice = {
			formFactor = expectedFormFactor,
		},
		name = "<string>",
		subtype = "edit",
		type = "version",
	}

	rxTest:expectEqual( expectedDocValue, docValue )

end

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

return WFRxTestCase.wrapSuite( 'WFCatVersionTestUtilsTest', WFCatVersionTestUtilsTest )
