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

WFCatPixelFileCacheCheckImp.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.

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

	-- Rx
local Rx = require 'Rx'

	-- WF.core.lua.threading
local WFRequestDispatcher = require 'WFRequestDispatcher'

	-- WF.core.os
local WFFileUtils = require "WFFileUtils"

	-- WF.core.utils
local WFArguments = require 'WFArguments'
local WFCloseSupport = require 'WFCloseSupport'
local WFFunctionUtils = require "WFFunctionUtils"

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

	-- shortcuts
local checkArgs = assert( WFArguments.check, "WFArguments.check not defined" )
local WFFileUtils_exists = assert( WFFileUtils.exists,
									"WFFileUtils.exists not defined" )
local WFFunctionUtils_map = assert( WFFunctionUtils.map,
									"WFFunctionUtils.map not defined" )

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

local WFCatPixelFileCacheCheckImp = {

	_pathMaker = false,
		-- ( function ) generated via WFCatFileSpec at init

	WFCloseSupport.addCloseSupportMethods,

}

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

local function makePathMaker( baseBinariesPath )

	return function( assetId, devSettingsDigest, longSide )

		local pathNoExtension = WFCatPixelPathGenerator.pathForPreviewDigestAndLongEdge( 
				baseBinariesPath, assetId, 
				devSettingsDigest, longSide )

		local pathJpgExtension = WFCatPixelPathGenerator.pathForPreviewDigestAndLongEdgeJpg( 
				baseBinariesPath, assetId, 
				devSettingsDigest, longSide )

		return pathNoExtension, pathJpgExtension

	end

end

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

--[[!
	Create a class for bulk checking if an assetId is in the cache
--]]

function WFCatPixelFileCacheCheckImp:__init( baseBinariesPath )

	checkArgs( 'WFCatPixelFileCacheCheckImp', '+string' )

	self._pathMaker = makePathMaker( baseBinariesPath )

	WFCloseSupport.mustCloseAtThreadTermination( self )

end

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

local function existsAsFile( path )

	local fileExists = WFFileUtils_exists( path ) == "file"
	local fileIsOnOfflineVolume = false
	
	if not fileExists then
		fileIsOnOfflineVolume = WFFileUtils.isPathOnOfflineVolume( path )
	end

	return fileExists, fileIsOnOfflineVolume

end

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

local function getPathExistsData( longSide, pathMaker, assetId, settingsDigest )

	local pathNoExtension, pathWithJpgExtension = pathMaker( assetId, settingsDigest, longSide )

	local pathNoExtensionExists = existsAsFile( pathNoExtension )
		-- Check if the extension-less path exists.
		
	local existsData = { pathNoExtensionExists, pathNoExtension, longSide }

	if not pathNoExtensionExists then
	
		local pathWithJpgExtensionExists = existsAsFile( pathWithJpgExtension )
			-- If the extension-less path doesn't exist, check to see if
			-- a .jpg version exists.
			
		if pathWithJpgExtensionExists then
		
			existsData = { pathWithJpgExtensionExists, pathWithJpgExtension, longSide }
				-- Only overwrite the result if the .jpg exists.
				
		end
			
	end

	return existsData

end

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

local function checkPathTuple( pathTuple, pathMaker )

	local pixStreamId, localAssetId, assetId, settingsDigest, longSides = 
			unpack( pathTuple )

	local pathExistsDataArray = 
			WFFunctionUtils_map( getPathExistsData, longSides, pathMaker,
									assetId, settingsDigest )

	return { pathExistsDataArray, pixStreamId, localAssetId, assetId, 
				settingsDigest, unpack( pathTuple, 6 ) }

end

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

--[[!
	\brief Check if each of the paths represented by the tuple exist

	\param ( table ) an array of { opaqueId, assetId, settingsDigest, longSide } tuples

	\return (table) an array of { bool, opaqueId, assetId, settingsDigest, longSide } tuples
--]]

function WFCatPixelFileCacheCheckImp:checkTuples( cachePathTuples )

	checkArgs( 'WFCatPixelFileCacheCheckImp', 'table' )

	return WFFunctionUtils_map( checkPathTuple, cachePathTuples,
									self._pathMaker )

end

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

--[[!
	\brief Check if the path exists

	\param (string) A file path.

	\return (bool) Indicates if the file exists.
	
--]]

WFCatPixelFileCacheCheckImp.checkPath = function( _, path )

	checkArgs( 'WFCatPixelFileCacheCheckImp', 'string' )

	return existsAsFile( path )
	
end

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

local function initMessageProcessor( WFCatPixelFileCacheCheckImpClass,
										ozCatalogId )

	local imp = WFCatPixelFileCacheCheckImpClass( ozCatalogId )

	WFRequestDispatcher.onMessageDo {
		message = 'checkTuples',
		target = imp,
	}

	WFRequestDispatcher.onMessageDo {
		message = 'checkPath',
		target = imp,
	}

end

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

WFCatPixelFileCacheCheckImp.class = {

	initMessageProcessor = initMessageProcessor

}

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

return Rx.Class( 'WFCatPixelFileCacheCheckImp', WFCatPixelFileCacheCheckImp )

