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

ADOBE CONFIDENTIAL
------------------
Copyright 2021 Adobe
All Rights Reserved.

NOTICE: All information contained herein is, and remains
the property of Adobe and its suppliers, if any. The intellectual
and technical concepts contained herein are proprietary to Adobe
and its suppliers and are protected by all applicable intellectual
property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material
is strictly forbidden unless prior written permission is obtained
from Adobe.

------------------------------------------------------------------------------]]
	
	-- RxLua
local Rx = require 'Rx'
local Observable = Rx.Observable

	-- WF.core.models
local WFModelsUtils = require 'WFModelsUtils'

	-- WF.core.utils
local WFPlatformInfo = require 'WFPlatformInfo'

	-- WF.oz_client.managed_catalog
local WFOzDocUtils = require 'WFOzDocUtils'
local WFCatStuckSyncUtils = require 'WFCatStuckSyncUtils'

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

local LrmSyncInfoModelImp = {}

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

--[[! This model that can be used gather real-time sync diagnostic data.

	Example response structure:
		
	{
	
		metadataSyncDiagnostics = {
		
			catalogPullerSyncDiagnostics = { < diagnostic data > },
			
			catalogPusherSyncDiagnostics = { < diagnostic data > },
			
			spaceFeedsManagerSyncDiagnostics = {
			
				spacePusherSyncDiagnostics = { < diagnostic data > },
				
				feedOfFeedsPullerSyncDiagnostics = { < diagnostic data > },
				
				spacePullersSyncDiagnostics = { 
				
					{ < space 1 diagnostic data > },
					
					{ < space 2 diagnostic data > },
					
					...
				}
				
			}
			
		},
		
		binaryUploaderSyncDiagnostics = { < diagnostic data > }
		
	}
	
	For more information on the diagnostic data, please see the syncDiagnostics
	functions with the associated sync classes.

--]]

function LrmSyncInfoModelImp:syncDiagnostics( optCalm )

	local syncClient = self.catalog:getSyncClient()

	return {
		value = syncClient:syncDiagnostics( optCalm )
	}

end

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

-- Get a stream that returns asset document IDs that the system believes are in
-- a stuck sync state due to non-uploaded metadata.

function LrmSyncInfoModelImp:getDocsStuckSyncingMetadataStream( optCalm, optStuckReportingTimeThreshold )

	local syncClient = self.catalog:getSyncClient()

	local isNormalSyncRunningStream = syncClient:isNormalSyncRunningStream()

	return WFModelsUtils.createModelForFunction( self._wfCatalogStream, 
			WFCatStuckSyncUtils.getDocsStuckSyncingMetadataStream, isNormalSyncRunningStream, optCalm, optStuckReportingTimeThreshold )

end

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

-- Get a stream that returns asset document IDs that the system believes are in
-- a stuck sync state due to an non-uploaded original.

function LrmSyncInfoModelImp:getDocsStuckUploadingOriginalStream( optCalm, optStuckReportingTimeThreshold )

	local syncClient = self.catalog:getSyncClient()

	local isNormalSyncRunningStream = syncClient:isNormalSyncRunningStream()

	return WFModelsUtils.createModelForFunction( self._wfCatalogStream, 
			WFCatStuckSyncUtils.getDocsStuckUploadingOriginalStream, isNormalSyncRunningStream, optCalm, optStuckReportingTimeThreshold )

end

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

-- The following function resolves to `true` if all of the assets
-- are fully synced with Oz, and there are no pending local changes
-- that have yet to sync. Otherwise, it resolves to false.

function LrmSyncInfoModelImp:areAssetsFullySyncedWithOz( assetIds )

	return WFModelsUtils.createModelForFunction( self._wfCatalogStream,
			WFOzDocUtils.areAssetsFullySyncedWithOz, assetIds )

end

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

-- The following function resolves to `true` once all of the assets
-- are fully synced with Oz.

function LrmSyncInfoModelImp:waitUntilAssetsAreFullySyncedWithOz( assetIds )

	return WFModelsUtils.createModelForFunction( self._wfCatalogStream,
			WFOzDocUtils.waitUntilAssetsAreFullySyncedWithOz, assetIds )

end

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

-- Returns a model that fires with the current WF Framework version number.

function LrmSyncInfoModelImp:getWFFrameworkVersion()

	local frameworkVersion = WFPlatformInfo.getFrameworkVersion()

	return {
		value = Observable.just( frameworkVersion )
	}

end

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

return LrmSyncInfoModelImp
