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

LrmLibraryNotification.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2020 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 LrmLibraryNotification = {}

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

function LrmLibraryNotification.notificationUnreadCountModel( self )
		
	return {
		value = self.catalog:getSyncClient():notificationUnreadCount()
	}

end

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

function LrmLibraryNotification.notificationListModel( self )

	local result = self._wfCatalogStream
		
		:flatMap( function( wfCatalog )
		
			local freezeObservable = Rx.ReplaySubject( 0 )
		
			return self._syncManager:getSyncActivityControl()
				:map( function( shouldSync, ... )
					return shouldSync
				end )
				:distinctUntilChanged()
				:flatMapLatest( function( shouldSync )
					
					if not shouldSync then
						return self.catalog:getSyncClient():notificationList()
							:map( function( notificationList )
								return notificationList, true
							end )
					else
						local waitUntilSyncCycleComplete = true
						return self.catalog:getSyncClient():notificationList( waitUntilSyncCycleComplete )
					end
					
				end )
				:takeUntil( freezeObservable )
				:map( function( notificationList, offlineOrSyncCycleComplete )
					
					if offlineOrSyncCycleComplete then
						freezeObservable:onNext( true )
					end
				
					return notificationList
					
				end )
				
		end )

	return {
		value = result
	}

end

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

function LrmLibraryNotification.setReadNotificationModel( self, notificationMarkRead, optNotificationMarkUnread )

	local result = self._wfCatalogStream

		:flatMap( function( wfCatalog )

			return self._syncManager:getSyncActivityControl()
				:take( 1 )
				:filter( function( shouldSync, ... )
					return shouldSync
				end )
				:flatMap( function()

					return self.catalog:getSyncClient():setReadNotification( notificationMarkRead, optNotificationMarkUnread )

				end )

		end )

	return {
		value = result
	}

end

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

function LrmLibraryNotification.setVisitedNotificationModel( self, notificationMarkVisited, optNotificationMarkUnvisited )

	local result = self._wfCatalogStream

		:flatMap( function( wfCatalog )

			return self._syncManager:getSyncActivityControl()
				:take( 1 )
				:filter( function( shouldSync, ... )
					return shouldSync
				end )
				:flatMap( function()

					return self.catalog:getSyncClient():setVisitedNotification( notificationMarkVisited, optNotificationMarkUnvisited )

				end )

		end )

	return {
		value = result
	}

end

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

function LrmLibraryNotification.notificationKillSwitchModel( self )
		
	return {
		value = self.catalog:getSyncClient():notificationKillSwitch()
	}

end

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

return LrmLibraryNotification
