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

WFOAuth2SessionMessageProcessor.lua

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

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

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

	-- WF.core.lua.threading
local WFRequestDispatcher = require 'WFRequestDispatcher'

	-- WF.web.oauth2_client
local WFOAuth2SessionImp = require 'WFOAuth2SessionImp'

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

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

-- @mark WFOAuth2SessionMessageProcessor namespace

local WFOAuth2SessionMessageProcessor = {}

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

function WFOAuth2SessionMessageProcessor.initMessageProcessor( sessionId, optServerBase )

	-- INFO [pkamin 2016-07-14]: Clients can provide their own implementation of the
	-- IMS Token refresh. The mobile clients will use the CreativeSDK to do this Job
	
	local nativeTokenFreshProvider = nil
	
	local externalModule = WFCapabilities.getCapability( 'WFOAuth2NativeTokenRefreshProvider' )
	
	if externalModule then
	
		nativeTokenFreshProvider = require( externalModule )
		
		assert( nativeTokenFreshProvider.requestNewTokens, 
			"WFOAuth2ExternalSessionProvider needs to implement the requestNewTokens method" )
			
	end

	local oAuth2SessionImp = WFOAuth2SessionImp( sessionId, nativeTokenFreshProvider )

	WFOAuth2SessionMessageProcessor.attachMessageHandlers( oAuth2SessionImp )

end

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

function WFOAuth2SessionMessageProcessor.attachMessageHandlers( oAuth2SessionImp )

	WFRequestDispatcher.onMessageDo {
		message = 'storeTokens',
		target = oAuth2SessionImp,
	}

	WFRequestDispatcher.onMessageDo {
		message = 'fetchTokens',
		target = oAuth2SessionImp,
	}

	WFRequestDispatcher.onMessageDo {
		message = 'get',
		target = oAuth2SessionImp,
	}

	WFRequestDispatcher.onMessageDo {
		message = 'put',
		target = oAuth2SessionImp,
	}

	WFRequestDispatcher.onMessageDo {
		message = 'hasValidLogin',
		target = oAuth2SessionImp,
		onlyIfCallbackIsStillValid = true,
	}

	WFRequestDispatcher.onMessageDo {
		message = 'simulateForcedLogout',
		target = oAuth2SessionImp,
	}

	WFRequestDispatcher.onMessageDo {
		message = 'logout',
		target = oAuth2SessionImp,
	}

	WFRequestDispatcher.enableDiagnosticLogForRequestStats()

end

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

return WFOAuth2SessionMessageProcessor
