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

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',
				'NeedCheckRenderSettingsForXmpDevelopInfo' } )

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

_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',
					'NeedCheckForLocalXmpDevelopForRender',
					'NeedCheckForLocalXmpDevelop',
					'NeedCheckForLocalXmpSidecar' } )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"Binary Disk Check",
			{ 'CheckingForAnyLocalNegativeForRender',
					'CheckingForLocalProxyForRender',
					'CheckingForLocalOriginal',
					'CheckingForLocalProxy',
					'CheckingForAnyLocalNegative',
					'CheckingOriginalForBestPossibleLocalNegativePath',
					'CheckingProxyForBestPossibleLocalNegativePath',
					'CheckingForLocalXmpDevelopForRender',
					'CheckingForLocalXmpDevelop',
					'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',
				'NeedXmpDevelopDownload2',
				'NeedXmpDevelopDownload3',
				'NeedXmpDevelopDownloadForRender',
				'NeedXmpDevelopDownloadForRender2',
				'NeedXmpDevelopDownloadForRender3',
				'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',
				'DownloadingXmpDevelop2',
				'DownloadingXmpDevelop3',
				'DownloadingXmpDevelopForRender',
				'DownloadingXmpDevelopForRender2',
				'DownloadingXmpDevelopForRender3',
				'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',
				'NeedToSaveOriginalForRender2',
				'NeedToSaveProxyForRender',
				'NeedToSaveProxyForRender2',
				'NeedToSaveOriginal',
				'NeedToSaveOriginal2',
				'NeedToSaveProxy',
				'NeedToSaveProxy2',
				'NeedToSaveOriginalForBestPossibleLocalNegativePath',
				'NeedToSaveOriginalForBestPossibleLocalNegativePath2',
				'NeedToSaveProxyForBestPossibleLocalNegativePath',
				'NeedToSaveProxyForBestPossibleLocalNegativePath2',
				'NeedToSaveXmpDevelop',
				'NeedToSaveXmpDevelop2',
				'NeedToSaveXmpDevelopForRender',
				'NeedToSaveXmpDevelopForRender2',
				'NeedToSaveXmpSidecar',
				'NeedToSaveXmpSidecar2',
			} )

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

_stateNameSummarizer = 
	addStateNamesAsCategory( _stateNameSummarizer,
		"SavingBinary",
			{
				'SavingOriginalForRender',
				'SavingOriginalForRender2',
				'SavingProxyForRender',
				'SavingProxyForRender2',
				'SavingOriginal',
				'SavingOriginal2',
				'SavingProxy',
				'SavingProxy2',
				'SavingOriginalForBestPossibleLocalNegativePath',
				'SavingOriginalForBestPossibleLocalNegativePath2',
				'SavingProxyForBestPossibleLocalNegativePath',
				'SavingProxyForBestPossibleLocalNegativePath2',
				'SavingXmpDevelop',
				'SavingXmpDevelop2',
				'SavingXmpDevelopForRender',
				'SavingXmpDevelopForRender2',
				'SavingXmpSidecar',
				'SavingXmpSidecar2' 
			} )

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

_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_EndStates =
		PixelPathProviderStateMachine.EndStates

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

local function getStateCategory( stateName )

	if PixelPathProviderStateMachine_EndStates[ 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,

}
