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

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 WFPlatformInfo = require 'WFPlatformInfo'
local LrAssetUtils = require 'LrAssetUtils'

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

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

local LrAssetUtilsTest = {}

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

local function createFakeWFCatalog( rxTest )

	local optArgs = {}

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

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

end

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

function LrAssetUtilsTest.rxTestIsLatestMetadataUploaded( rxTest )

	local wfCatalog = createFakeWFCatalog( rxTest )

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	-- Create a new asset doc. By default, the asset doc has a link
	-- to the master.

	local optOverrides = {
		internalXmpCameraRaw = true
	}

	local assetDoc = WFCatAssetTestUtils.createAssetDocument( rxTest, wfCatalog, assetId, optOverrides )
	assetDoc._revId = assetDoc._newRevId
	assetDoc._newRevId = nil
	assetDoc._serverProvidedMetadata.revision_ids = nil
	
	-- No labels, and rev id is not listed in SPM, so this should evaluate to false.

	rxTest:expectEqual( false, LrAssetUtils.isLatestMetadataUploaded( assetDoc ) )

	-- Now that there's an Oz label, this should be true.

	assetDoc._labels = {}
	assetDoc._labels[ 'oz' ] = true

	rxTest:expectEqual( true, LrAssetUtils.isLatestMetadataUploaded( assetDoc ) )

	-- Remove the Oz label, but add a '_dontsendtooz' label.

	assetDoc._labels[ 'oz' ] = nil
	assetDoc._labels[ '_dontsendtooz' ] = true

	rxTest:expectEqual( true, LrAssetUtils.isLatestMetadataUploaded( assetDoc ) )

	-- Remove the '_dontsendtooz' label, but add a '_pendingCommit' label.

	assetDoc._labels[ '_dontsendtooz' ] = nil
	assetDoc._labels[ '_pendingCommit' ] = true

	rxTest:expectEqual( true, LrAssetUtils.isLatestMetadataUploaded( assetDoc ) )

	-- No labels, but the revision id is listed in the SPM

	assetDoc._labels[ '_pendingCommit' ] = nil

	assetDoc._serverProvidedMetadata.revision_ids = {
		assetDoc._revId
	}

	rxTest:expectEqual( true, LrAssetUtils.isLatestMetadataUploaded( assetDoc ) )

end

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

function LrAssetUtilsTest.rxTestHasNonSyncingLabel( rxTest )

	local wfCatalog = createFakeWFCatalog( rxTest )

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	local assetDoc = WFCatAssetTestUtils.createAssetDocument( rxTest, wfCatalog, assetId )

	-- No labels by default

	rxTest:expectEqual( false, LrAssetUtils.hasNonSyncingLabel( assetDoc ) )

	-- Verify non syncing labels

	local nonSyncingLabels = LrAssetUtils._TEST_nonSyncingLabels

	for label in pairs( nonSyncingLabels ) do

		assetDoc._labels = {
			[ label ] = true
		}

		rxTest:expectEqual( true, LrAssetUtils.hasNonSyncingLabel( assetDoc ) )

	end

	-- Add a meaningless label

	assetDoc._labels = {
		[ 'butter' ] = true
	}

	rxTest:expectEqual( false, LrAssetUtils.hasNonSyncingLabel( assetDoc ) )

end

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

return WFRxTestCase.wrapSuite( 'LrAssetUtilsTest', LrAssetUtilsTest )
