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

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

local WFRxScheduler = require "WFRxScheduler"

	-- WF.core.os
local WFFileUtils = require 'WFFileUtils'

-- local runUntilDone = require "WFRxRunUntilDone"
local WFPlatformInfo = require 'WFPlatformInfo'
--==============================================================================

local LrmOzBinaryDownloaderUtils = {}

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

local function makeDiskSpaceWatchdog( catalog )

	local freeSpace = Rx.Observable.interval( 60, WFRxScheduler.sharedScheduler() )
		:startWith( 0 )
		:flatMapLatest(function ()
			return catalog
				:getDataCachePath()
				:map( function( path )
				if WFPlatformInfo.isAndroid then
					local KSPathUtils = require 'KSPathUtils'
					path = KSPathUtils.parent (path)
				end
					local fileSystemFreeSpace, _ -- fileSystemSize
							= WFFileUtils.freeSpaceAtPath( path )
							
					return fileSystemFreeSpace
						
				end )	
		end )

	return freeSpace
	
end

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

function LrmOzBinaryDownloaderUtils.makeObserverBinaryDownloaderFn( binaryDownloader )

	local minimumFreeSpace = 600 * (1024 * 1024)

	return function( reachability, freeSpace, isWiFiOnlySync, isSyncEnabled, forceUpgrade, inLoupe, isAuthenticatedUser, binaryStrategy, isAppInBackground, entitlement )

		local downloadBinaries = true
		local downloadPresetBinaries = true

		if not isSyncEnabled then
			downloadBinaries = false
			downloadPresetBinaries = false
		end
		
		if reachability == 'unreachable' then
			downloadBinaries = false
			downloadPresetBinaries = false
		end
		
		if reachability == 'wwan' and isWiFiOnlySync then
			downloadBinaries = false
			downloadPresetBinaries = false
		end
		
		if freeSpace and freeSpace < minimumFreeSpace then
			downloadBinaries = false
			downloadPresetBinaries = false
		end
		
		if forceUpgrade then
			downloadBinaries = false
			downloadPresetBinaries = false
		end
		
		if inLoupe then
			downloadBinaries = false
		end
		
		if not isAuthenticatedUser then
			downloadBinaries = false
			downloadPresetBinaries = false
		end

		if WFPlatformInfo.isAndroid then

			if isAppInBackground then
				downloadBinaries = false
				downloadPresetBinaries = false
			end

			if entitlement and (entitlement.status == 'created') then
				downloadPresetBinaries = false
			end

		end

		return downloadBinaries, binaryStrategy, downloadPresetBinaries

	end
	
end

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

function LrmOzBinaryDownloaderUtils.makeBinaryDownloaderManager( args )

	local catalog = assert( args.catalog )
	local binaryDownloader = assert( args.binaryDownloader )
	local isWiFiOnlySync = assert( args.isWiFiOnlySync )
	local isSyncEnabled = assert( args.isSyncEnabled )
	local isInLoupe = assert( args.isInLoupe )
	local isAuthenticatedUser = assert( args.isAuthenticatedUser )
	local forceUpgrade = assert( args.forceUpgrade)
	local isReachable = assert( args.isReachable )
	local binaryDownloadStrategy = assert( args.binaryDownloadStrategy )
	local isAppInBackground = assert( args.isAppInBackground )
	local entitlement = assert( args.entitlement )
	local freeSpace = makeDiskSpaceWatchdog( catalog )
	
	local enableBinaryDownloader = Rx.Observable.combineLatest( 
				LrmOzBinaryDownloaderUtils.makeObserverBinaryDownloaderFn( binaryDownloader ),
				isReachable, freeSpace, isWiFiOnlySync, isSyncEnabled, forceUpgrade, isInLoupe, isAuthenticatedUser,
				binaryDownloadStrategy, isAppInBackground, entitlement )

	return enableBinaryDownloader
				
end

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

return LrmOzBinaryDownloaderUtils
