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

ADOBE CONFIDENTIAL
------------------
Copyright 2016 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 Rx = require 'Rx'

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

local SchedulerArgument = Rx.internals.SchedulerArgument
local CompositeDisposable = Rx.CompositeDisposable
local SerialDisposable = Rx.SerialDisposable
local ObservableBase = Rx.ObservableBase

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

local WFRxDifferenceFilterWithWindowObservable = Rx.Class( 'WFRxDifferenceFilterWithWindow', {

	ObservableBase,
	
	_source = false,
	
	_maxDifference = false,
	
	_window = false,
	
	_scheduler = false,
	

	__init = function( self, source, maxDifference, window, scheduler )
	
		self._source = source
		self._maxDifference = maxDifference
		self._window = window
		self._scheduler = scheduler
		
	end,
	
	subscribeCore = function( self, observer )
	
		local cancelable = SerialDisposable()
		
	 	local value
		
		local function clearValue()
			cancelable:dispose()
			value = nil
		end
		
		local window = self._window
		local scheduler = self._scheduler
		local maxDifference = self._maxDifference
		local source = self._source
		
	 	local subscription = source:subscribe(
			
			function( currentValue, unusedValue )
			
				assert( unusedValue == nil, 'Only one parameter is supported' )
				
				assert( type( currentValue ) == 'number', 'Only Lua type number is allowed' )
			
				if currentValue >= maxDifference then
				
					clearValue()
					
					observer:onNext( currentValue )
					
				else
				
					value = currentValue
					
					if not cancelable:getDisposable() then
					
						local si = scheduler:scheduleFuture( window,
								function()
	
									if value then
	
										observer:onNext( value )
	
										value = nil
	
									end
	
								end)
					
						cancelable:setDisposable(si)
						
					end
					
				end
				
			end,
			
			function( ... )
			
				clearValue()
				observer:onError(...)
				
			end,
			
			function()
			
				clearValue()
				observer:onCompleted()
				
			end
			
		)
		
		return CompositeDisposable( subscription, cancelable )
		
	end,
	
} )


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

return function( self, maxDifference, window, scheduler )

	scheduler = SchedulerArgument.validateWithoutDefault(
		'WFRxDifferenceFilterWithWindow', scheduler)

	if type(window) ~= 'number' or window <= 0 then
		error("WFRxDifferenceFilterWithWindow: window argument must be a number greater than 0", 2)
	end
	
	if type( maxDifference ) ~= 'number' or maxDifference <= 0 then
		error("WFRxDifferenceFilterWithWindow: maxDifference argument must be a number greater than 0", 2)
	end
	
	return WFRxDifferenceFilterWithWindowObservable( self, maxDifference, window, scheduler )
	
end
