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

DownloadInvalidRenderStateMachine.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2020 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 downloading invalid renditions
--==============================================================================

local function addDownloadStates( fsm, downloadStateGroupName )

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

end

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

local function addTransitions( fsm, PixelPathProviderStateMachine )

	local DocstoreInfoStates = PixelPathProviderStateMachine.DocstoreInfoStates

	local PixelCacheCheckStates = 
			PixelPathProviderStateMachine.PixelCacheCheckStates

	local LocalBinaryPathCheckStates =
			PixelPathProviderStateMachine.LocalBinaryPathCheckStates

	local DownloadStates = PixelPathProviderStateMachine.DownloadStates

	local SaveRenderStates = PixelPathProviderStateMachine.SaveRenderStates

	local SendDataStates = PixelPathProviderStateMachine.SendDataStates

	------------------------------------------------------------
	-- PixelCacheCheckStates transitions
	------------------------------------------------------------

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			PixelCacheCheckStates.ChooseRenditionStrategy, 
			{ 
				{ 'try_download_invalid_rendition', DownloadStates.NeedInvalidRenderDownload }, 
			} )

	------------------------------------------------------------
	-- DownloadStates transitions
	------------------------------------------------------------
	
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DownloadStates.NeedInvalidRenderDownload,
			{ 
				{ 'start_download', DownloadStates.DownloadingInvalidRender },
				
				{ 'retry_download', LocalBinaryPathCheckStates.NeedCheckAnyLocalNegativeForRender },
				{ 'cannot_download', PixelCacheCheckStates.ChooseRenderOnlyRenditionStrategy },
				{ 'no_internet', PixelCacheCheckStates.ChooseRenderOnlyRenditionStrategy },
			
				{ 'new_docstore_render_info', DocstoreInfoStates.NeedAssetInfoForRender },
				{ 'new_long_side', DocstoreInfoStates.NeedAssetInfoForRender },
		
				{ 'missing_download_info', SendDataStates.SendPermanentDownloadError },
				{ 'volume_cannot_save', SendDataStates.SendPermanentDownloadError },
	
				{ 'has_pending_rendition_save', SaveRenderStates.NeedToSaveRender },
				{ 'has_active_rendition_save', SaveRenderStates.SavingRender },
				{ 'start_save_render', SaveRenderStates.SavingRender }
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DownloadStates.DownloadingInvalidRender, 
			{ 

				{ 'cannot_retry_download', LocalBinaryPathCheckStates.NeedCheckAnyLocalNegativeForRender },
				{ 'retry_download', LocalBinaryPathCheckStates.NeedCheckAnyLocalNegativeForRender },

				{ 'missing_download_info', DocstoreInfoStates.NeedAssetInfoForRender },

				{ 'received_download', SaveRenderStates.NeedToSaveRender },

				{ 'reschedule_download', DownloadStates.NeedInvalidRenderDownload } 
			} )

	return fsm

end

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

return {

	addDownloadStates = addDownloadStates,

	addTransitions = addTransitions,

}
