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

PixelProviderPixelPathsModel.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2016 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remains 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 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 Systems Incorporated.

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

local Rx = require "Rx"

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

local logger = require "WFLogger"( "PixelProviderPixelPathsModel" )

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

local function makeWeakRef( v )

	local weakRef = require "KSTableUtils".makeWithMode( "kv" )

	weakRef[ v ] = v

	return weakRef

end

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

local function derefWeakRef( weakRef )

	return next( weakRef )

end

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

local function checkForDroppedPaths( weakPixelProviderProcessor, bufferedPaths ) 

	local pixelProviderProcessor = derefWeakRef( weakPixelProviderProcessor )

	if not pixelProviderProcessor then

		return bufferedPaths

	end

	rawset( pixelProviderProcessor, 
			"_DEBUG_daemonBufferedPathsCallClient",
			( rawget( pixelProviderProcessor, 
						"_DEBUG_daemonBufferedPathsCallClient" ) or 0 ) + 1 )

	rawset( pixelProviderProcessor, 
			"_DEBUG_daemonBufferedPathsSentClient",
			( rawget( pixelProviderProcessor, 
						"_DEBUG_daemonBufferedPathsSentClient" ) or 0 ) +
						#bufferedPaths )

	bufferedPaths._DEBUG_daemonBufferedPathsCallClient =
					rawget( pixelProviderProcessor,
							"_DEBUG_daemonBufferedPathsCallClient" )

	bufferedPaths._DEBUG_daemonBufferedPathsSentClient =
					rawget( pixelProviderProcessor,
							"_DEBUG_daemonBufferedPathsSentClient" )

	if bufferedPaths._DEBUG_daemonBufferedPathsCallClient ~=
			bufferedPaths._DEBUG_daemonBufferedPathsCallWorker or
			bufferedPaths._DEBUG_daemonBufferedPathsSentClient ~=
				bufferedPaths._DEBUG_daemonBufferedPathsSentWorker then

		logger:warnf( "pixel paths dropped between daemon threads: sent %s (%s total), received %s (%s total)",
						bufferedPaths._DEBUG_daemonBufferedPathsCallClient,
						bufferedPaths._DEBUG_daemonBufferedPathsSentClient,
						bufferedPaths._DEBUG_daemonBufferedPathsCallWorker,
						bufferedPaths._DEBUG_daemonBufferedPathsSentWorker )

	end

	return bufferedPaths
						
end

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

local function makePixelPathsStreamOfStream( fps, ozCatalogId, wfCatalog,
												pixelProviderProcessor )

	if wfCatalog:ozCatalogId() ~= ozCatalogId then

		return Rx.Observable.empty()

	end

	local weakPixelProviderProcessor = 
			makeWeakRef( pixelProviderProcessor )

	return pixelProviderProcessor
				:bufferedPixelPathStream( fps )
				:map( 
					function( bufferedPaths )

						return checkForDroppedPaths( 
									weakPixelProviderProcessor, bufferedPaths )

						
					end )

end

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

local function make( wfCatalogPixelProviderProcessorStream, ozCatalogId, fps )

	local fpsWFCatalogPixelProviderProcessorStream =
			Rx.Observable.combineLatest(
					Rx.Observable.just( fps ),
					Rx.Observable.just( ozCatalogId ),
					wfCatalogPixelProviderProcessorStream ) 
	

	return {

		value = fpsWFCatalogPixelProviderProcessorStream 
					:flatMapLatest( makePixelPathsStreamOfStream )

	}

end

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

return {

	make = make

}

