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

ADOBE CONFIDENTIAL
___________________

Copyright 2020 Adobe

All Rights Reserved.

NOTICE: All information contained herein is, and remains
the property of Adobe and its suppliers, if any. The intellectual
and technical concepts contained herein are proprietary to Adobe
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.
------------------------------------------------------------------------------]]

	-- WF.core.os
local WFPlatformInfo = require 'WFPlatformInfo'

if not WFPlatformInfo.isMobile() then
       return {}
end

	-- RxLua
local Rx = require 'Rx'

	-- WF.core.utils
local WFArguments = require 'WFArguments'
local KSPathUtils = require 'KSPathUtils'

	-- WF.core.self_test
local WFRxTestCase = require 'WFRxTestCase'

	-- WF.oz_client.managed_catalog.sync
local WFOzUserTestSession = require 'WFOzUserTestSession'
local WFCatUser = require 'WFCatUser'
local WFCatalog = require 'WFCatalog'

	-- lrmobile
local LrmSyncClient = require 'LrmSyncClient'

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

local check = assert( WFArguments.check )

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

-- @mark LrmSyncClientTest namespace

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

local LrmSyncClientTest = {}

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

function LrmSyncClientTest.rxTestCallAllSyncClientBaseMethods_testNightly( rxTest )

	rxTest:setTimeLimit( 30 )
	rxTest:makeVerbose()
	
	local userTestSession = WFOzUserTestSession( rxTest, { 
				suppressStoreTokens = true,
				v2 = true 
			} )
	
	local user
	local catSpec
	local catalog
	
	local userInfoCachePath = KSPathUtils.child( KSPathUtils.getStandardFilePath 'temp', 'userCache' )
	local docStoreRootPath = KSPathUtils.getStandardFilePath 'temp'
	
	rxTest:expectSingleValue( userTestSession:getCleanOzAccount()
		
		:flatMapLatest( function( userId )
		
			check( 'uuid' )
			
			user = WFCatUser( userTestSession:ozServerBaseUrl(), userId, userInfoCachePath )
			
			rxTest:finally( function()
			
				local userDocStore = user:userInfoCache()
				
				userDocStore:haltAndDelete()
				
			end )
			
			local accessToken, refreshToken = userTestSession:accessAndRefreshTokens()
			
			return user:storeTokens( accessToken, refreshToken )
				:concat( Rx.Observable.just( true ) )
				:flatMapLatest( function()
				
					return user:createCatalog( 'lightroom' )
					
				end )
			
		end )
		
		:map( function( catalogSpec )
		
			check( 'WFCatSpec' )
			
			catSpec = catalogSpec
			
			catalog = WFCatalog( catalogSpec, docStoreRootPath, { } )
			
			rxTest:finally( function()
			
				catalog:docStore():haltAndDelete()
				catalog:indexStore():haltAndDelete()
				
			end )
			
			return true
			
		end ), true )

	-- The following code verifies that all of the methods within WFCatSyncClientBase 
	-- are exposed as methods on the client-facing LrmSyncClient object. The code
	-- doesn't verify the methods work as expected, just that they're exposed.
	
	local syncClient = LrmSyncClient( catalog )

	local subscription = syncClient:startSync():subscribe()
	subscription:dispose()
	
	subscription = syncClient:stopSync():subscribe()
	subscription:dispose()
	
	subscription = syncClient:startSyncInResidualMode():subscribe()
	subscription:dispose()
	
	subscription = syncClient:stopSyncInResidualMode():subscribe()
	subscription:dispose()
	
	subscription = syncClient:startSpaceMetadataSyncInFreemiumMode():subscribe()
	subscription:dispose()
	
	subscription = syncClient:stopSpaceMetadataSyncInFreemiumMode():subscribe()
	subscription:dispose()
	
	subscription = syncClient:spaceFeedsMaintenanceNotificationStream():subscribe()
	subscription:dispose()
	
	subscription = syncClient:pausePusherReturningCounter():subscribe()
	subscription:dispose()
	
	subscription = syncClient:resumePusher():subscribe()
	subscription:dispose()
	
	subscription = syncClient:_DEBUG_startMetadataPusher():subscribe()
	subscription:dispose()
	
	subscription = syncClient:_DEBUG_stopMetadataPusher():subscribe()
	subscription:dispose()

	subscription = syncClient:setUserIsOverQuota():subscribe()
	subscription:dispose()
	
	subscription = syncClient:setUserIsExpired():subscribe()
	subscription:dispose()
	
	subscription = syncClient:isUploadingMetadata():subscribe()
	subscription:dispose()
	
	subscription = syncClient:isDownloadingMetadata():subscribe()
	subscription:dispose()
	
	subscription = syncClient:getLatestDownloadedAssetIdsStream():subscribe()
	subscription:dispose()
	
	subscription = syncClient:isOzCatalogDeleted():subscribe()
	subscription:dispose()
	
	subscription = syncClient:getRequiredUpdateStream():subscribe()
	subscription:dispose()
	
	subscription = syncClient:checkOzForUpdates():subscribe()
	subscription:dispose()

	subscription = syncClient:setReadNotification():subscribe()
	subscription:dispose()
	
	subscription = syncClient:setVisitedNotification():subscribe()
	subscription:dispose()
	
	subscription = syncClient:notificationUnreadCount():subscribe()
	subscription:dispose()
	
	subscription = syncClient:notificationList():subscribe()
	subscription:dispose()
	
	subscription = syncClient:notificationKillSwitch():subscribe()
	subscription:dispose()
	
	subscription = syncClient:haltAndDeleteNotificationDb():subscribe()
	subscription:dispose()
	
	subscription = syncClient:_DEBUG_simulateCorruptionNotificationDb():subscribe()
	subscription:dispose()
	
	-- Verify custom methods in LrmSyncClientImp
	----------------------------------------------------------------------------

	subscription = syncClient:isPushingMetadata():subscribe()
	subscription:dispose()
	
	subscription = syncClient:isPullingMetadata():subscribe()
	subscription:dispose()
	
	subscription = syncClient:getDeleteFeedCallback():subscribe()
	subscription:dispose()
	
	subscription = syncClient:startSyncWithoutUploader():subscribe()
	subscription:dispose()
	
	subscription = syncClient:startAccountsSync():subscribe()
	subscription:dispose()
	
	subscription = syncClient:stopAccountsSync():subscribe()
	subscription:dispose()
	
	subscription = syncClient:startSpaceSyncWithoutOzCatalog():subscribe()
	subscription:dispose()
	
	subscription = syncClient:getDetailedUploadProgress():subscribe()
	subscription:dispose()
	
	subscription = syncClient:getOzMaintenanceNotificationStream():subscribe()
	subscription:dispose()
	
	-- Verify return values of debug methods:
	----------------------------------------------------------------------------
	
	rxTest:expectSingleValue( syncClient:_DEBUG_SyncClientReturnsFoo(), 'foo' )
	
	rxTest:expectSingleValue( syncClient:_DEBUG_SyncClientBaseReturnsTrue(), true )
	
	-- Verify non existent methods cause an error:
	----------------------------------------------------------------------------

	rxTest:expectOnlyError( syncClient:foo(), ':ignore:' )

	syncClient:close()
	
end

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

return WFRxTestCase.wrapSuite( LrmSyncClientTest )