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

NmbAppMessaging.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2016 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

	-- WF.core.os
local WFLocalizationUtils = require 'WFLocalizationUtils'
local WFPlatformInfo = require 'WFPlatformInfo'

	-- WF.core.utils
local WFRxRunUntilDone = require 'WFRxRunUntilDone'
local WFConfiguration = require 'WFConfiguration'

	-- WF.core.rxlua
local WFRxScheduler = require 'WFRxScheduler'


	-- WF.web.json
local KSJsonDecoder = require 'KSJsonDecoder'

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

local runUntilDone = WFRxRunUntilDone

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

-- @mark NmbAppMessaging

local NmbAppMessaging = {}

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

local function getVersionString()

	local WFCapabilities = require 'WFCapabilities'

	local appVersion = WFCapabilities.getCapability( 'WFHttpClient_AppVersion' )
	
	local build = WFCapabilities.getCapability( 'WFHttpClient_Build' )
	
	return string.format( "%s.%s", appVersion, build )

end

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

-- For 'regular' test configurability.
local inAppMessageServerAddress = WFConfiguration.inAppMessaging.ServerAddress
		or "http://www.photoshop.com/api/service_status/"
local inAppMessageProductName
		= WFConfiguration.inAppMessaging.ProductName or "nimbus"
local inAppMessageUpdateAvailableDelay
		= WFConfiguration.inAppMessaging.inAppMessageUpdateAvailableDelay or 60

-- For when the admin tools on pxfuture.com/photoshop.com are broken, and we
-- have to rely on already configured messages/switches to get some testing
-- done.
local inAppMessagingVersion
		= WFConfiguration.inAppMessaging.inAppMessagingVersion or
		getVersionString()
local inAppMessageUpdateAvailableServiceName
		= WFConfiguration.inAppMessaging.inAppMessageUpdateServiceName or
		"nimbus-update-available"

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

local function checkForInAppMessage( wfCatUser, isUpdateAvailable, serviceName )

	local platform = WFPlatformInfo.isMac and "mac" or
									WFPlatformInfo.isWindows and "win" or nil
	local locale = WFLocalizationUtils.getLocaleId()
	
	assert( platform, "platform must be either mac or win" )

	local checkUrl = string.format( "%s%s/%s/%s/%s/%s", inAppMessageServerAddress,
													inAppMessageProductName, 
													inAppMessagingVersion,
													platform,
													locale,
													serviceName )

	return wfCatUser:get( checkUrl )
	:map( function( response, headers )
		if headers and headers.status_code and
								tonumber( headers.status_code ) == 200  then
			local decodedResponse = response and
						KSJsonDecoder.decodeWebSafeJson_stripNull( response )
			if decodedResponse and decodedResponse.message_title then
				return {
					message_title = decodedResponse.message_title,
					message_body = '',
						-- decodedResponse.message_body
						-- Assume that the use of one string is sufficient,
						-- for now. We can also get a 'message_body' field
						-- from photoshop.com, if necessary.
				}
			end
			
		end
	end )
	
end

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

NmbAppMessaging.setupUpdateAvailableCheck = function( wfCatUser,
															isUpdateAvailable )
	Observable.timer( inAppMessageUpdateAvailableDelay, WFRxScheduler.sharedScheduler() )
		:flatMapLatest( function()                                        
			return checkForInAppMessage( wfCatUser, isUpdateAvailable,
										inAppMessageUpdateAvailableServiceName )
		end )
		:tap( function( message )
			if message then
				isUpdateAvailable:onNext( message )
			end
		end )
	:call( runUntilDone )
															
end

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

return NmbAppMessaging
