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

WFRxModelsTest_Server.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 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.

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

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

	-- WF.core.lua
local WFRequestDispatcher = require 'WFRequestDispatcher'

	-- WF.core.rxlua
local WFRxScheduler = require 'WFRxScheduler'


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

local WFRxModelsTest_Server = {}

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

WFRequestDispatcher.onMessage( 'not_this-message', function() end )

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

function WFRxModelsTest_Server.simpleStringTest()

	debug.print( "WFRxModelsTest_Server.simpleStringTest" )

	local var = Observable.just( 'foo' )

	return { value = var }

end

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

function WFRxModelsTest_Server.simpleIntegerTest()

	debug.print( "WFRxModelsTest_Server.simpleIntegerTest" )

	local var = Observable.just( 1 )

	return { value = var, }

end

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

function WFRxModelsTest_Server.simpleTableTest()

	debug.print( "WFRxModelsTest_Server.simpleTableTest" )

	return {
		value = Observable.just( { foo = "bar" } )
	}

end

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

function WFRxModelsTest_Server.multiResultTest( x, y, z )

	debug.print( "WFRxModelsTest_Server.multiResultTest" )

	local var = Observable.just( x, y, z )

	return { value = var, }

end

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

function WFRxModelsTest_Server.echo( x )

	debug.print( "WFRxModelsTest_Server.echo, ", x )

	local subject = Rx.ReplaySubject( 1 )
	subject:onNext( x )

	return {

		value = subject,

		events = {

			echo = function( self, y )
				self.value:onNext( y )
			end,

			error = function( self, y )
				self.value:onError( y )
			end,

		},

		models = {

			toUpper = function( self )
				return self.valueB:mapB( string.upper )
			end,

		}

	}

end

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

function WFRxModelsTest_Server.simpleObservableTable()

	debug.print( "WFRxModelsTest_Server.simpleObservableTable" )

	local var1 = Observable.just( 'foo' )

	local var2 = Observable.timer( 1, WFRxScheduler.sharedScheduler() )
		:map( function()
			return 'bar'
		end )

	return {

		value = {
			var1 = var1,
			var2 = var2,
			staticString = 'const',
			staticNumber = 42,
		}

	}

end

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

function WFRxModelsTest_Server.openStream()

	local subject = Rx.Subject()

	return {

		value = {
			closesFast = Observable.just( true ),
			keptOpen = subject,
		},

		events = {

			setData = function( self, value )
				subject:onNext( value )
			end,

			closeSubject = function( self )
				subject:onCompleted()
			end,

		}

	}

end

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

function WFRxModelsTest_Server.openLoudElidedStream()

	local subject = Rx.ReplaySubject( 3 )

	return {

		value = {
			streamValue = subject,
		},
		
		events = {
		
			postSomeDataToStream = function( self, value )
				
				subject:onNext( 1 )
				subject:onNext( 2 )
				subject:onNext( 3 )
				
			end,
			
			closeSubject = function( self )
				subject:onCompleted()
			end,
		}
	}
		
end

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

function WFRxModelsTest_Server.openLoudNonElidedStream()

	local subject = Rx.ReplaySubject( 3 )

	return {

		deliverAllValues = true,
			-- For historical reasons, WFModels will, by default, omit values
			-- when values are generated by the server faster than the client
			-- can process them, so long as the server delivers the most recent
			-- value in a reasonably timely fashion. The "deliverAllValues"
			-- option disables this behavior and forces delivery of all
			-- values in the order in which they were generated.

		value = {
			streamValue = subject,
		},
		
		events = {
		
			postSomeDataToStream = function( self, value )
				
				subject:onNext( 1 )
				subject:onNext( 2 )
				subject:onNext( 3 )
				
			end,
			
			closeSubject = function( self )
				subject:onCompleted()
			end,
		}
	}
		
end

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

function WFRxModelsTest_Server.allHaveToProduceOneResult()

	local values = {}

	for i = 1, 3 do

		values[ 'key' .. i ] = Observable.timer( i, WFRxScheduler.sharedScheduler() )
			:map( function()
				return i
			end )

	end

	return {
		value = values,
	}

end

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

function WFRxModelsTest_Server.singleError()

	return {

		value = { Observable.throw( 'error' ) }

	}

end

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

function WFRxModelsTest_Server.combinedError()

	return {

		value = {
			var1 = Observable.just( 42 ),
			var2 = Observable.throw( 'error' ),

		}

	}

end

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

function WFRxModelsTest_Server.noValue()

	return {

		value = Observable.empty(),

	}

end

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

function WFRxModelsTest_Server.counter()

	local counter = 0

	local counterObs = Rx.ReplaySubject( 1 )
	counterObs:onNext( counter )

	return {

		value = counterObs,

		models = {

			sub = function( self, x )

				counter = counter + 1
				counterObs:onNext( counter )

				return {
					value = Rx.BehaviorSubject( counter ),
					events = {
						echo = function( self, y )
							self.value:onNext( y )
						end,
					},
					close = function()
						counter = counter - 1
						counterObs:onNext( counter )
					end
				}
			end,

		},

		events = {

			completeModel = function( self, x )
				counterObs:onCompleted()
			end,

		},

		close = function()
			debug.print( "Closed" )
		end,

	}

end

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

function WFRxModelsTest_Server.scalarValue()

	return {
		value = 'foo',
	}

end

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

return WFRxModelsTest_Server
