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

ExportFullRenditionStateMachine.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2019 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,
			{
				'NeedAssetInfoForExportFullRendition',
				'NeedAssetInfoForExportFullRenditionFromDocstore',
				'FetchingAssetInfoForExportFullRendition',
				'NeedCheckRenderSettingsForExportFullRendition'
			} )

end

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

local function addPixelCacheCheckStates( fsm, pixelCacheCheckStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, pixelCacheCheckStateGroupName,
			{
				'NeedCheckForExportFullRenditionInCache',
				'CheckingForExportFullRenditionInCache'
			} )

end

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

local function addSendDataStates( fsm, sendDataStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, sendDataStateGroupName,
			{
				'SendFinalExportFullRenditionPath',
				'SendPermanentErrorForExportFullRenditionDownloadFail',
				'SendPermanentErrorForExportFullRenditionCreateFail',
				'SendPermanentErrorForExportFullRenditionDownloadNoLinks',
			} )

end

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

local function addDownloadStates( fsm, downloadStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, downloadStateGroupName,
			{
				'TryExistingFullSizeRenditionDownload', 
				'DownloadingExistingFullRenditionLinkForExport',
				'TryJustCreatedFullSizeRenditionDownload', 
				'DownloadingJustCreatedFullRenditionLinkForExport',
				'TryUpdatedFullSizeRenditionDownload', 
				'DownloadingUpdatedFullRenditionLinkForExport',
			} )

end

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

local function addSaveRenderStates( fsm, saveRenderStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, saveRenderStateGroupName,
			{
				'NeedToSaveExportFullRendition',
				'SavingExportFullRendition',
			} )

end

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

local function addEndStates( fsm, endStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, endStateGroupName,
			{
				'FinishedSendingBestExportFullRenditionPath',
				'FinishedWithoutSendingBestExportFullRenditionPath',
				'WaitForFullRenditionLinkUpdate',
			} )

end

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

local _CheckFullRenditionDownloadLinkStatusStateGroupName =
			"CheckFullRenditionDownloadLinkStatusStates"

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

local function addCheckFullRenditionDownloadLinkStatusStates( fsm )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, _CheckFullRenditionDownloadLinkStatusStateGroupName,
			{
				'CheckForAnyFullRenditionLink',
				'CheckForCreateFullRenditionLink',
				'CheckForUpdatedFullRenditionLink',
				'WaitingForFullRenditionLinkCreation',
			} )

end

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

local CustomStateGroupPairs =
	{
		{ _CheckFullRenditionDownloadLinkStatusStateGroupName, addCheckFullRenditionDownloadLinkStatusStates }
	}

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

local function addTransitions( fsm, PixelPathProviderStateMachine )

	local DocstoreInfoStates = PixelPathProviderStateMachine.DocstoreInfoStates

	local PixelCacheCheckStates = 
			PixelPathProviderStateMachine.PixelCacheCheckStates

	local CheckFullRenditionDownloadLinkStatusStates = 
			PixelPathProviderStateMachine.CheckFullRenditionDownloadLinkStatusStates

	local DownloadStates = PixelPathProviderStateMachine.DownloadStates

	local SaveRenderStates = PixelPathProviderStateMachine.SaveRenderStates

	local SendDataStates = PixelPathProviderStateMachine.SendDataStates

	local EndStates = PixelPathProviderStateMachine.EndStates

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

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DocstoreInfoStates.NeedAssetInfoForExportFullRendition, 
			{
				{ 'must_fetch', DocstoreInfoStates.NeedAssetInfoForExportFullRenditionFromDocstore },
				{ 'already_present', DocstoreInfoStates.NeedCheckRenderSettingsForExportFullRendition }
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DocstoreInfoStates.NeedAssetInfoForExportFullRenditionFromDocstore,
			{
				{ 'start_docstore_read', DocstoreInfoStates.FetchingAssetInfoForExportFullRendition },
				{ 'received_docstore_read', DocstoreInfoStates.NeedCheckRenderSettingsForExportFullRendition },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DocstoreInfoStates.FetchingAssetInfoForExportFullRendition,
			{
				{ 'received_docstore_read', DocstoreInfoStates.NeedCheckRenderSettingsForExportFullRendition },
				{ 'bad_asset_id', SendDataStates.SendPermanentBadAssetIdError }
			} )
	
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, DocstoreInfoStates.NeedCheckRenderSettingsForExportFullRendition,
			{
				{ 'have_xmp_develop', PixelCacheCheckStates.NeedCheckForExportFullRenditionInCache },
				{ 'xmp_develop_is_external', PixelCacheCheckStates.NeedCheckForExportFullRenditionInCache },
				{ 'xmp_develop_and_aux_files_are_external', PixelCacheCheckStates.NeedCheckForExportFullRenditionInCache }
			} )

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

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			PixelCacheCheckStates.NeedCheckForExportFullRenditionInCache, 
			{
				{ 'start_rendition_check', PixelCacheCheckStates.CheckingForExportFullRenditionInCache },
				{ 'need_docstore_info', DocstoreInfoStates.NeedAssetInfoForExportFullRendition }, 
				{ 'have_rendition', SendDataStates.SendFinalExportFullRenditionPath },
			} )
							  
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			PixelCacheCheckStates.CheckingForExportFullRenditionInCache, 
			{
				{ 'need_docstore_info', DocstoreInfoStates.NeedAssetInfoForExportFullRendition }, 
				{ 'stale_check', PixelCacheCheckStates.CheckingForExportFullRenditionInCache },
				{ 'have_local_rendition', SendDataStates.SendFinalExportFullRenditionPath },
				{ 'no_local_rendition', CheckFullRenditionDownloadLinkStatusStates.CheckForAnyFullRenditionLink },
			} )


	------------------------------------------------------------
	-- CheckFullRenditionDownloadLinkStatusStates transitions
	------------------------------------------------------------

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			CheckFullRenditionDownloadLinkStatusStates.CheckForAnyFullRenditionLink,
			{
				{ 'need_docstore_info', DocstoreInfoStates.NeedAssetInfoForExportFullRendition }, 
				{ 'have_fullsize_link', DownloadStates.TryExistingFullSizeRenditionDownload },
				{ 'sent_create_fullsize_request', CheckFullRenditionDownloadLinkStatusStates.WaitingForFullRenditionLinkCreation }, 
				{ 'no_fullsize_links', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadNoLinks },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			CheckFullRenditionDownloadLinkStatusStates.CheckForCreateFullRenditionLink,
			{
				{ 'need_docstore_info', DocstoreInfoStates.NeedAssetInfoForExportFullRendition }, 
				{ 'sent_create_fullsize_request', CheckFullRenditionDownloadLinkStatusStates.WaitingForFullRenditionLinkCreation }, 
				{ 'no_fullsize_links', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadNoLinks },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			CheckFullRenditionDownloadLinkStatusStates.CheckForUpdatedFullRenditionLink,
			{
				{ 'need_docstore_info', DocstoreInfoStates.NeedAssetInfoForExportFullRendition }, 
				{ 'have_fullsize_link', DownloadStates.TryUpdatedFullSizeRenditionDownload },
				{ 'no_fullsize_links', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadNoLinks },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			CheckFullRenditionDownloadLinkStatusStates.WaitingForFullRenditionLinkCreation,
			{
				{ 'created_fullsize_rendition', DownloadStates.TryJustCreatedFullSizeRenditionDownload },
				{ 'create_fullsize_request_fail', SendDataStates.SendPermanentErrorForExportFullRenditionCreateFail },
			} )

	------------------------------------------------------------
	-- DownloadStates transitions
	------------------------------------------------------------
	
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DownloadStates.TryExistingFullSizeRenditionDownload, 
			{
				{ 'start_download', DownloadStates.DownloadingExistingFullRenditionLinkForExport },
				{ 'missing_download_info', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
				{ 'new_long_side', DocstoreInfoStates.NeedAssetInfoForExportFullRendition },
				{ 'cannot_download', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
				{ 'no_internet', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
				{ 'volume_cannot_save', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
				{ 'has_pending_rendition_save', SaveRenderStates.NeedToSaveExportFullRendition },
				{ 'has_active_rendition_save', SaveRenderStates.SavingExportFullRendition },
				{ 'start_save_render', SaveRenderStates.SavingExportFullRendition },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DownloadStates.DownloadingExistingFullRenditionLinkForExport,
			{
				{ 'retry_download', CheckFullRenditionDownloadLinkStatusStates.CheckForCreateFullRenditionLink },
				{ 'cannot_retry_download', CheckFullRenditionDownloadLinkStatusStates.CheckForCreateFullRenditionLink },
				{ 'missing_download_info', DocstoreInfoStates.NeedAssetInfoForExportFullRendition },
				{ 'received_download', SaveRenderStates.NeedToSaveExportFullRendition },
				{ 'reschedule_download', DownloadStates.TryExistingFullSizeRenditionDownload },
			} )
	
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DownloadStates.TryJustCreatedFullSizeRenditionDownload, 
			{
				{ 'start_download', DownloadStates.DownloadingJustCreatedFullRenditionLinkForExport },
				{ 'missing_download_info', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
				{ 'new_long_side', DocstoreInfoStates.NeedAssetInfoForExportFullRendition },
				{ 'cannot_download', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
				{ 'no_internet', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
				{ 'volume_cannot_save', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
				{ 'has_pending_rendition_save', SaveRenderStates.NeedToSaveExportFullRendition },
				{ 'has_active_rendition_save', SaveRenderStates.SavingExportFullRendition },
				{ 'start_save_render', SaveRenderStates.SavingExportFullRendition },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DownloadStates.DownloadingJustCreatedFullRenditionLinkForExport,
			{
				{ 'retry_download', EndStates.WaitForFullRenditionLinkUpdate },
				{ 'cannot_retry_download', EndStates.WaitForFullRenditionLinkUpdate },
				{ 'missing_download_info', DocstoreInfoStates.NeedAssetInfoForExportFullRendition },
				{ 'received_download', SaveRenderStates.NeedToSaveExportFullRendition },
				{ 'reschedule_download', DownloadStates.TryJustCreatedFullSizeRenditionDownload },
			} )
	
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DownloadStates.TryUpdatedFullSizeRenditionDownload, 
			{
				{ 'start_download', DownloadStates.DownloadingUpdatedFullRenditionLinkForExport },
				{ 'missing_download_info', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
				{ 'new_long_side', DocstoreInfoStates.NeedAssetInfoForExportFullRendition },
				{ 'cannot_download', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
				{ 'no_internet', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
				{ 'volume_cannot_save', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
				{ 'has_pending_rendition_save', SaveRenderStates.NeedToSaveExportFullRendition },
				{ 'has_active_rendition_save', SaveRenderStates.SavingExportFullRendition },
				{ 'start_save_render', SaveRenderStates.SavingExportFullRendition },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DownloadStates.DownloadingUpdatedFullRenditionLinkForExport,
			{
				{ 'retry_download', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
				{ 'cannot_retry_download', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
				{ 'missing_download_info', DocstoreInfoStates.NeedAssetInfoForExportFullRendition },
				{ 'received_download', SaveRenderStates.NeedToSaveExportFullRendition },
				{ 'reschedule_download', DownloadStates.TryUpdatedFullSizeRenditionDownload },
			} )

	------------------------------------------------------------
	-- SaveRenderStates transitions
	------------------------------------------------------------
	
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			SaveRenderStates.NeedToSaveExportFullRendition,
			{
				{ 'has_pending_rendition_save', SaveRenderStates.NeedToSaveExportFullRendition },
				{ 'start_save_render', SaveRenderStates.SavingExportFullRendition }, 
				{ 'no_render_blob', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
				{ 'no_save_path', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			SaveRenderStates.SavingExportFullRendition,
			{
				{ 'saved_render', SendDataStates.SendFinalExportFullRenditionPath }, 
				{ 'retry_save_render', SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail },
			} )

	------------------------------------------------------------
	-- SendDataStates transitions
	------------------------------------------------------------
	
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			SendDataStates.SendFinalExportFullRenditionPath,
			{
				{ 'missing_expected_rendition_path', EndStates.FinishedWithoutSendingBestExportFullRenditionPath },
				{ 'sent_path', EndStates.FinishedSendingBestExportFullRenditionPath }
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			SendDataStates.SendPermanentErrorForExportFullRenditionDownloadNoLinks,
			{ { 'sent', EndStates.FinishedWithoutSendingBestExportFullRenditionPath } } )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			SendDataStates.SendPermanentErrorForExportFullRenditionDownloadFail,
			{ { 'sent', EndStates.FinishedWithoutSendingBestExportFullRenditionPath } } )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			SendDataStates.SendPermanentErrorForExportFullRenditionCreateFail,
			{ { 'sent', EndStates.FinishedWithoutSendingBestExportFullRenditionPath } } )

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

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			EndStates.FinishedSendingBestExportFullRenditionPath, 
			{
				{ 'renditions_status_change', SendDataStates.SendFinalExportFullRenditionPath },
				{ 'new_docstore_render_info', DocstoreInfoStates.NeedAssetInfoForExportFullRendition },
				{ 'updated_docstore_fullsize_rendition_link', DocstoreInfoStates.NeedAssetInfoForExportFullRendition },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			EndStates.FinishedWithoutSendingBestExportFullRenditionPath, 
			{
				{ 'renditions_status_change', SendDataStates.SendFinalExportFullRenditionPath },
				{ 'new_docstore_render_info', DocstoreInfoStates.NeedAssetInfoForExportFullRendition },
				{ 'updated_docstore_fullsize_rendition_link', DocstoreInfoStates.NeedAssetInfoForExportFullRendition },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			EndStates.WaitForFullRenditionLinkUpdate, 
			{
				{ 'renditions_status_change', SendDataStates.SendFinalExportFullRenditionPath },
				{ 'new_docstore_render_info', DocstoreInfoStates.NeedAssetInfoForExportFullRendition },
				{ 'updated_docstore_fullsize_rendition_link', DownloadStates.TryUpdatedFullSizeRenditionDownload },
			} )

	return fsm

end

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

return {

	addDocStoreInfoStates = addDocStoreInfoStates,

	addPixelCacheCheckStates = addPixelCacheCheckStates,

	addSendDataStates = addSendDataStates,

	addDownloadStates = addDownloadStates,

	addSaveRenderStates = addSaveRenderStates,

	addEndStates = addEndStates,

	CustomStateGroupPairs = CustomStateGroupPairs,

	addTransitions = addTransitions,

}
