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

ADOBE CONFIDENTIAL
------------------
Copyright 2022 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

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

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

-- WF.core.utils
local KSIdGenerator = require 'KSIdGenerator'
local KSTableUtils = require 'KSTableUtils'
local derefOpt = assert( KSTableUtils.derefOpt )

-- WF.lrmobile
local LrmVideoDevelopModel = require 'LrmVideoDevelopModel'

-- test suite
local WFRxTestCase = require 'WFRxTestCase'

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

-- @mark LrmVideoDevelopModelTest namespace

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

local LrmVideoDevelopModelTest = {}

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

local kOriginalPath = '/original/path'

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

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,
			}
		},
		_localOnly = {
			thumbnail2x = {
				md5 = '<hash>',
			},
			preview = {
				md5 = '<hash>',
			},
			originalFilePath = kOriginalPath
		},
		_serverProvidedMetadata = {
			links = {
				[ '/rels/master' ] = {
					href = 'catalogs/f50509f546184519afa38d4e55e442fc/assets/496e4bec7eff1bee558b4760ae00efa3/revisions/16b97d9d9e3042d09633c3878622d48a/master',
				},
				[ '/rels/proxy_type/360p' ] = {
					href = 'catalogs/f50509f546184519afa38d4e55e442fc/assets/496e4bec7eff1bee558b4760ae00efa3/revisions/16b97d9d9e3042d09633c3878622d48a/master',
				},
				[ '/rels/proxy_type/1080p' ] = {
					href = 'catalogs/f50509f546184519afa38d4e55e442fc/assets/496e4bec7eff1bee558b4760ae00efa3/revisions/16b97d9d9e3042d09633c3878622d48a/master',
				},
				[ '/rels/rendition_type/360p' ] = {
					href = 'catalogs/f50509f546184519afa38d4e55e442fc/assets/496e4bec7eff1bee558b4760ae00efa3/revisions/16b97d9d9e3042d09633c3878622d48a/master',
				},
				[ '/rels/rendition_type/720p' ] = {
					href = 'catalogs/f50509f546184519afa38d4e55e442fc/assets/496e4bec7eff1bee558b4760ae00efa3/revisions/16b97d9d9e3042d09633c3878622d48a/master',
				},
			}
		}
	}

	return docValue

end

local function getExpectedDocument( docValue )

	local urls = {}
	urls.proxy_360p = derefOpt( docValue, "_serverProvidedMetadata", "links", "/rels/proxy_type/360p", "href" )
	urls.proxy_1080p = derefOpt( docValue, "_serverProvidedMetadata", "links", "/rels/proxy_type/1080p", "href" )
	urls.rendition_360p = derefOpt( docValue, "_serverProvidedMetadata", "links", "/rels/rendition_type/360p", "href" )
	urls.rendition_720p = derefOpt( docValue, "_serverProvidedMetadata", "links", "/rels/rendition_type/720p", "href" )
	urls.main = derefOpt( docValue, "_serverProvidedMetadata", "links", "/rels/master", "href" )

	local expected = {
		settings = docValue.develop,
		urls = urls,
		originalFilePath = kOriginalPath,
		originalHeight = derefOpt( docValue, "importSource", "originalHeight"),
		originalWidth = derefOpt( docValue, "importSource", "originalWidth"),
		pixelAspectRatio = 1,
	}

	return expected

end

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

local function stripUserUpdated( docValue )

	local developAdjustments = derefOpt( docValue, "settings", "adjustments" )

	if type( developAdjustments ) == 'table' and next( developAdjustments ) then

		KSTableUtils.setNested( docValue, nil, 'settings', 'adjustments', 'userUpdated' )
		KSTableUtils.setNested( docValue, nil, 'settings', 'adjustments', 'device' )

	end

	local developTemporal = derefOpt( docValue, "settings", "temporal" )

	if type( developTemporal ) == 'table' and next( developTemporal )  then

		KSTableUtils.setNested( docValue, nil, 'settings', 'temporal', 'userUpdated' )
		KSTableUtils.setNested( docValue, nil, 'settings', 'temporal', 'device' )

	end

	return docValue

end

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

local function createCatalogService( rxTest )

	local catalogService = LrmCatalogTestHelper.makeCatalogService( rxTest )
	
	rxTest:finally( function()
		catalogService:close()
	end )
	
	return catalogService
	
end

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

function LrmVideoDevelopModelTest.rxTestPixelAspectRatio( rxTest )

	local catalog = createCatalogService( rxTest )

	local docStore = catalog:docStore()
	
	local assetId = KSIdGenerator.uuidInLowercaseHex()

	local originalDocument = getAssetDocument( assetId )

	originalDocument.video = {
		pixelAspectRatio = {
			15
		}
	}

	local expected = getExpectedDocument( originalDocument )

	expected.pixelAspectRatio = 15

	rxTest:waitFor( docStore:applyOverlay( originalDocument ) )

	local model = LrmVideoDevelopModel.makeModel( catalog, assetId )

	local obs = rxTest:observe( model.value )

	obs:expectData( expected )

	local overlay = {
		_id = assetId,
		video = {
			pixelAspectRatio = {
				5, 2
			}
		},
	}

	rxTest:waitFor( docStore:applyOverlay( overlay ) )

	expected.pixelAspectRatio = 2.5

	obs:expectData( expected )

	model.close( model )

	rxTest:expectEqual( model.tsLogEntry.exitStatus, 'close' )

end

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

function LrmVideoDevelopModelTest.rxTestMakeModel( rxTest )

	local catalog = createCatalogService( rxTest )

	local docStore = catalog:docStore()
	
	local assetId = KSIdGenerator.uuidInLowercaseHex()

	local originalDocument = getAssetDocument( assetId )

	local expected = getExpectedDocument( originalDocument )

	rxTest:waitFor( docStore:applyOverlay( originalDocument ) )

	local model = LrmVideoDevelopModel.makeModel( catalog, assetId )

	local obs = rxTest:observe( model.value

	:map( function( docValue )

		return stripUserUpdated ( docValue )

	end ) )

	obs:expectData( expected )

	local developChanges = "{\"adjustments\":{\"adjustmentsFraction\":1001,\"postCropVignetteMidpoint\":50000}}"

	local revId = KSIdGenerator.uuidInLowercaseHex()

	-- Saving Edit changes
	rxTest:expectSingleValue( model.models.saveEditChanges(model, revId, developChanges).value, revId)

	developChanges = {
		adjustments = {
			adjustmentsFraction = 1001,
			postCropVignetteMidpoint = 50000,
		},
	}

	expected.settings = developChanges

	obs:expectData( expected )

	model.close( model )

	rxTest:expectEqual( model.tsLogEntry.exitStatus, 'save And close' )

end

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

return WFRxTestCase.wrapSuite( 'LrmVideoDevelopModelTest', LrmVideoDevelopModelTest )
