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

LrmAssetMetadataModelTest.lua

------------------
ADOBE CONFIDENTIAL
------------------
Copyright 2022 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 KSData = require 'KSData'
local KSTableUtils = require 'KSTableUtils'
local WFRxTestCase = require 'WFRxTestCase'
local KSIdGenerator = require 'KSIdGenerator'
local LrmCatalogTestHelper = require 'LrmCatalogTestHelper'
local LrmAssetMetadataModel = require 'LrmAssetMetadataModel'
local WFOzRenditionServerUtils = require 'WFOzRenditionServerUtils'

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

local derefOpt = assert( KSTableUtils.derefOpt )

local LrmAssetMetadataModelTest = {}

local catalog, docStore, indexStore

function LrmAssetMetadataModelTest.setUp()

	catalog = LrmCatalogTestHelper.makeCatalogService()

	docStore = catalog:docStore()
	indexStore = catalog:indexStore()

end

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

function LrmAssetMetadataModelTest.tearDown()

	if catalog then

		catalog:close()

	end

end

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

local function getAssetDocument ( assetId )

	local docValue = {
		type = 'asset',
		subtype = 'video',
		_id = assetId,
		importSource = {
			fileSize = 400000,
			originalWidth = 1200,
			originalHeight = 1000,
		},
		develop = {
			adjustments = {
				adjustmentsFraction = 1000,
				blacks2012 = 0,
			}
		}
	}

	return docValue

end


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

local function createAsset( rxTest )

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	local originalDocument = getAssetDocument( assetId )

	rxTest:waitFor( docStore:applyOverlay( originalDocument ) )

	return assetId

end

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

local function getMD5( assetId, ksdata )

	local newMD5 = ksdata:digest( 'MD5' )

	return WFOzRenditionServerUtils.getFullMD5HashedKey( newMD5, assetId )

end

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

local function stripEpoch( t )
	if t then
		t.epoch = nil
	end
end

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

function LrmAssetMetadataModelTest.rxTestSaveRenditions( rxTest )

	local assetId = createAsset ( rxTest )

	local model = LrmAssetMetadataModel.makeModel( catalog )

	local revId = KSIdGenerator.uuidInLowercaseHex()

	local thumbData = KSData.make( "thumbKSData" )

	local previewData = KSData.make( "prevKSData" )

	rxTest:expectSingleValue( model.events.saveRenditions(model, assetId, revId, thumbData, previewData ), ':ignore:')

	local expectedTable = {
		thumbnail2x = {
			revId = revId,
			size = KSData.length( thumbData ),
			md5 = getMD5( assetId, thumbData )
		},
		preview = {
			revId = revId,
			size = KSData.length( previewData ),
			md5 = getMD5( assetId, previewData )
		},
	}

	local result = {}

	rxTest:expectSingleValue(
		catalog:docStore():docValue( assetId )
		:tap( function( docValue )
			-- Take only relevant fields

			local thumbTable = derefOpt( docValue, "_localOnly", "thumbnail2x" )
			stripEpoch( thumbTable )

			local previewTable = derefOpt( docValue, "_localOnly", "preview" )
			stripEpoch( previewTable )

			result = {
				thumbnail2x = thumbTable,
				preview = previewTable,
			}
		end ),
		':ignore:' )

	rxTest:expectEqual( expectedTable, result )

end

return WFRxTestCase.wrapSuite( 'LrmAssetMetadataModelTest', LrmAssetMetadataModelTest )
