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

OzCatalogUrls.lua

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

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

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

Centralized repository for Oz URLs so we can change it as needed for new
milestone server deployments.

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

	-- WF.core.logging
local WFLogger = require 'WFLogger'

	-- WF.core.os
local KSPathUtils = require 'KSPathUtils'
local KSPrefs = require 'KSPrefs'
local WFPlatformInfo = require 'WFPlatformInfo'

	-- WF.core.reactive
local KSReactive = require 'KSReactive'

	-- WF.core.utils
local WFCapabilities = require 'WFCapabilities'


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

local disallowCurrentUser = require 'WFCapabilities'.getCapability 'WFOzCurrentUser.disabled' == 'disabled'

local logger = WFLogger 'OzCatalogUrls'

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

if disallowCurrentUser then
	return setmetatable( {}, {
		__index = function( self, funcName )
			logger:error( "OzCatalogUrls.%s disallowed when WFOzCurrentUser.disabled\nvia %s",
					funcName, ':traceback:' )
			error 'OzCatalogURLs disabled by WFOzCurrentUser.disabled'
		end
	} )
end

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

local OzCatalogUrls = {}

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

local function setUrlsForOverrideUrl( url )
	
	local result = {
		baseServerUrl = url,
	}
	for k, v in pairs( result ) do
		OzCatalogUrls[ k ] = v
	end
	
	return result
	
end

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

local baseB

if WFPlatformInfo.isAndroid then

	baseB = KSReactive.constantB( KSPathUtils.getOzServerUrl() )

else

	baseB = KSPrefs.prefB 'ozServerOverrideUrl'
	
end

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

local function urlForPref( url )
	if url then
		return url
	else
		local defaultUrl = WFCapabilities.getCapability( 'DefaultOzServer' )
		return defaultUrl or 'https://photos.adobe.io'
	end
end

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

local sCatalogUrlsB = baseB:closingMapB( function( url )
	local baseUrl = urlForPref( url )
	--debug.print( "OzCatalogUrls is using Oz server url:", url )
	return setUrlsForOverrideUrl( baseUrl )
end )

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

--[[!
	\brief Get a behavior which vends the current set of base catalog URLs.
	This behavior can update when changing service stacks, e.g. OzNext versus
	OzProduction.
	
	Note: in Distribution and Android builds, this behavior never changes.
	
	\return a behavior, whose value is a table.  The table has entries for
	baseServerUrl (the same URLs that can be obtained directly from this 
namespace as one-shot values)
	
--]]
OzCatalogUrls.catalogUrlsB = function()
	return sCatalogUrlsB:relayB()
end

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

return OzCatalogUrls
