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

ExportBiggestPossibleRenditionStateMachine.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 all of the states we can go through when processing
-- a loupe rendition request.
--==============================================================================

local function addDocStoreInfoStates( fsm, docstoreInfoStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, docstoreInfoStateGroupName,
			{
				'NeedAssetInfoForExportBiggestPossibleRendition',
				'NeedAssetInfoForExportBiggestPossibleRenditionFromDocstore',
				'FetchingAssetInfoForExportBiggestPossibleRendition',
			} )

end

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

local function addPixelCacheCheckStates( fsm, pixelCacheCheckStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, pixelCacheCheckStateGroupName,
			{
				'NeedCheckForExportBiggestPossibleRenditionInCache',
				'CheckingForExportBiggestPossibleRenditionInCache'
			} )

end

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

local function addSendDataStates( fsm, sendDataStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, sendDataStateGroupName,
			{
				'SendFinalExportBiggestPossibleRenditionPath',
				'SendPermanentErrorForExportBiggestPossibleRenditionDownloadFail',
				'SendPermanentErrorForExportBiggestPossibleRenditionDownloadNoLinks',
			} )

end

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

local function addDownloadStates( fsm, downloadStateGroupName )

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

end

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

local function addSaveRenderStates( fsm, saveRenderStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, saveRenderStateGroupName,
			{
				'NeedToSaveExportBiggestPossibleRenditionDownload',
				'SavingBiggestPossibleRenditionDownload',
			} )

end

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

local function addEndStates( fsm, endStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, endStateGroupName,
			{
				'FinishedSendingBestExportBiggestPossibleRenditionPath',
				'FinishedWithoutSendingBestExportBiggestPossibleRenditionPath',
			} )

end

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

local function addTransitions( fsm, PixelPathProviderStateMachine )

	local DocstoreInfoStates = PixelPathProviderStateMachine.DocstoreInfoStates

	local PixelCacheCheckStates = 
			PixelPathProviderStateMachine.PixelCacheCheckStates

	local DownloadStates = PixelPathProviderStateMachine.DownloadStates

	local SaveRenderStates = PixelPathProviderStateMachine.SaveRenderStates

	local SendDataStates = PixelPathProviderStateMachine.SendDataStates

	local EndStates = PixelPathProviderStateMachine.EndStates

	------------------------------------------------------------
	-- DocstoreInfoStates transitions
	------------------------------------------------------------

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DocstoreInfoStates.NeedAssetInfoForExportBiggestPossibleRendition, 
			{
				{ 'must_fetch', DocstoreInfoStates.NeedAssetInfoForExportBiggestPossibleRenditionFromDocstore },
				{ 'already_present', PixelCacheCheckStates.NeedCheckForExportBiggestPossibleRenditionInCache },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DocstoreInfoStates.NeedAssetInfoForExportBiggestPossibleRenditionFromDocstore,
			{
				{ 'start_docstore_read', DocstoreInfoStates.FetchingAssetInfoForExportBiggestPossibleRendition },
				{ 'received_docstore_read', PixelCacheCheckStates.NeedCheckForExportBiggestPossibleRenditionInCache },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DocstoreInfoStates.FetchingAssetInfoForExportBiggestPossibleRendition,
			{
				{ 'received_docstore_read', PixelCacheCheckStates.NeedCheckForExportBiggestPossibleRenditionInCache },
				{ 'bad_asset_id', SendDataStates.SendPermanentBadAssetIdError }
			} )

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

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			PixelCacheCheckStates.NeedCheckForExportBiggestPossibleRenditionInCache, 
			{
				{ 'start_rendition_check', PixelCacheCheckStates.CheckingForExportBiggestPossibleRenditionInCache },
				{ 'need_docstore_info', DocstoreInfoStates.NeedAssetInfoForExportBiggestPossibleRendition }, 
				{ 'have_rendition', SendDataStates.SendFinalExportBiggestPossibleRenditionPath },
			} )
							  
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			PixelCacheCheckStates.CheckingForExportBiggestPossibleRenditionInCache, 
			{
				{ 'need_docstore_info', DocstoreInfoStates.NeedAssetInfoForExportBiggestPossibleRendition }, 
				{ 'stale_check', PixelCacheCheckStates.CheckingForExportBiggestPossibleRenditionInCache },
				{ 'have_local_rendition', SendDataStates.SendFinalExportBiggestPossibleRenditionPath },
				{ 'no_local_rendition', DownloadStates.TryBiggestPossibleRenditionDownload },
			} )

	------------------------------------------------------------
	-- DownloadStates transitions
	------------------------------------------------------------
	
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DownloadStates.TryBiggestPossibleRenditionDownload, 
			{
				{ 'new_docstore_render_info', DocstoreInfoStates.NeedAssetInfoForExportBiggestPossibleRendition },
				{ 'start_download', DownloadStates.DownloadingBiggestPossibleRenditionLinkForExport },
				{ 'missing_download_info', SendDataStates.SendPermanentErrorForExportBiggestPossibleRenditionDownloadFail },
				{ 'new_long_side', DocstoreInfoStates.NeedAssetInfoForExportBiggestPossibleRendition },
				{ 'cannot_download', SendDataStates.SendPermanentErrorForExportBiggestPossibleRenditionDownloadFail },
				{ 'no_internet', SendDataStates.SendPermanentErrorForExportBiggestPossibleRenditionDownloadFail },
				{ 'volume_cannot_save', SendDataStates.SendPermanentErrorForExportBiggestPossibleRenditionDownloadFail },
				{ 'has_active_rendition_save', SaveRenderStates.SavingBiggestPossibleRenditionDownload }, 
				{ 'has_pending_rendition_save', SaveRenderStates.NeedToSaveExportBiggestPossibleRenditionDownload },
				{ 'start_save_render', SaveRenderStates.SavingBiggestPossibleRenditionDownload }, 
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DownloadStates.DownloadingBiggestPossibleRenditionLinkForExport,
			{
				{ 'retry_download', SendDataStates.SendPermanentErrorForExportBiggestPossibleRenditionDownloadFail },
				{ 'cannot_retry_download', SendDataStates.SendPermanentErrorForExportBiggestPossibleRenditionDownloadFail },
				{ 'missing_download_info', DocstoreInfoStates.NeedAssetInfoForExportBiggestPossibleRendition },
				{ 'received_download', SaveRenderStates.NeedToSaveExportBiggestPossibleRenditionDownload },
				{ 'reschedule_download', DownloadStates.TryBiggestPossibleRenditionDownload },
			} )

	------------------------------------------------------------
	-- SaveRenderStates transitions
	------------------------------------------------------------
	
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			SaveRenderStates.NeedToSaveExportBiggestPossibleRenditionDownload,
			{
				{ 'has_pending_rendition_save', SaveRenderStates.NeedToSaveExportBiggestPossibleRenditionDownload },
				{ 'has_active_rendition_save', SaveRenderStates.SavingBiggestPossibleRenditionDownload }, 
				{ 'start_save_render', SaveRenderStates.SavingBiggestPossibleRenditionDownload }, 
				{ 'no_render_blob', SendDataStates.SendPermanentErrorForExportBiggestPossibleRenditionDownloadFail },
				{ 'no_save_path', SendDataStates.SendPermanentErrorForExportBiggestPossibleRenditionDownloadFail },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			SaveRenderStates.SavingBiggestPossibleRenditionDownload,
			{
				{ 'saved_render', SendDataStates.SendFinalExportBiggestPossibleRenditionPath }, 
				{ 'retry_save_render', SendDataStates.SendPermanentErrorForExportBiggestPossibleRenditionDownloadFail },
			} )

	------------------------------------------------------------
	-- SendDataStates transitions
	------------------------------------------------------------
	
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			SendDataStates.SendFinalExportBiggestPossibleRenditionPath,
			{
				{ 'missing_expected_rendition_path', EndStates.FinishedWithoutSendingBestExportBiggestPossibleRenditionPath },
				{ 'sent_path', EndStates.FinishedSendingBestExportBiggestPossibleRenditionPath }
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			SendDataStates.SendPermanentErrorForExportBiggestPossibleRenditionDownloadNoLinks,
			{ { 'sent', EndStates.FinishedWithoutSendingBestExportBiggestPossibleRenditionPath } } )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			SendDataStates.SendPermanentErrorForExportBiggestPossibleRenditionDownloadFail,
			{ { 'sent', EndStates.FinishedWithoutSendingBestExportBiggestPossibleRenditionPath } } )

	------------------------------------------------------------
	-- EndStates transitions
	------------------------------------------------------------

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			EndStates.FinishedSendingBestExportBiggestPossibleRenditionPath, 
			{
				{ 'renditions_status_change', SendDataStates.SendFinalExportBiggestPossibleRenditionPath },
				{ 'new_docstore_render_info', DocstoreInfoStates.NeedAssetInfoForExportBiggestPossibleRendition },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			EndStates.FinishedWithoutSendingBestExportBiggestPossibleRenditionPath, 
			{
				{ 'renditions_status_change', SendDataStates.SendFinalExportBiggestPossibleRenditionPath },
				{ 'new_docstore_render_info', DocstoreInfoStates.NeedAssetInfoForExportBiggestPossibleRendition },
			} )

	return fsm

end

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

return {

	addDocStoreInfoStates = addDocStoreInfoStates,

	addPixelCacheCheckStates = addPixelCacheCheckStates,

	addSendDataStates = addSendDataStates,

	addDownloadStates = addDownloadStates,

	addSaveRenderStates = addSaveRenderStates,

	addEndStates = addEndStates,

	addTransitions = addTransitions,

}
