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

WFCatSpecMock.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.

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

Mock catalog spec object to be used in self tests.

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

	-- RxLua
local Rx = require 'Rx'

	-- WF.core.utils
local _ = require 'WFArguments_uuid'
local KSJsonDecoder = require 'KSJsonDecoder'
local WFArguments = require 'WFArguments'
local WFCloseSupport = require 'WFCloseSupport'

	-- WF.web.http_client
local WFRxHttpClientMock = require 'WFRxHttpClientMock'

	-- WF.web.utils
local WFURI = require 'WFURI'

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

local check = assert( WFArguments.check )

local storageLimit = 100*1024*1024

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

local WFCatSpecMock = {

	_catalogId = false,
	_userId = false,
	_catalogHref = false,
	_httpMock = false,
	_subscriptionExpired = false,
	_entitlement = false,
	
}

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

function WFCatSpecMock:__init( catalogId, userId, rxTest, expectedRequestResponsePairs, optRateLimiter )

	check( 'WFCatSpec', 'uuid', 'uuid', 'WFRxTestCaseRunner', '?table', '?boolean' )

	self._catalogId = catalogId

	self._userId = userId

	self._catalogHref = WFURI.resolve( '/v2/catalogs/', catalogId )

	self._httpMock = WFRxHttpClientMock( rxTest, expectedRequestResponsePairs or {}, optRateLimiter )

	self._subscriptionExpired = Rx.BehaviorSubject( false )

	self._entitlement = Rx.BehaviorSubject( {
		storage = {
			used = 0,
			limit = storageLimit,
			display_limit = storageLimit,
			warn = storageLimit * .9
		}
	} )

end

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

function WFCatSpecMock:serverBaseUrl()

	check( 'WFCatSpec' )

	return ""

end

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

function WFCatSpecMock:catalogId()

	return self._catalogId

end

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

function WFCatSpecMock:ozUserId()

	return self._userId

end

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

function WFCatSpecMock:userIdIMS()

	return Rx.Observable.just( "@AdobeID" )

end

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

function WFCatSpecMock:wfOzUser()

	-- this is a terribly stubbed out WFCatUser. Add more as needed for mock purposes
	
	local WFCatUserMock = {
	
		__init = function() end,
	
		userId = function() return self._userId end,
		
		isSubscriptionExpired = function() return self._subscriptionExpired end,
	
		entitlement = function() return self._entitlement end,
		
		serverBaseUrl = function() return '' end,
		
		userInfoCacheFolder = function() return '' end,
		
		getAccountDocFromOz = function( _, options )
			
			if options and options.dontFetchAccountDoc then
			
				return self._entitlement
					:map( function( entitlement )
						return {
							entitlement = entitlement,
						}
					end )
			
			end
			
			local accountUrl = WFURI.resolve( self:serverBaseUrl(), string.format( "/v2/accounts/%s", self._userId ) )
	
			local requestOptions = {
				bypassRateLimiter = true,
			}

			return Rx.Observable.combineLatest( self:get( accountUrl, requestOptions ), self._entitlement )
			
				:flatMap( function( userAccountJSON, entitlement )

					local userAccountDoc = KSJsonDecoder.decodeWebSafeJson_stripNull_ignoreEmpty( userAccountJSON )

					userAccountDoc.entitlement = entitlement
					
					return Rx.Observable.just( userAccountDoc )
					
				end )
			
		end,
		
		purgeAssetsAfterValue = function() return Rx.Observable.just( 60*24*60*60 ) end,
		
		hasValidConnectionToOz = function() return Rx.Observable.just( true ) end,
		
		WFCloseSupport.addCloseSupportMethods,
		
	}
	
	local wfCatUserMock = Rx.Class 'WFCatUser'( WFCatUserMock )
	
	return wfCatUserMock()
	
end

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

function WFCatSpecMock:catalogHref()

	check( 'WFCatSpec' )

	return self._catalogHref

end

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

function WFCatSpecMock:catalogDoc()
	
	check( 'WFCatSpec' )
	
	return nil

end

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

function WFCatSpecMock:catalogUpdatesUrl()
	check( 'WFCatSpec' )
	
	local updatesUrl = self:catalogHref() .. '/updates'
         -- TODO [scouten 2015-07-29]: Use WFURI to construct this.
         
     return WFURI.stringByAppendingPathComponents( self:serverBaseUrl(),
                                                          updatesUrl )
end

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

function WFCatSpecMock:spaceUploadUrl()
	
	check( 'WFCatSpec' )
		
	local spaceUploadHref = '/v2/spaces/updates'	
	
	return WFURI.stringByAppendingPathComponents( self:serverBaseUrl(),
														 spaceUploadHref )
														 
end

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

function WFCatSpecMock:spaceUpdatesUrlForSpaceId( spaceId )
	
	check( 'WFCatSpec', '+string' )
		
	local spaceUpdatesHref = string.format( "/v2/spaces/%s/updates", spaceId )
		
	return WFURI.stringByAppendingPathComponents( self:serverBaseUrl(),
												  		spaceUpdatesHref )
														 
end

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

function WFCatSpecMock:get( ... )

	check( 'WFCatSpec' )

	return self._httpMock.get( ... )

end

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

function WFCatSpecMock:put( ... )

	check( 'WFCatSpec' )

	return self._httpMock.put( ... )

end

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

function WFCatSpecMock:getBinary( ... )

	check( 'WFCatSpec' )

	return self._httpMock.get( ... )

end

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

function WFCatSpecMock:putBinary( ... )

	check( 'WFCatSpec' )

	return self._httpMock.put( ... )

end

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

function WFCatSpecMock:expectHttpRequest( httpRequest )
	
	check( 'WFCatSpec' )
	
	self._httpMock.addHttpRequest( httpRequest )
	
end

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

function WFCatSpecMock:requestCount()

	check( 'WFCatSpec' )

	return self._httpMock.requestCount()

end

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

function WFCatSpecMock:watchHttpErrors()

	check( 'WFCatSpec' )

	return self._httpMock.watchHttpErrors()

end

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

function WFCatSpecMock:setSubscriptionExpired( isExpired )

	self._subscriptionExpired:onNext( isExpired )

end

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

function WFCatSpecMock:updateStorageUsed( used )

	self._entitlement:onNext( {
		storage = {
			used = used,
			limit = storageLimit,
			display_limit = storageLimit,
			warn = storageLimit * .9
		}
	} )

end

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

function WFCatSpecMock:updateEntitlementStatus( entitlementStatus )

	self._entitlement:onNext( {
		status = entitlementStatus
	} )

end

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

function WFCatSpecMock:isStageEnv()

	check( 'WFCatSpec' )

	return false

end

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

return Rx.Class 'WFCatSpec'( WFCatSpecMock )
