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

WFCatPixelPathGeneratorTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 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.os
local KSPathUtils = require 'KSPathUtils'
local WFFileUtils = require 'WFFileUtils'

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

	-- WF.core.utils
local KSIdGenerator = require 'KSIdGenerator'
local KSTestUtils = require 'KSTestUtils'

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

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

local WFCatPixelPathGeneratorTest = {}

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

function WFCatPixelPathGeneratorTest.rxTestPathForProxyTypeAndUuid( rxTest )
	
	local pixelsPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), 'WFCatPixelPathGeneratorTest' )

	rxTest:finally( function()
		WFFileUtils.delete( pixelsPath )
	end )

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	-- With File Extension

	local qtPath = WFCatPixelPathGenerator.pathForProxyTypeAndUuidWithExtension( pixelsPath, 'panorama', assetId )

	rxTest:log( "qtPath = " .. qtPath )
	
	rxTest:expectEqual( pixelsPath, qtPath:sub( 1, #pixelsPath ) )
	
	rxTest:expectEqual( '.dng', qtPath:sub( -4 ) )

	rxTest:expectEqual( assetId:sub( 5 ), qtPath:sub( -32, -5 ) )

	rxTest:expectEqual( 'directory', WFFileUtils.exists( KSPathUtils.parent( qtPath ) ) )
	
	-- Without File Extension
	
	qtPath = WFCatPixelPathGenerator.pathForProxyTypeAndUuid( pixelsPath, 'panorama', assetId )

	rxTest:log( "qtPath = " .. qtPath )
	
	rxTest:expectEqual( pixelsPath, qtPath:sub( 1, #pixelsPath ) )
	
	rxTest:expectNotEqual( '.dng', qtPath:sub( -4 ) )

	rxTest:expectEqual( assetId:sub( 5 ), qtPath:sub( -28, -1 ) )

	rxTest:expectEqual( 'directory', WFFileUtils.exists( KSPathUtils.parent( qtPath ) ) )
	

end

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

function WFCatPixelPathGeneratorTest.rxTestPathForVideoProxyTypeAndUuid( rxTest )
	
	local pixelsPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), 'WFCatPixelPathGeneratorTest' )

	rxTest:finally( function()
		WFFileUtils.delete( pixelsPath )
	end )

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	-- With File Extension

	local qtPath = WFCatPixelPathGenerator.pathForProxyTypeAndUuidWithExtension( pixelsPath, '720p', assetId )

	rxTest:log( "qtPath = " .. qtPath )
	
	rxTest:expectEqual( pixelsPath, qtPath:sub( 1, #pixelsPath ) )
	
	rxTest:expectEqual( '.mp4', qtPath:sub( -4 ) )

	rxTest:expectEqual( assetId:sub( 5 ), qtPath:sub( -32, -5 ) )

	rxTest:expectEqual( 'directory', WFFileUtils.exists( KSPathUtils.parent( qtPath ) ) )

	-- Without File Extension
	
	qtPath = WFCatPixelPathGenerator.pathForProxyTypeAndUuid( pixelsPath, 'panorama', assetId )

	rxTest:log( "qtPath = " .. qtPath )
	
	rxTest:expectEqual( pixelsPath, qtPath:sub( 1, #pixelsPath ) )
	
	rxTest:expectNotEqual( '.mp4', qtPath:sub( -4 ) )

	rxTest:expectEqual( assetId:sub( 5 ), qtPath:sub( -28, -1 ) )

	rxTest:expectEqual( 'directory', WFFileUtils.exists( KSPathUtils.parent( qtPath ) ) )

end

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

function WFCatPixelPathGeneratorTest.rxTestPathForOriginal( rxTest )

	local pixelsPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), 'WFCatPixelPathGeneratorTest' )

	rxTest:finally( function()
		WFFileUtils.delete( pixelsPath )
	end )

	local captureDate = '2013-09-26T14:52:19.544'
	local fileName = 'IMG_0003.jpg'

	local originalPath = WFCatPixelPathGenerator.pathForOriginal( pixelsPath, fileName, captureDate )

	rxTest:log( "originalPath = " .. originalPath )

	rxTest:expectEqual( pixelsPath, originalPath:sub( 1, #pixelsPath ) )

	rxTest:expectEqual( '.jpg', originalPath:sub( -4 ) )

	rxTest:expectEqual( 'IMG_0003', originalPath:sub( -12, -5 ) )
	rxTest:expectEqual( '2013-09-26', originalPath:sub( -23, -14 ) )
	rxTest:expectEqual( '2013', originalPath:sub( -28, -25 ) )

	rxTest:expectEqual( 'directory', WFFileUtils.exists( KSPathUtils.parent( originalPath ) ) )

	KSTestUtils.testFails( function()
		WFCatPixelPathGenerator.pathForOriginal( pixelsPath, fileName, "" )
	end )

	KSTestUtils.testFails( function()
		WFCatPixelPathGenerator.pathForOriginal( pixelsPath, fileName )
	end )

end

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

function WFCatPixelPathGeneratorTest.rxTestPathForXmpSidecar( rxTest )

	local pixelsPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), 'WFCatPixelPathGeneratorTest' )

	rxTest:finally( function()
		WFFileUtils.delete( pixelsPath )
	end )

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	local qtPath = WFCatPixelPathGenerator.pathForXmpSidecar( pixelsPath, assetId )

	rxTest:log( "qtPath = " .. qtPath )

	rxTest:expectEqual( pixelsPath, qtPath:sub( 1, #pixelsPath ) )

	rxTest:expectEqual( '.xmp', qtPath:sub( -4 ) )

	rxTest:expectEqual( assetId:sub( 5 ), qtPath:sub( -32, -5 ) )

	rxTest:expectEqual( 'directory', WFFileUtils.exists( KSPathUtils.parent( qtPath ) ) )

end

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

function WFCatPixelPathGeneratorTest.rxTestPathForPreviewDigestAndLongEdgeJpg( rxTest )

	local WFCrypto = require 'WFCrypto'

	local pixelsPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), 'WFCatPixelPathGeneratorTest' )

	rxTest:finally( function()
		WFFileUtils.delete( pixelsPath )
	end )

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

	for _, longEdge in ipairs{ 2, 160, 320, 640, 1280, 2048, 2^16 - 2, "full" } do

		rxTest:log( "assetId = " .. assetId )
		rxTest:log( "digest = " .. digest )
		rxTest:log( "longEdge = " .. longEdge )

		local qtPath = WFCatPixelPathGenerator.pathForPreviewDigestAndLongEdgeJpg( pixelsPath, assetId, digest, longEdge, false )

		rxTest:log( "qtPath = " .. qtPath )

		rxTest:expectEqual( pixelsPath, qtPath:sub( 1, #pixelsPath ) )

		rxTest:expectEqual( {
			assetId,
			digest,
			longEdge
		}, {
			WFCatPixelPathGenerator.assetIdDigestAndLongEdgeFromPreviewPath( qtPath )
		} )

	end

	for _, badVal in ipairs { "wut", 1, 65536, 7, 1024.1, 0/1, 0/0 } do
		rxTest:expectError(
			WFCatPixelPathGenerator.pathForPreviewDigestAndLongEdge,
			pixelsPath,
			assetId,
			digest,
			badVal )
	end

end

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

function WFCatPixelPathGeneratorTest.rxTestPathForPreviewDigestAndLongEdgeNoExtension( rxTest )

	local WFCrypto = require 'WFCrypto'

	local pixelsPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), 'WFCatPixelPathGeneratorTest' )

	rxTest:finally( function()
		WFFileUtils.delete( pixelsPath )
	end )

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

	for _, longEdge in ipairs{ 2, 160, 320, 640, 1280, 2048, 2^16 - 2, "full" } do

		rxTest:log( "assetId = " .. assetId )
		rxTest:log( "digest = " .. digest )
		rxTest:log( "longEdge = " .. longEdge )

		local qtPath = WFCatPixelPathGenerator.pathForPreviewDigestAndLongEdge( pixelsPath, assetId, digest, longEdge, false )

		rxTest:log( "qtPath = " .. qtPath )

		rxTest:expectEqual( pixelsPath, qtPath:sub( 1, #pixelsPath ) )

		rxTest:expectEqual( {
			assetId,
			digest,
			longEdge
		}, {
			WFCatPixelPathGenerator.assetIdDigestAndLongEdgeFromPreviewPath( qtPath )
		} )

	end

	for _, badVal in ipairs { "wut", 1, 65536, 7, 1024.1, 0/1, 0/0 } do
		rxTest:expectError(
			WFCatPixelPathGenerator.pathForPreviewDigestAndLongEdge,
			pixelsPath,
			assetId,
			digest,
			badVal )
	end

end

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

function WFCatPixelPathGeneratorTest.rxTestConvertHexStringToBase32( rxTest )

	local enc = assert( WFCatPixelPathGenerator.convertHexStringToBase32 )
	local dec = assert( WFCatPixelPathGenerator.convertBase32ToHexString )

	local t = function( base32, hex )
		rxTest:expectEqual( {
			base32 = base32,
			base32_fromUpper = base32,
			hex = hex,
			hex_fromUpper = hex,
		}, {
			base32 = enc(hex, #base32),
			base32_fromUpper = enc(string.upper(hex), #base32),
			hex = dec( base32, #hex ),
			hex_fromUpper = dec( string.upper(base32), #hex )
		} )
	end

	t( 's', 'f' )
	t( 'h9', 'ff' )
	t( 'd99', 'fff' )
	t( '', '' )
	t( 'a', '0' )
	t( 'aa', '00' )
	t( 'aaa', '000' )
	t( 'aaaa', '0000' )
	t( 'aaaa', '00000' )
	t( 'abva', '0640' )
	t( '9', '1f' )
	t( '99', '3ff' )
	t( '999', '7fff' )
	t( 'fmymymymymymymymymymymymym', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' )
	t( 'cejucejucejucejucejucejuce', '44444444444444444444444444444444' )
	t( 'a91mfdde7fkxd5cb48ujx7cfwx', '1fba8a3193a54d07b1075e8a29d11674' )
	t( 'as1362dc9vksh4tecz6bg33rnv', '0fbe79818bf24bcfa81056e04d9cb972' )
	t( 'exqdt46apkj24muaw9njw3wpu5', '9468e1ae01894634a8827f5a2799b23b' )
	t( 'e', '4' )
	t( 'ga', 'c0' )
	t( 'b9a', '7e0' )
	t( 'a647', '735d' )
	t( 'jqgh', '434c7' )
	t( '999', '7fff' )

	rxTest:expectEqual( 'a91mfdde7fkxd5cb48ujx7cfwxejzmers26sjb95gwcgvxna5ztnbb2',
		   enc '1fba8a3193a54d07b1075e8a29d11674'
		.. enc '88b288e7e38f407fb34c4695160dda0b'
		.. enc( string.format( "%03x", 1080 ) ) )

end

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

function WFCatPixelPathGeneratorTest.rxTestAssetIdFolderNames( rxTest )

	local expectedFolders = {}

	for i = 0, 255 do

		expectedFolders[ i + 1 ] = WFCatPixelPathGenerator.convertHexStringToBase32(
								string.format( "%02x", i ) )

	end

	table.sort( expectedFolders )

	local assetIdFolders = WFCatPixelPathGenerator.assetIdRenditionFolderNames()

	table.sort( assetIdFolders )

	rxTest:expectEqual( #assetIdFolders, #expectedFolders )

	rxTest:expectEqual( assetIdFolders, expectedFolders )

end

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

function WFCatPixelPathGeneratorTest.rxTestPathForStyle( rxTest )

	local pixelsPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), 'WFCatPixelPathGeneratorTest' )

	rxTest:finally( function()
		WFFileUtils.delete( pixelsPath )
	end )

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	local stylePath = WFCatPixelPathGenerator.pathForStyle( pixelsPath, assetId )

	rxTest:expectEqual( pixelsPath, stylePath:sub( 1, #pixelsPath ) )

	rxTest:expectEqual( assetId..'.xmp', KSPathUtils.leafName( stylePath ) )

	rxTest:expectEqual( 'directory', WFFileUtils.exists( KSPathUtils.parent( stylePath ) ) )

end

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

function WFCatPixelPathGeneratorTest.rxTestMaybeStylePath( rxTest )

	local pixelsPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), 'WFCatPixelPathGeneratorTest' )

	rxTest:finally( function()
		WFFileUtils.delete( pixelsPath )
	end )

	local assetId = KSIdGenerator.uuidInLowercaseHex()

	local stylePath = WFCatPixelPathGenerator.pathForStyle( pixelsPath, assetId )

	rxTest:expectEqual( true, WFCatPixelPathGenerator.maybeStylePath( pixelsPath, stylePath ) )
	
	local folderPath = KSPathUtils.parent( stylePath )
	
	rxTest:expectEqual( false, WFCatPixelPathGenerator.maybeStylePath( pixelsPath, folderPath ) )
	
	local dummyPath = KSPathUtils.child( folderPath, "dummy.jpg" )
	
	rxTest:expectEqual( false, WFCatPixelPathGenerator.maybeStylePath( pixelsPath, dummyPath ) )

end

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

function WFCatPixelPathGeneratorTest.rxTestPathForDCP( rxTest )

	local pixelsPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), 'WFCatPixelPathGeneratorTest' )

	rxTest:finally( function()
		WFFileUtils.delete( pixelsPath )
	end )

	local fileName = "Canon EOS 5D Agfa Scala 200 - C.dcp"

	local dcpPath = WFCatPixelPathGenerator.pathForDCP( pixelsPath, fileName )
	
	local settingsPath = KSPathUtils.child( pixelsPath, "cr_cameraprofiles" )
	local expectedDCPath = KSPathUtils.child( settingsPath, fileName )

	rxTest:expectEqual( dcpPath, expectedDCPath )

end

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

return WFRxTestCase.wrapSuite( WFCatPixelPathGeneratorTest )
