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

LoupeRenditionStateMachine.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,
			{ 
				'NeedAssetInfoForLoupeRendition',
				'NeedAssetInfoForLoupeRenditionFromDocstore',
				'FetchingAssetInfoForLoupeRendition',
				'NeedCheckRenderSettingsForLoupeRendition'
			} )

end

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

local function addPixelCacheCheckStates( fsm, pixelCacheCheckStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, pixelCacheCheckStateGroupName,
			{
				'NeedCheckForLoupeRenditionInCache',
				'CheckingForLoupeRenditionInCache'
			} )

end

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

local function addSendDataStates( fsm, sendDataStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, sendDataStateGroupName,
			{
				'MaybeSendLoupeRenditionPathFromInMemoryCache',
				'SendFinalLoupeRenditionPath',
			} )

end

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

local function addDownloadStates( fsm, downloadStateGroupName )

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

end

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

local function addSaveRenderStates( fsm, saveRenderStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, saveRenderStateGroupName,
			{
				'NeedToSaveLoupeRendition',
				'SavingLoupeRendition',
			} )

end

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

local function addEndStates( fsm, endStateGroupName )

	return WFFiniteStateMachine.addGroupedStates( 
			fsm, endStateGroupName,
			{
				'FinishedSendingBestLoupeRenditionPath',
				'FinishedWithoutSendingBestLoupeRenditionPath' 
			} )

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

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DocstoreInfoStates.NeedAssetInfoForLoupeRendition, 
			{ 
				{ 'must_fetch', DocstoreInfoStates.NeedAssetInfoForLoupeRenditionFromDocstore },
				{ 'already_present', DocstoreInfoStates.NeedCheckRenderSettingsForLoupeRendition } 
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DocstoreInfoStates.NeedAssetInfoForLoupeRenditionFromDocstore,
			{
				{ 'start_docstore_read', DocstoreInfoStates.FetchingAssetInfoForLoupeRendition },
				{ 'received_docstore_read', DocstoreInfoStates.NeedCheckRenderSettingsForLoupeRendition },
				{ 'new_docstore_render_info', DocstoreInfoStates.NeedCheckRenderSettingsForLoupeRendition } 
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DocstoreInfoStates.FetchingAssetInfoForLoupeRendition,
			{ 
				{ 'received_docstore_read', DocstoreInfoStates.NeedCheckRenderSettingsForLoupeRendition },
				{ 'new_docstore_render_info', DocstoreInfoStates.NeedCheckRenderSettingsForLoupeRendition },
				{ 'bad_asset_id', SendDataStates.SendPermanentBadAssetIdError } 
			} )
	
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, DocstoreInfoStates.NeedCheckRenderSettingsForLoupeRendition,
			{ 
				{ 'have_xmp_develop', PixelCacheCheckStates.NeedCheckForLoupeRenditionInCache },
				{ 'xmp_develop_is_external', PixelCacheCheckStates.NeedCheckForLoupeRenditionInCache } 
			} )

	-- pixel cache check task steps

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			PixelCacheCheckStates.NeedCheckForLoupeRenditionInCache, 
			{ 
				{ 'start_rendition_check', PixelCacheCheckStates.CheckingForLoupeRenditionInCache },
				{ 'need_docstore_info', DocstoreInfoStates.NeedAssetInfoForLoupeRendition }, 
				{ 'have_rendition', SendDataStates.SendFinalLoupeRenditionPath },
			} )
							  
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			PixelCacheCheckStates.CheckingForLoupeRenditionInCache, 
			{ 
				{ 'need_docstore_info', DocstoreInfoStates.NeedAssetInfoForLoupeRendition }, 
				{ 'stale_check', PixelCacheCheckStates.CheckingForLoupeRenditionInCache },
				{ 'have_local_rendition', SendDataStates.SendFinalLoupeRenditionPath },
				{ 'no_local_rendition', DownloadStates.NeedLoupeRenditionDownload },
			} )

	-- download loupe rendition steps
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DownloadStates.NeedLoupeRenditionDownload, 
			{ 
				{ 'start_download', DownloadStates.DownloadingLoupeRendition },
				{ 'new_docstore_render_info', DocstoreInfoStates.NeedAssetInfoForLoupeRendition },
				{ 'missing_download_info', SendDataStates.SendPermanentDownloadError },
				{ 'new_long_side', DocstoreInfoStates.NeedAssetInfoForLoupeRendition },
				{ 'cannot_download', SendDataStates.SendPermanentDownloadError },
				{ 'no_internet', SendDataStates.SendPermanentDownloadError },
				{ 'volume_cannot_save', SendDataStates.SendPermanentDownloadError },
				{ 'has_pending_rendition_save', SaveRenderStates.NeedToSaveLoupeRendition },
				{ 'has_active_rendition_save', SaveRenderStates.SavingLoupeRendition },
				{ 'start_save_render', SaveRenderStates.SavingLoupeRendition },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			DownloadStates.DownloadingLoupeRendition,
			{
				{ 'retry_download', SendDataStates.SendPermanentDownloadError },
				{ 'cannot_retry_download', SendDataStates.SendPermanentDownloadError },
				{ 'missing_download_info', DocstoreInfoStates.NeedAssetInfoForLoupeRendition },
				{ 'received_download', SaveRenderStates.NeedToSaveLoupeRendition },
				{ 'reschedule_download', DownloadStates.NeedLoupeRenditionDownload },
			} )

	------------------------------------------------------------
	-- SaveRenderStates
	------------------------------------------------------------
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			SaveRenderStates.NeedToSaveLoupeRendition,
			{
				{ 'has_pending_rendition_save', SaveRenderStates.NeedToSaveLoupeRendition },
				{ 'start_save_render', SaveRenderStates.SavingLoupeRendition }, 
				{ 'no_render_blob', SendDataStates.SendPermanentDownloadError },
				{ 'no_save_path', SendDataStates.SendPermanentDownloadError },
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			SaveRenderStates.SavingLoupeRendition,
			{
				{ 'saved_render', SendDataStates.SendFinalLoupeRenditionPath }, 
				{ 'retry_save_render', SendDataStates.SendPermanentDownloadError },
			} )

	-- send data task steps
	
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			SendDataStates.MaybeSendLoupeRenditionPathFromInMemoryCache,
			{ 
				{ 'sent_path_need_further_check', DocstoreInfoStates.NeedAssetInfoForLoupeRendition },
				{ 'no_suitable_rendition_need_further_check', DocstoreInfoStates.NeedAssetInfoForLoupeRendition },
				{ 'sent_path', EndStates.FinishedSendingBestLoupeRenditionPath } 
			} )
	
	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			SendDataStates.SendFinalLoupeRenditionPath,
			{ 
				{ 'missing_expected_rendition_path', EndStates.FinishedWithoutSendingBestLoupeRenditionPath },
				{ 'sent_path', EndStates.FinishedSendingBestLoupeRenditionPath } 
			} )

	-- final states

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			EndStates.FinishedSendingBestLoupeRenditionPath, 
			{ 
				{ 'new_downloadable_links_for_rendition_only_asset', DocstoreInfoStates.NeedAssetInfoForLoupeRendition } 
			} )

	fsm = WFFiniteStateMachine.addTransitions( 
			fsm, 
			EndStates.FinishedWithoutSendingBestLoupeRenditionPath, 
			{ 
				{ 'new_downloadable_links_for_rendition_only_asset', DocstoreInfoStates.NeedAssetInfoForLoupeRendition } 
			} )

	return fsm

end

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

return {

	addDocStoreInfoStates = addDocStoreInfoStates,

	addPixelCacheCheckStates = addPixelCacheCheckStates,

	addSendDataStates = addSendDataStates,

	addDownloadStates = addDownloadStates,

	addSaveRenderStates = addSaveRenderStates,

	addEndStates = addEndStates,

	addTransitions = addTransitions,

}
