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

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 isValidLongSideForMP4Path( longSide )

	if longSide == 'full' or
		longSide == '1080p' or
		longSide == '1080p709' or
		longSide == '720p' or
		longSide == '360p' then

		return true
		
	end

	return false

end

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

local function isVideoSubtype( subtype )
	return subtype == "video"
end

--------------------------------------------------------------------------------
local function getPathForSubtypeAndLongSide( baseBinariesPath, assetId, devSettingsDigest, longSide, subtype )

  local isVideoMP4Path = isVideoSubtype( subtype ) and isValidLongSideForMP4Path( longSide )

  if isVideoMP4Path then

    return WFCatPixelPathGenerator.pathForPreviewDigestAndLongEdgeMP4(  
				baseBinariesPath, assetId, 
				devSettingsDigest, longSide )
                 
  end

  return WFCatPixelPathGenerator.pathForPreviewDigestAndLongEdge( baseBinariesPath, assetId, 
				devSettingsDigest, longSide)

end
--------------------------------------------------------------------------------

local function makePathMaker( baseBinariesPath )

	return function( assetId, devSettingsDigest, longSide, subtype )
		
		local pathMaybeNoExtension = getPathForSubtypeAndLongSide ( baseBinariesPath, assetId, devSettingsDigest, longSide, subtype )

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

		return pathMaybeNoExtension, 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, subtype )

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

	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, _, _, subtype = 
			unpack( pathTuple )

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

	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 )

