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

LrmCatXmpDevelopTest.lua

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

ADOBE CONFIDENTIAL
___________________

Copyright 2020 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'

local KSIdGenerator = require 'KSIdGenerator'
local KSTableUtils = require 'KSTableUtils'
local KSData = require "KSData"
local WFFileUtils = require 'WFFileUtils'
local WFPlatformInfo = require "WFPlatformInfo"

    -- WF.oz_client.managed_catalog
local LrmCatXmpDevelop = require 'LrmCatXmpDevelop'
local WFCatMockCatalog = require 'WFCatMockCatalog'
local WFMockHttpRequest = require 'WFMockHttpRequest'
local WFCatAssetTestUtils = require 'WFCatAssetTestUtils'

local OzFilePaths = require "OzFilePaths"

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


if not WFPlatformInfo.isMobile() then
	return {}
end

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

local LrmCatalogTestHelper = require 'LrmCatalogTestHelper'

local TEST_TIMEOUT = 5

local LrmCatXmpDevelopTest = {}

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

local function createFakeWFCatalog( rxTest )

	local optArgs = {}
	optArgs.indexDefinitions = 'LrmCatalogIndexDefinitions.lua'

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

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

local function generateAsset( rxTest, catalog, xmpCameraRawSection, spaceId, xmpLinkSection )

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	local overlay = {
		type = 'asset',
		subtype = 'image',
		_id = assetId,
		_spaceId = spaceId,
		importSource = {
			originalWidth = 100,
			originalHeight = 100,
		},
		develop = {
			xmpCameraRaw = xmpCameraRawSection
		},
		_serverProvidedMetadata = {
			links = {
				[ '/rels/master' ] = assetId,
				[ '/rels/xmp/develop' ] = xmpLinkSection
			}
		}
	}
	
	rxTest:waitFor( catalog:docStore():applyOverlay( overlay ) )
	
	return assetId

end

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

function LrmCatXmpDevelopTest.rxTestExternalizeDevelopSettings( rxTest )

	rxTest:setTimeLimit( TEST_TIMEOUT )
	
	local wfCatalog = createFakeWFCatalog( rxTest )
	
	-- Asset with large xmp settings.
	local xmpCameraRaw = "this is a dummy xmp camera raw string"
	
	local sha256 = KSData.make( xmpCameraRaw ):digest( 'SHA256' )
	
	local assetId = generateAsset( rxTest, wfCatalog, xmpCameraRaw )

	rxTest:expectSingleValue( LrmCatXmpDevelop.externalizeDevelopSettings( wfCatalog, assetId, 10 ),
		true )
	
	local newDocValue
	
	rxTest:expectSingleValue( wfCatalog:docStore():docValue( assetId )
		:map( function( docValue )
			newDocValue = docValue			
			return nil
		end ),
		nil )
	
	local newXmpCameraRawSection = KSTableUtils.derefOpt( newDocValue, 'develop', 'xmpCameraRaw' )
	rxTest:expectEqual( 'table', type( newXmpCameraRawSection ) )
	rxTest:expectEqual( sha256, newXmpCameraRawSection.sha256 )
	
	local localOnlyXmpFilesSection = KSTableUtils.derefOpt( newDocValue, '_localOnly', 'files', 'xmp_develop' )

	rxTest:expectEqual( 'table', type( localOnlyXmpFilesSection ) )
	
	if WFPlatformInfo.isiOS then
		rxTest:expectEqual( newXmpCameraRawSection.contentId, localOnlyXmpFilesSection.sha256 )	
	else
		rxTest:expectEqual( newXmpCameraRawSection.sha256, localOnlyXmpFilesSection.sha256 )	
	end
	
	local _, absPath = OzFilePaths.getXmpDevelopPath( wfCatalog:ozCatalogId(), newDocValue  ) 
	
	rxTest:expectEqual( 'file', WFFileUtils.exists( absPath ) )
	
	-- Asset with already externalized settings. Asset shouldn't be modified.
	rxTest:expectSingleValue( LrmCatXmpDevelop.externalizeDevelopSettings( wfCatalog, assetId, 10 ),
		true )
	
	local newRevId
	rxTest:expectSingleValue( wfCatalog:docStore():docValue( assetId )
		:map( function( docValue )
			newRevId = docValue._revId
			
			return newRevId
		end ),
		':ignore:' )
	rxTest:expectEqual( newDocValue._revId, newRevId )
	
end

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

function LrmCatXmpDevelopTest.rxTestExternalizeDevelopSettingsForSpaceAsset( rxTest )

	rxTest:setTimeLimit( TEST_TIMEOUT )
	
	local wfCatalog = createFakeWFCatalog( rxTest )
	
	-- Asset with large xmp settings.
	local xmpCameraRaw = "this is a dummy xmp camera raw string"
	
	local sha256 = KSData.make( xmpCameraRaw ):digest( 'SHA256' )

	local spaceId = KSIdGenerator.uuidInLowercaseHex()
	
	local assetId = generateAsset( rxTest, wfCatalog, xmpCameraRaw, spaceId )

	rxTest:expectSingleValue( LrmCatXmpDevelop.externalizeDevelopSettings( wfCatalog, assetId, 10 ),
		true )
	
	local newDocValue
	
	rxTest:expectSingleValue( wfCatalog:docStore():docValue( assetId )
		:map( function( docValue )
			newDocValue = docValue			
			return nil
		end ),
		nil )
	
	local newXmpCameraRawSection = KSTableUtils.derefOpt( newDocValue, 'develop', 'xmpCameraRaw' )
	rxTest:expectEqual( 'table', type( newXmpCameraRawSection ) )
	rxTest:expectEqual( sha256, newXmpCameraRawSection.sha256 )
	
	local localOnlyXmpFilesSection = KSTableUtils.derefOpt( newDocValue, '_localOnly', 'files', 'xmp_develop' )

	rxTest:expectEqual( 'table', type( localOnlyXmpFilesSection ) )
	
	if WFPlatformInfo.isiOS then
		rxTest:expectEqual( newXmpCameraRawSection.contentId, localOnlyXmpFilesSection.sha256 )	
	else
		rxTest:expectEqual( newXmpCameraRawSection.sha256, localOnlyXmpFilesSection.sha256 )	
	end
	
	local _, absPath = OzFilePaths.getXmpDevelopPath( wfCatalog:ozCatalogId(), newDocValue  ) 
	
	if WFPlatformInfo.isAndroid then
	
		-- The path should contain the space Id.
		
		assert( string.match( absPath, spaceId ), "The path does not contain the space Id" )	
	
	end
	
	-- And the file should exist..
	
	rxTest:expectEqual( 'file', WFFileUtils.exists( absPath ) )
	
end

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

function LrmCatXmpDevelopTest.rxTestGetExternalizeDevelopSettings( rxTest )

	local catalogService, wfCatalog = LrmCatalogTestHelper.makeCatalogService( rxTest )

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	local optOverrides = {
		ozDevelopSettingsAreInvalid = true,
		noLocalDevelopSettings = true,
	}

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

	local sha256 = documentData.developSettingsSha256
	local xmpCameraRaw = documentData.xmpDevelopSettingsString
	local developSettingsBlob = KSData.make( xmpCameraRaw )

	rxTest:waitFor( wfCatalog:docStore():setDocValue( assetDoc ) )
	
	wfCatalog:ozCatalogSpec():expectHttpRequest(
		WFMockHttpRequest()
			:put( "/href/bogus/xmp_develop" )
			:expectRequestData( ":ignore:" )
			:respond( '200', developSettingsBlob ) )
			
	rxTest:expectSingleValue( LrmCatXmpDevelop.getExternalXmpDevelop( catalogService, assetId, sha256 )
								:map( function( xmpCameraRawBlob, xmpRelPath, selectedDigests )
									return tostring( xmpCameraRawBlob ), selectedDigests
								end), xmpCameraRaw, nil )
	
end

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

return WFRxTestCase.wrapSuite( 'LrmCatXmpDevelopTest', LrmCatXmpDevelopTest )
