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

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_bufferedPathsCallClient",
			( rawget( pixelProviderProcessor, 
						"_DEBUG_bufferedPathsCallClient" ) or 0 ) + 1 )

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

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

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

	if bufferedPaths._DEBUG_bufferedPathsCallClient ~=
			bufferedPaths._DEBUG_bufferedPathsCallWorker or
			bufferedPaths._DEBUG_bufferedPathsSentClient ~=
				bufferedPaths._DEBUG_bufferedPathsSentWorker then

		logger:warnf( "pixel paths dropped between threads: sent %s (%s total), received %s (%s total)",
						bufferedPaths._DEBUG_bufferedPathsCallClient,
						bufferedPaths._DEBUG_bufferedPathsSentClient,
						bufferedPaths._DEBUG_bufferedPathsCallWorker,
						bufferedPaths._DEBUG_bufferedPathsSentWorker )

	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

}

