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

PixelPathProviderSchedulerDebugUtils.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2017 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.

------------------------------------------------------------------------------]]

local PixelPathProviderStateMachine = require "PixelPathProviderStateMachine"

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

local function addStateNamesAsCategory( stateNameSummarizer, category, stateNames )

	stateNameSummarizer.categoryStateNames[ category ] = stateNames

	for _, stateName in ipairs( stateNames ) do

		stateNameSummarizer.stateToCategory[ stateName ] = category
		
	end

	return stateNameSummarizer

end

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

local _stateNameSummarizer = { stateToCategory = {}, categoryStateNames = {} }

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"Initial wait",
			{ 'NeedAssetInfoForRender',
				'NeedAssetInfoForLoupeRendition',
				'NeedAssetInfoForExportFullRendition',
				'NeedAssetInfoForExportBiggestPossibleRendition',
				'NeedDownloadOriginalInfo',
				'NeedDownloadProxyInfo',
				'NeedBestPossibleLocalNegativePathInfo',
				'NeedAnyLocalNegativePathInfo',
				'NeedXmpDevelopInfo',
				'NeedXmpSidecarInfo' } )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"Schedule Docstore",
			{ 'NeedAnyLocalNegativePathInfoFromDocstore',
				'NeedDownloadOriginalInfoFromDocstore',
				'NeedAssetInfoForRenderFromDocstore',
				'NeedAssetInfoForLoupeRenditionFromDocstore',
				'NeedAssetInfoForExportFullRenditionFromDocstore',
				'NeedAssetInfoForExportBiggestPossibleRenditionFromDocstore',
				'NeedDownloadProxyInfoFromDocstore',
				'NeedBestPossibleLocalNegativePathInfoFromDocstore',
				'NeedXmpDevelopInfoFromDocstore',
				'NeedXmpSidecarInfoFromDocstore' } )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"Check Develop XMP",
			{ 'NeedCheckRenderSettings',
				'NeedCheckRenderSettingsForLoupeRendition',
				'NeedCheckRenderSettingsForExportFullRendition',
				'NeedCheckIfXmpDevelopInlineOrExternal' } )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"Fetch From Docstore",
			{ 'FetchingAssetInfoForRender',
					'FetchingAssetInfoForLoupeRendition',
					'FetchingAssetInfoForExportFullRendition',
					'FetchingAssetInfoForExportBiggestPossibleRendition',
					'FetchingDownloadOriginalInfo',
					'FetchingDownloadProxyInfo',
					'FetchingBestPossibleLocalNegativePathInfo',
					'FetchingAnyLocalNegativePathInfo',
					'FetchingXmpDevelopInfo',
					'FetchingXmpSidecarInfo' } )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"Wait Binary Disk Check",
			{ 'NeedCheckAnyLocalNegativeForRender',
					'NeedCheckLocalProxyForRender',
					'NeedCheckForLocalOriginal',
					'NeedCheckForLocalProxy',
					'NeedCheckForAnyLocalNegative',
					'NeedCheckOriginalForBestPossibleLocalNegativePath',
					'NeedCheckProxyForBestPossibleLocalNegativePath',
					'NeedCheckIfExternalXmpDevelopIsLocal',
					'RenditionStrategyNeedCheckIfExternalXmpDevelopIsLocal',
					'NeedCheckForLocalXmpSidecar' } )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"Binary Disk Check",
			{ 'CheckingForAnyLocalNegativeForRender',
					'CheckingForLocalProxyForRender',
					'CheckingForLocalOriginal',
					'CheckingForLocalProxy',
					'CheckingForAnyLocalNegative',
					'CheckingOriginalForBestPossibleLocalNegativePath',
					'CheckingProxyForBestPossibleLocalNegativePath',
					'RenditionStrategyCheckingIfExternalXmpDevelopIsLocal',
					'CheckingIfExternalXmpDevelopIsLocal',
					'CheckingForLocalXmpSidecar' } )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"Wait Rendition Disk Check",
			{ 'NeedCheckForPixelPathInCache',
					'NeedCheckForLoupeRenditionInCache',
					'NeedCheckForExportFullRenditionInCache',
					'NeedCheckForExportBiggestPossibleRenditionInCache' } )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"Rendition Disk Check",
			{ 'CheckingForPixelPathInCache',
					'CheckingForLoupeRenditionInCache',
					'CheckingForExportFullRenditionInCache',
					'CheckingForExportBiggestPossibleRenditionInCache' } )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"Wait Blob Download",
			{ 
				'NeedRenderDownload', 
				'NeedRenderDownload2', 
				'NeedRenderDownload3', 
				'NeedProxyDownload', 
				'NeedProxyDownload2', 
				'NeedProxyDownload3', 
				'NeedProxyDownloadForRender', 
				'NeedProxyDownloadForRender2', 
				'NeedProxyDownloadForRender3', 
				'NeedOriginalDownload', 
				'NeedOriginalDownload2', 
				'NeedOriginalDownload3', 
				'NeedOriginalDownloadForRender', 
				'NeedOriginalDownloadForRender2', 
				'NeedOriginalDownloadForRender3', 
				'NeedXmpDevelopDownload',
				'RenderNeedXmpDevelopDownload',
				'NeedXmpSidecarDownload',
				'NeedXmpSidecarDownload2',
				'NeedXmpSidecarDownload3',
				'NeedOriginalDownloadForBestPossibleLocalNegativePath', 
				'NeedOriginalDownloadForBestPossibleLocalNegativePath2', 
				'NeedOriginalDownloadForBestPossibleLocalNegativePath3', 
				'NeedProxyDownloadForBestPossibleLocalNegativePath',
				'NeedProxyDownloadForBestPossibleLocalNegativePath2', 
				'NeedProxyDownloadForBestPossibleLocalNegativePath3',
				'NeedLoupeRenditionDownload', 
				'TryExistingFullSizeRenditionDownload', 
				'TryJustCreatedFullSizeRenditionDownload', 
				'TryUpdatedFullSizeRenditionDownload', 
				'TryBiggestPossibleRenditionDownload', 
				'NeedInvalidRenderDownload',
			} )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"Download Blob",
			{ 
				'DownloadingRender',
				'DownloadingRender2',
				'DownloadingRender3',
				'DownloadingLoupeRendition',
				'DownloadingExistingFullRenditionLinkForExport',
				'DownloadingJustCreatedFullRenditionLinkForExport',
				'DownloadingUpdatedFullRenditionLinkForExport',
				'DownloadingBiggestPossibleRenditionLinkForExport',
				'DownloadingProxy',
				'DownloadingProxy2',
				'DownloadingProxy3',
				'DownloadingProxyForRender',
				'DownloadingProxyForRender2',
				'DownloadingProxyForRender3',
				'DownloadingOriginal',
				'DownloadingOriginal2',
				'DownloadingOriginal3',
				'DownloadingOriginalForRender',
				'DownloadingOriginalForRender2',
				'DownloadingOriginalForRender3',
				'DownloadingXmpDevelop',
				'RenderDownloadingXmpDevelop',
				'DownloadingXmpSidecar',
				'DownloadingXmpSidecar2',
				'DownloadingXmpSidecar3',
				'DownloadingOriginalForBestPossibleLocalNegativePath', 
				'DownloadingOriginalForBestPossibleLocalNegativePath2', 
				'DownloadingOriginalForBestPossibleLocalNegativePath3', 
				'DownloadingProxyForBestPossibleLocalNegativePath',
				'DownloadingProxyForBestPossibleLocalNegativePath2', 
				'DownloadingProxyForBestPossibleLocalNegativePath3',
				'DownloadingInvalidRender',
			} )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"Wait Render", 
			{ 'NeedsRender', 'NeedsRender2' } )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"Render",
			{ 'RenderScheduled', 'RenderScheduled2' } )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"WaitForSaveBinary",
			{
				'NeedToSaveOriginalForRender',
				'NeedToSaveProxyForRender',
				'NeedToSaveOriginal',
				'NeedToSaveProxy',
				'NeedToSaveOriginalForBestPossibleLocalNegativePath',
				'NeedToSaveProxyForBestPossibleLocalNegativePath',
				'NeedToSaveXmpDevelop',
				'RenderNeedToSaveXmpDevelop',
				'NeedToSaveXmpSidecar',
			} )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"SavingBinary",
			{
				'SavingOriginalForRender',
				'SavingProxyForRender',
				'SavingOriginal',
				'SavingProxy',
				'SavingOriginalForBestPossibleLocalNegativePath',
				'SavingProxyForBestPossibleLocalNegativePath',
				'SavingXmpDevelop',
				'RenderSavingXmpDevelop',
				'SavingXmpSidecar',
			} )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"WaitForSaveRendition",
			{ 'NeedToSaveRender', 'NeedToSaveRender2' } )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"SavingRendition",
			{ 'SavingRender', 'SavingRender2' } )

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

local function extractKeys( t )

	local keys = {}

	for k in pairs( t ) do

		keys[ #keys + 1 ] = k

	end

	return keys

end

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"Wait Send Data",
			extractKeys( PixelPathProviderStateMachine.SendDataStates ) )

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

local PixelPathProviderStateMachine_finishStates

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

local function getStateCategory( stateName )

	PixelPathProviderStateMachine_finishStates =
		PixelPathProviderStateMachine_finishStates or
		PixelPathProviderStateMachine.getFinishStates()

	if PixelPathProviderStateMachine_finishStates[ stateName ] then

		-- ignore end states, we expect to spend most of the time in them.
		return

	end

	return _stateNameSummarizer.stateToCategory[ stateName ] or 
			stateName

end

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

local function getCategoryStateNames( category, stateNameMap )

	return _stateNameSummarizer.categoryStateNames[ category ] or 
			( stateNameMap[ category ] and { category } ) or
			{}

end

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

return {

	getStateCategory = getStateCategory,

	getCategoryStateNames = getCategoryStateNames,

}
