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

NmbProfileDownloadHelper.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2018 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.

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

local Rx = require 'Rx'
local WFLogger = require 'WFLogger'

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

local logger = WFLogger 'NmbProfileDownloadHelper'
	--:setLevel( 'DEBUG' )


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

local function matchesAssetRequest( key )

	return function( responseKey)

		return responseKey == key

	end

end

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

local function makeResponseStreamForRequest( downloader, fileName )

	return downloader:getResponseStream()
		:where( matchesAssetRequest( fileName ) )
		:take( 1 )
end

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

local function doOneStdProfileDownload( downloader, fileName, profileInfo, httpPriority )

	logger:debugf( "doOneStdProfileDownload fileName:%s", fileName )
	local savedSubscription
	
	return Rx.Observable.create(
		function( observer )

			local responseStream = makeResponseStreamForRequest( downloader, fileName )
			
			savedSubscription = 
					responseStream:subscribe( observer )

			downloader:requestProfileBinary( profileInfo, httpPriority )
			
			return Rx.Disposable.create( function()
			
				-- TODO [ashj 2018-03-09]: When the stream is
				-- complete, this will get called. Can we
				-- differentiate?
				-- Anyways cancelling will be a nop if the
				-- request has already completed.
				
				downloader:cancelBinaryRequest( fileName )
				savedSubscription:dispose()

			end )
		
	end )
end

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

local function doOneCustomStyleDownload( downloader, assetId, httpPriority )

	local savedSubscription
	
	return Rx.Observable.create(
		function( observer )

			local responseStream = makeResponseStreamForRequest( downloader, assetId )
			
			savedSubscription = 
					responseStream:subscribe( observer )

			downloader:requestStyleBinary( assetId, httpPriority )
			
			return Rx.Disposable.create( function()
			
				-- TODO [ashj 2018-03-09]: When the stream is
				-- complete, this will get called. Can we
				-- differentiate?
				-- Anyways cancelling will be a nop if the
				-- request has already completed.
				
				downloader:cancelStyleBinaryRequest( assetId )
				savedSubscription:dispose()

			end )
		
	end )
end

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

return {
	doOneStdProfileDownload = doOneStdProfileDownload,
	doOneCustomStyleDownload = doOneCustomStyleDownload,
}