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

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.

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

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

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

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

-- @mark LrmSyncClientMessageProcessor namespace

local LrmSyncClientMessageProcessor = {}

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

local valueFnEnv = {} -- disallow all function calls

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

local function parseOptions( optionsString )

	local valueFn = loadstring( 'return ' .. optionsString )
	assert( type( valueFn ) == 'function' )
	setfenv( valueFn, valueFnEnv ) -- disallow all function calls
	
	return valueFn()
	
end

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

local function createSyncClientImp( options )

	local syncClientImpName = options.syncClientImpName
	local syncClientModule = require( syncClientImpName )
	
	assert( syncClientModule )
	
	local syncImp = syncClientModule( options )
	
	return syncImp
	
end

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

local function isMetamethod( methodName )
	
	return string.find( methodName, "^__")	

end

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

function LrmSyncClientMessageProcessor.initMessageProcessor( optionsString )

	local options = parseOptions( optionsString )
	
	local syncClientImp = createSyncClientImp( options )

	LrmSyncClientMessageProcessor.attachMessageHandlers( syncClientImp )

end

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

function LrmSyncClientMessageProcessor.attachMessageHandlers( syncImp )

	WFCloseSupport.mustCloseAtThreadTermination( syncImp )
	
	if rawget( syncImp, 'attachMessageHandlers' ) then
		syncImp:attachMessageHandlers()
	end

	-- The following code creates message handlers for all of the
	-- class methods defined within WFCatSyncClientBase and within
	-- LrmSyncClientImp.
	
	local syncClientImpMethods = syncImp.listMethods()
	
	for _, methodName in ipairs( syncClientImpMethods ) do
	
		if not isMetamethod( methodName ) then
	
			WFRequestDispatcher.onMessageDo { 
				message = methodName,
				target = syncImp,
			}
		
		end
	
	end
	
	local syncClientBaseMethods = syncImp:getSyncClientBaseMethodList()
	
	for _, methodName in ipairs( syncClientBaseMethods ) do
	
		if not isMetamethod( methodName ) then
	
			WFRequestDispatcher.onMessageDo { 
				message = methodName,
				target = syncImp,
			}
	
		end
	
	end

	WFRequestDispatcher.enableDiagnosticLogForRequestStats()
	
end

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

return LrmSyncClientMessageProcessor