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

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.utils
local WFArguments = require 'WFArguments'
local WFCloseSupport = require 'WFCloseSupport'
local WFLogger = require 'WFLogger'

	-- WF.core.lua
local WFWorkerThread = require 'WFWorkerThread'

	-- WF.core.table
local WFTableArchiving = require 'WFTableArchiving'

local LrmConfig = require "LrmConfig"

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

local check = assert( WFArguments.check )

LrmConfig.registerBinaryStrategyCheck()

local logger = WFLogger( 'LrmSyncClient' )

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

-- @mark LrmSyncClient namespace

-- Manages the reference to Sync MessageProcessor where our Sync infrastructure
-- is running on. See LrmSyncClientImp & WFCatSyncClientBase for the actual 
-- implementation.

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

local syncClientMethods = {

	_syncClientImp = false,
		-- (WFWorkerThread) Reference to the SyncClient WorkerThread instance
	
	WFCloseSupport.addCloseSupportMethods
	
}

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

function syncClientMethods:__init( wfCatalog, optIndexSpecScript, optLocale, optSyncClientImpName, optDataCache )
	
	check( 'LrmSyncClient', 'WFCatalog', '+string|nil', '+string|nil', '+string|nil', '?KSDataCache' )
	
	optIndexSpecScript = optIndexSpecScript or 'WFCatIndexDefinitions.lua'
	optLocale = optLocale or 'en_US'
	optSyncClientImpName = optSyncClientImpName or 'LrmSyncClientImp'
	
	local options = {
		syncClientImpName = optSyncClientImpName,
		serverBase = wfCatalog:ozUser():serverBaseUrl(),
		userId = wfCatalog:ozUserId(),
		userInfoCacheFolder = wfCatalog:ozUser():userInfoCacheFolder(),
		catalogId = wfCatalog:ozCatalogId(),
		indexSpecScript = optIndexSpecScript,
		rootCatalogPath = wfCatalog:fileSpec():rootPath(),
		docStorePath = wfCatalog:fileSpec():docStorePath(),
		dataCache = optDataCache,
		locale = optLocale,
	}
	
	local optionsString = WFTableArchiving.tableToString( options )
	
	self:setOwned {
		_syncClientImp = WFWorkerThread( 'LrmSyncClientMessageProcessor.lua', optionsString )
	}
	
	WFCloseSupport.mustClose( self )
	
end

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

function syncClientMethods:__index( k )

	-- When code attempts to call an undefined method on a LrmSyncClient object,
	-- this function is called instead. This method will then then generate a
	-- function that will automatically call the correct method on the sync client
	-- worker thread.

	if not k then
		return
	end

	local syncClientImpFn = function( _, ... )
	
		logger:infof( "Now calling LrmSyncClientImp:%s via LrmSyncClient", k )
	
		return self._syncClientImp:request( k, ... )
			
	end

	return syncClientImpFn

end

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

return Rx.Class( 'LrmSyncClient', syncClientMethods )
