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

NmbCatWatermarkTest.lua

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

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

NOTICE: All information contained herein is, and remains 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 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 Systems Incorporated.

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


	-- WF.core.self_test

local WFRxTestCase = require 'WFRxTestCase'

	-- WF.oz_client.managed_catalog
local WFCatWatermark = require 'WFCatWatermark'
local WFCatMockCatalog = require 'WFCatMockCatalog'
local KSPathUtils = require 'KSPathUtils'
local WFFileUtils = require 'WFFileUtils'
local KSIdGenerator = require "KSIdGenerator"
local WFTestFileUtils = require "WFTestFileUtils"
local WFPlatformInfo = require 'WFPlatformInfo'

local NmbCatWatermark = require "NmbCatWatermark"

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

local NmbCatWatermarkTest = {}

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

local function getWatermarkTestSetttings()
	
	return {
		opacity = 73,
		rotation = 90,
		position = "center",
		xOffset = 0.23,
		yOffset = 0.40,
		size = 22,
		text = "@ saravana",
		font = {
			name = "SourceSansPro",
			bold = true,
			italic = true,
		},
		shadow = {
			enabled = "off"
		},
		rgbColor = { 0.4, 0.33, 0.0 },
		activeWatermarkType = "text",
	}
	
end


-----------------------------------------------------------------------------
--
--1. create multiple watermarks (esp. image watermarks) in fake catalog (as per other unit tests) - I believe this would be similar to one of your WFCatWatermarkTest unit test 
--2. have the file paths for each graphic watermark created (using WFFileUtils)
--3. then trigger my purge API which extracts getAllWatermarksInfo() and then obtains all the filepaths from each of watermark infos.
--4. expect in return after calling my purge API that only the used filepaths remain and WFFileUtils.exists() fails for every other cleaned up file.

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


-- [A, B, C, D, E, F, G] => files present under catalog folder (7 files)
-- C.png, E.png, F.png => referred to from watermarks [three watermarks - image watermarks]
-- Cleanup/Purge: A, B, D, G => expectation from test

if WFPlatformInfo.isDesktop then

function NmbCatWatermarkTest.rxTestPurgeOrphanWatermarkAssets( rxTest )

-- This test case verifies whether WF stores the file path and 
-- its derived attributes such as file size, sha256 in a _localOnly table inside the watermark doc 
-- when create watermark API being invoked with an optFilePath(valid path) argument 

	rxTest:setTimeLimit( 30 )
	
	-- Gist:
	-- 1. Create a test image watermarks folder under catalog folder 
	-- 2. Create multiple subfolders under the test image watermarks folder
	-- 3. Create some image watermarks and place some test graphics into each one of them.
	-- 4. Also additionnally add more unused graphic to some of these folders
	-- 5. Trigger prune watermarks
	-- 6. Validate if the unused files are cleaned up and only the used graphics remain
	-- 7. Cleanup test folder
	
	-- 1. Create a test image watermarks folder under catalog folder 
	local testDataFolder = KSPathUtils.getStandardFilePath( 'testdata' )
	local imageWatermarksPath = KSPathUtils.child( testDataFolder, "watermarks" )
	pcall( WFFileUtils.mkdir, imageWatermarksPath )
	

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )
	
	local usedGraphicFiles = {}
	local unusedGraphicFiles = {}
	local lastWatermarkId
	
	local function createWatermarkSettingsParameters( watermarkNum )

		local name = "Image Watermark#" .. watermarkNum
		
		local settings = getWatermarkTestSetttings()

		local wmId = KSIdGenerator.uuidInLowercaseHex()
		
		
		local testDataPath = KSPathUtils.getStandardFilePath( "testdata" )
		local relativePath = KSPathUtils.child( 'assets', '1.5MB.jpg' )
		local absolutePath = KSPathUtils.child( testDataPath, relativePath )
		local testWatermarkFolder = KSPathUtils.child( imageWatermarksPath, wmId )
		pcall( WFFileUtils.mkdir, testWatermarkFolder )

		WFTestFileUtils.addTestFolder( imageWatermarksPath )
		
		local testWatermarkFileName = KSIdGenerator.uuidInLowercaseHex() .. ".jpg"
		relativePath = KSPathUtils.child( "watermarks", KSPathUtils.child( wmId, testWatermarkFileName ) )
		
		local testWatermarkFilePath = KSPathUtils.child( testWatermarkFolder, testWatermarkFileName )
		WFFileUtils.copy( absolutePath, testWatermarkFilePath )
		
		local testUnusedWatermarkFilePath = KSPathUtils.child( testWatermarkFolder, KSIdGenerator.uuidInLowercaseHex() .. ".jpg" )
		WFFileUtils.copy( absolutePath, testUnusedWatermarkFilePath )
		
		usedGraphicFiles[ #usedGraphicFiles + 1 ] = testWatermarkFilePath
		unusedGraphicFiles[ #unusedGraphicFiles + 1 ] = testUnusedWatermarkFilePath
		
		return name, settings, relativePath, '1.5MB.jpg', wmId
	end
	
	rxTest:waitFor(
		WFCatWatermark.createWatermark( wfCatalog, createWatermarkSettingsParameters( 1 ) )
			:tap( function( watermarkId )					
				lastWatermarkId = watermarkId
			end ) )

	rxTest:waitFor(
		WFCatWatermark.createWatermark( wfCatalog, createWatermarkSettingsParameters( 2 ) )
			:tap( function( watermarkId )					
				lastWatermarkId = watermarkId
			end ) )
	
	rxTest:waitFor(
		WFCatWatermark.createWatermark( wfCatalog, createWatermarkSettingsParameters( 3 ) )
			:tap( function( watermarkId )					
				lastWatermarkId = watermarkId
			end ) )
	
	-- Ensure that the unused files are indeed cleaned up via purge!
	rxTest:expectSingleValue(
		NmbCatWatermark.purgeOrphanWatermarkAssets( wfCatalog )
			:take(1)
			:map( function(data)
				-- If some of the unused files are still existing, then the purge operation didn;t do what it is supposed to.
				for _, unusedFile in ipairs( unusedGraphicFiles ) do
					if WFFileUtils.exists( unusedFile ) then
						return false
					end
				end
				-- If some of the used files are not exissting, then the purge operation didn;t do what it is supposed to.
				for _, usedFile in ipairs( usedGraphicFiles ) do
					if not WFFileUtils.exists( usedFile ) then
						return false
					end
				end
				return true
			end ), true )
	
	-- 6. Cleanup 
	rxTest:finally( function()
		WFFileUtils.delete( imageWatermarksPath )
	end )
	
end

end

return WFRxTestCase.wrapSuite( 'NmbCatWatermarkTest', NmbCatWatermarkTest )
