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

OriginalOrFallbackProxyLocalNegativeStateMachine.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2025 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
local WFFiniteStateMachine = require 'WFFiniteStateMachine'

--==============================================================================
-- This file encapsulates the process of getting the original path or falling back
-- to proxy path when original is not available
--==============================================================================

local function addDocStoreInfoStates( fsm, docStoreStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, docStoreStateGroupName,
			{
				'NeedOriginalOrFallbackProxyLocalNegativePathInfo',
				'NeedOriginalOrFallbackProxyLocalNegativePathInfoFromDocstore',
				'FetchingOriginalOrFallbackProxyLocalNegativePathInfo'
			} )

end

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

local function addLocalBinaryPathCheckStates( fsm, localBinaryPathCheckStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, localBinaryPathCheckStateGroupName,
			{
				'NeedCheckOriginalOrFallbackProxyLocalNegative',
				'CheckingOriginalOrFallbackProxyLocalNegative'
			} )

end

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

local function addDownloadStates( fsm, downloadStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, downloadStateGroupName,
			{
				'DownloadOriginalWithProxyFallback',
			} )

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

local function addTransitions( fsm, PixelPathProviderStateMachine )

	local DocstoreInfoStates = PixelPathProviderStateMachine.DocstoreInfoStates
	local DownloadStates = PixelPathProviderStateMachine.DownloadStates
	local LocalBinaryPathCheckStates = PixelPathProviderStateMachine.LocalBinaryPathCheckStates
	local SendDataStates = PixelPathProviderStateMachine.SendDataStates
	local DownloadToFileStates = PixelPathProviderStateMachine.DownloadToFileStates

	------------------------------------------------------------
	-- DocstoreInfoStates transitions
	------------------------------------------------------------
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DocstoreInfoStates.NeedOriginalOrFallbackProxyLocalNegativePathInfo, 
			{ 
				{ 'must_fetch', DocstoreInfoStates.NeedOriginalOrFallbackProxyLocalNegativePathInfoFromDocstore },
				{ 'already_present', LocalBinaryPathCheckStates.NeedCheckOriginalOrFallbackProxyLocalNegative },
				{ 'bad_asset_id', SendDataStates.SendPermanentBadAssetIdError }
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DocstoreInfoStates.NeedOriginalOrFallbackProxyLocalNegativePathInfoFromDocstore, 
			{ 
				{ 'start_docstore_read', DocstoreInfoStates.FetchingOriginalOrFallbackProxyLocalNegativePathInfo },
				{ 'received_docstore_read', LocalBinaryPathCheckStates.NeedCheckOriginalOrFallbackProxyLocalNegative }
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DocstoreInfoStates.FetchingOriginalOrFallbackProxyLocalNegativePathInfo, 
			{ 
				{ 'received_docstore_read', LocalBinaryPathCheckStates.NeedCheckOriginalOrFallbackProxyLocalNegative },
				{ 'bad_asset_id', SendDataStates.SendPermanentBadAssetIdError }
			} )

	------------------------------------------------------------
	-- LocalBinaryPathCheckStates transitions
	------------------------------------------------------------
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			LocalBinaryPathCheckStates.NeedCheckOriginalOrFallbackProxyLocalNegative, 
			{ 
				{ 'start_non_rendition_check', LocalBinaryPathCheckStates.CheckingOriginalOrFallbackProxyLocalNegative },
				{ 'no_downloadable_orig_uri', PixelPathProviderStateMachine.StartStates.NeedAnyLocalNegativePathInfo },
				{ 'no_downloadable_uri', SendDataStates.SendNoDownloadableBinary },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			LocalBinaryPathCheckStates.CheckingOriginalOrFallbackProxyLocalNegative, 
			{ 
				{ 'has_local', SendDataStates.HaveLocalNegativePath },
				{ 'need_download_original', DownloadStates.DownloadOriginalWithProxyFallback },
				{ 'no_downloadable_orig_uri', PixelPathProviderStateMachine.StartStates.NeedAnyLocalNegativePathInfo },
				{ 'no_downloadable_uri', SendDataStates.SendNoDownloadableBinary }
			} )
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DownloadStates.DownloadOriginalWithProxyFallback, 
			{ 
				-- In this state transition, we only want to try to get the original once, if that fails for any reason,
				-- just go straight into an attempt to get any local negative path (local or downloadable proxy).
				-- The purpose of this FSM is to get the best possible negative either through downloading or local at the time.
				-- In the case of no_internet, do not attempt to wait for the connection to be restored.
				
				-- errors downloading original, switch to proxy
				{ 'cannot_download', PixelPathProviderStateMachine.StartStates.NeedAnyLocalNegativePathInfo },
				{ 'missing_download_info',  PixelPathProviderStateMachine.StartStates.NeedAnyLocalNegativePathInfo },
				{ 'no_internet',  PixelPathProviderStateMachine.StartStates.NeedAnyLocalNegativePathInfo },
				{ 'volume_cannot_save',  PixelPathProviderStateMachine.StartStates.NeedAnyLocalNegativePathInfo },

				-- will download original
				{ 'start_original_download_to_file', DownloadToFileStates.DownloadingOriginalToFile },
			} )

	return fsm

end

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

return {

	addDocStoreInfoStates = addDocStoreInfoStates,
	addLocalBinaryPathCheckStates = addLocalBinaryPathCheckStates,
	addDownloadStates = addDownloadStates,
	addTransitions = addTransitions,

} 
