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

WFCatPixelFileCacheCheckTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 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.utils
local KSIdGenerator = require 'KSIdGenerator'
local WFCrypto = require 'WFCrypto'
local WFFileUtils = require 'WFFileUtils'
local KSPathUtils = require 'KSPathUtils'
local WFPlatformInfo = require 'WFPlatformInfo'

	-- WF.core.self_test
local WFRxTestCase = require 'WFRxTestCase'
local WFCatMockCatalog = require 'WFCatMockCatalog'

	-- WF.oz_client.managed_catalog.pixel_provider
local WFCatPixelFileCacheCheck = require 'WFCatPixelFileCacheCheck'
local WFCatPixelPathGenerator = require 'WFCatPixelPathGenerator'

--------------------------------------------------------------------------------
-- from PiL
local function permutationGenerator( a, n )

	n = n or #a

	if n <= 1 then

		coroutine.yield( a )

	else

		for i = 1, n do

			-- put i-th element as the last one
			a[n], a[i] = a[i], a[n]

			-- generate all permutations of the other elements
			permutationGenerator( a, n - 1 )

			-- restore i-th element
			a[n], a[i] = a[i], a[n]

		end

	end

end

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

local function permutations( a )

	local n = #a

	return coroutine.wrap(function () permutationGenerator( a, n ) end )

end

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

local function rxTestTupleAPIUsage( rxTest )

	rxTest:setTimeLimit( 10 )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )
	
	local basePath = wfCatalog:fileSpec():pixelsPath()
	
	rxTest:finally( function()
		WFFileUtils.delete( basePath )
	end )
	
	local cacheChecker = WFCatPixelFileCacheCheck( basePath )

	local checkFunc = cacheChecker.checkTuples

	rxTest:expectError( checkFunc )

	rxTest:expectError( checkFunc, cacheChecker, "bogus" )

	rxTest:expectError( checkFunc, cacheChecker, {} ) -- empty array is a no-no

	local localAssetId = 1

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	local digest = WFCrypto.MD5.digest( KSIdGenerator.uuidInLowercaseHex() )

	for i, bogusTupleArray in ipairs { 
								{ "bogus" }, -- an array with a non-tuple
								{ { "pixStreamId", "badLocalAssetId" } }, -- a bogus tuple 
								{ { "pixStreamId", localAssetId, "badAssetId" } }, -- a bogus tuple 
								{ { "pixStreamId", localAssetId, assetId, "badDigest" } }, -- a bogus tuple 
								{ { "pixStreamId", localAssetId, assetId, digest, 1024 } }, -- a bogus tuple (odd longSide)
								{ { "pixStreamId", localAssetId, assetId, digest, { "wut" } } }, -- a bogus tuple 
								{ { "pixStreamId", localAssetId, assetId, digest, { 2^16 } } }, -- a bogus tuple 
								{ { "pixStreamId", localAssetId, assetId, digest, { 123 } } }, -- a bogus tuple (odd longSide)
							} do

		rxTest:log( "running bogus check " .. tostring( i ) )

		local watchBogus = rxTest:observe( cacheChecker:checkTuples( bogusTupleArray ) )

		rxTest:log( "finishing bogus check " .. tostring( i ) )

		watchBogus:expectError( ':ignore:' )

	end

	local assetLongSideSetBySubtypes = {
		["image"] = {
				{ 2, 640, 2 ^16 - 2, "full" }
			},
		["video"] =	{
				{"full", "360p", "720p", "1080p", "1080p709" },
				{ 2, 640, "full", "360p", "1080p"},
			}
	}
	
	for assetSubtype, longSideSets in pairs( assetLongSideSetBySubtypes ) do
						
		for _, longSideSet in ipairs( longSideSets ) do 
		
			local i = 1
			
			for longSides in permutations( longSideSet ) do

				rxTest:log( "running valid path check " .. tostring( i ) )

				local pathTuple = { "pixStreamId", localAssetId, assetId, digest, longSides, i, nil, assetSubtype }


				local watchCheck = rxTest:observe( cacheChecker:checkTuples( { pathTuple } ) )

				local pathExistsDataArray = {}

				for _, longSide in ipairs( longSides ) do

					local expectedPath 
					
					-- image renditions requests are number variables or "full"
					-- if the original asset subtype is a video, "full" is actually a mp4 rendition.
					if type( longSide ) == "number" or (assetSubtype == "image" and longSide == "full") then
					
						expectedPath = WFCatPixelPathGenerator.pathForPreviewDigestAndLongEdge( 
													basePath, assetId, digest, longSide )
					else
					
						expectedPath = WFCatPixelPathGenerator.pathForPreviewDigestAndLongEdgeMP4( 
									basePath, assetId, digest, longSide )
						
					end

					pathExistsDataArray[ #pathExistsDataArray + 1 ] = 
						{ false, expectedPath, longSide }

				end

				watchCheck:expectData( { { pathExistsDataArray, "pixStreamId",
											localAssetId, assetId, digest, i, nil, assetSubtype } } )

				rxTest:log( "finishing valid path check " .. tostring( i ) )

				i = i + 1
			end
		end
	end
	
end

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

local function rxTestPathAPIUsage( rxTest )

	local cacheChecker = WFCatPixelFileCacheCheck( 'foo' )

	local testData = KSPathUtils.getStandardFilePath( 'testdata' )
	local filePath = testData .. '/text/1MB.txt'

	local watchCheck = rxTest:observe( cacheChecker:checkPath( testData ) )

	watchCheck:expectData( false, false )

	watchCheck = rxTest:observe( cacheChecker:checkPath( filePath ) )

	watchCheck:expectData( true, false )

	-- Asset on offline Volume

	local offlinePath = '/Volumes/foo/images'

	if WFPlatformInfo.isWindows then
		offlinePath = 'Q:\foo\images'
	end

	watchCheck = rxTest:observe( cacheChecker:checkPath( offlinePath ) )

	watchCheck:expectData( false, true )
	
end

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

return WFRxTestCase.wrapSuite( 'WFCatPixelFileCacheCheckTest', {

	rxTestTupleAPIUsage = rxTestTupleAPIUsage,
	
	rxTestPathAPIUsage = rxTestPathAPIUsage

} )
