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

NmbEnsureProfileExistsModel.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2018 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remains 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 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 Systems Incorporated.

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

	-- Rx
local Rx = require "Rx"
local Observable = Rx.Observable

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

local logger = require "WFLogger"( "NmbEnsureProfileExistsModel" )

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

local function  logErrorForStreamNamed( streamName )

	return function( errStr )
		logger:errorf( "Stream:%s encountered error:%s", streamName, errStr )
	end
end

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

local function isNonNil( v )

	return v ~= nil

end

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

local function makeProcessCameraModelStream( styleSynchronizer, cameraModelName )

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

			local responseStream = 
				styleSynchronizer:makeAdjustablePriorityASAPRequestForCameraModel( cameraModelName )
			
			savedSubscription = 
					responseStream:subscribe( function( cancelFn )
						downgradePriorityFn = cancelFn
					end )


			return Rx.Disposable.create( function()
				
				-- [ashj 2018-09-21]: Since all the profiles
				-- corresponding to the camera model will have to
				-- be downloaded, we are NOT cancelling the request,
				-- instead we are reducing the priority of the
				-- request. 
				savedSubscription:dispose()
				if downgradePriorityFn then
					downgradePriorityFn()
				end

			end )
		
	end )
end

--------------------------------------------------------------------------------
-- TODO [ashj 2018-09-21]: When a negative is available, we need
-- to download the corresponding profiles due to two reasons:
--
-- 1. We idle download camera profiles. But once the image is
-- 		is opened for editing, probably we would want to download
-- 		all the corresponding profiles with high priority.
-- 2. We idle download proifles based on camera-model name
-- 		retrieved from the exif info of the images. But ACR
-- 		profiles uses cannonical model names which might not
-- 		be always same as the one obtained from exif. For eg.,
-- 		for images from iPhoneX, tiff model name is iPhone X,
--		while the cannonical name can be IPHONE10,6 BACK CAMERA.
-- 		The latter can be obtained by calling an api on the 
--		negative. Hence idle downloaded will NOT download those
-- 		profiles and this api should be used.

local function make( wfCatSynchronizerStream )

	local cameraModelNameStream = Rx.ReplaySubject( 1 )
	
	local requestStream = Observable.combineLatest( 
		wfCatSynchronizerStream:filter( isNonNil ),
		cameraModelNameStream:distinctUntilChanged() )
		
			:flatMapLatest( function( wfCatSynchronizer, cameraModelName )
				
				return makeProcessCameraModelStream( 
					wfCatSynchronizer:getStyleSynchronizer(), cameraModelName )
				
			end )
			
	
	local requestStreamSubscription = requestStream
		:subscribeOnError( logErrorForStreamNamed( "EnsureCameraProfileRequestStream" ) )
	
	return {
	
		value = {
			activeCameraModelName = cameraModelNameStream,
		},
		
		events = {
			setActiveCameraModelName = function( _, cameraModelName )
				
				cameraModelNameStream:onNext( cameraModelName )
			end
		},
		
		close = function()

			logger:info( "Closing ensureProfilesExistsForCamera" )
			requestStreamSubscription:dispose()

		end,
	}
end

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

return {

	make = make,

}
