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

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.

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

	-- RxLua
local Rx = require 'Rx'

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

	-- WF.oz_client.ims
local WFImsAppIdentity = require 'WFImsAppIdentity'

	-- WF.oz_client.user
local WFOzUserLogin = require 'WFOzUserLogin'

	-- WF.oz_client.sync
local WFCatalog = require 'WFCatalog'

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

-- @mark WFOzProductionUserLogin namespace

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

local WFOzProductionUserLogin = {}

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

function WFOzProductionUserLogin.createWFCatalogForCredentials( rxTest, email, password )

	local catalog
	
	local userInfoCachePath = KSPathUtils.child( KSPathUtils.getStandardFilePath 'temp', 'userCache' )
	local docStoreRootPath = KSPathUtils.getStandardFilePath 'temp'
	local baseServerUrl = 'https://photos.adobe.io'
	
	local clientId, secret = WFImsAppIdentity.getClientIdAndSecret()
	
	WFImsAppIdentity.setClientIdAndSecret( 'CarouselTest1', 'ce948ff4-d59c-4096-81a4-168838f023c7' )
	
	rxTest:finally( function()
	
		WFImsAppIdentity.setClientIdAndSecret( clientId, secret )
		
	end )
	
	rxTest:expectSingleValue( 
	
		WFOzUserLogin.tokensFromEmailAndPassword( 
			baseServerUrl,
			'v2',
			email, 
			password )
			
		:flatMapLatest( function( baseServerUrl, accessToken, refreshToken )
		
			return WFOzUserLogin.loginFromTokens( userInfoCachePath, baseServerUrl, accessToken, refreshToken )
			
		end )
		
		:flatMapLatest( function( user, _, accessToken, refreshToken )
		
			rxTest:finally( function()
				
				local userDocStore = user:userInfoCache()
				
				userDocStore:haltAndDelete()
				
			end )
			
			return user:storeTokens( accessToken, refreshToken )
				:concat( Rx.Observable.just( true ) )
				:flatMapLatest( function()
				
					local catalogSubtype = 'lightroom'
					
					return user:findOrCreateCatalog( 'lightroom', catalogSubtype )
					
				end )
					
		end )
		
		:map( function( catalogSpec )
			
			WFArguments.check( 'WFCatSpec' )
			
			catalog = WFCatalog( catalogSpec, docStoreRootPath, { } )
			
			rxTest:finally( function()
			
				catalog:indexStore():haltAndDelete()
				catalog:docStore():haltAndDelete()
				
				catalog:close()
				
			end )
			
			return true
			
		end ), true )
		
	return catalog
	
end

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

return WFOzProductionUserLogin
