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

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 = WFImsAppIdentity.getClientId()
	local clientSecret = WFImsAppIdentity.DEBUG_getClientSecret()
	
	WFImsAppIdentity.setClientId( 'XXX' )
	WFImsAppIdentity.DEBUG_setClientSecret( 'XXX' )
	
	rxTest:finally( function()
	
		WFImsAppIdentity.setClientId( clientId )
		WFImsAppIdentity.DEBUG_setClientSecret( clientSecret )

	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
