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

WFCatImportManifestProcessorBase.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2017 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 WFRxScheduler = require 'WFRxScheduler'

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

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

	-- WF.oz_client.managed_catalog.import
local WFCatImportManifestConstants = require 'WFCatImportManifestConstants'

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

local logger = WFLogger 'WFCatImportManifestProcessorBase'
local derefOpt = KSTableUtils.derefOpt

local kDocStoreFetchBatchSize = 100

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

local WFCatImportManifestProcessorBase = {

	_stageName = false,
	
	_totalCount = false,
	_errorCount = false,
	_successCount = false,
	_alreadyProcessedCount = false,
	
	_processedRows = false,
	
	_messageCodeCompleted = false,
	_messageCodeErrorCount = false,
	_messageCodeGenericError = false, 
	
}

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

function WFCatImportManifestProcessorBase:__init( )
	
	-- This expects the stageName to be set in child class
	assert( self._stageName )
	
	self._totalCount = 0
	self._errorCount = 0
	self._successCount = 0
	self._alreadyProcessedCount = 0
	
	self._processedRows = {}
end

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

function WFCatImportManifestProcessorBase:_reportProgress( messageHandler )
	
	assert( self._totalCount > 0 )
	
	messageHandler:updateProgress( self._stageName,
		( self._errorCount + self._successCount + self._alreadyProcessedCount ) / self._totalCount )
end

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

function WFCatImportManifestProcessorBase:_updateError( messageHandler, increment )
	self._errorCount = self._errorCount + increment
	self:_reportProgress( messageHandler )
end

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

function WFCatImportManifestProcessorBase:_updateSuccess( messageHandler, increment )
	self._successCount = self._successCount + increment
	self:_reportProgress( messageHandler )

end

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

function WFCatImportManifestProcessorBase:_updateAlreadyProcessed( messageHandler, increment )
	self._alreadyProcessedCount = self._alreadyProcessedCount + increment
	self:_reportProgress( messageHandler )
end

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

function WFCatImportManifestProcessorBase:_sendMessage( messageHandler, messageCode, data )
	
	messageHandler:sendMessage( {
		stage = self._stageName,
		messageCode = messageCode,
		data = data,
	} )
	
end

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

function WFCatImportManifestProcessorBase:_sendStatus( messageHandler, info )
	
	messageHandler:sendStatus( info )
	
end

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

-- Setting up the chain may result in all the processors being setup on one big
-- call stack - which leads to stackOverflow errors. Break it using a shceduler.
local function asyncObservable( observable, op, self )

	local savedSubscription
	
	return Observable.create( function( observer )
		logger:debugf( "Scheduling subscribe %s %s", op, tostring( self ) )

		WFRxScheduler.sharedScheduler():schedule( function()
			logger:debugf( "Executing subscribe %s %s", op, tostring( self ) )

			savedSubscription = observable:subscribe(
				function( ... )
					local args = { ... }
					local nArgs = select( '#', ... )					
					logger:debugf( "Scheduling onNext %s %s", op, tostring( self ) )
					
					WFRxScheduler.sharedScheduler():schedule( function()
					
						logger:debugf( "Executing onNext %s %s", op, tostring( self ) )
						observer:onNext( unpack( args, 1, nArgs ) )
						
					end )
				end,
				
				function( ... )
					local args = { ... }
					local nArgs = select( '#', ... )
					logger:debugf( "Scheduling onError %s %s", op, tostring( self ) )

					WFRxScheduler.sharedScheduler():schedule( function()
					
						logger:debugf( "Executing onError %s %s", op, tostring( self ) )
						observer:onError( unpack( args, 1, nArgs ) )
						
					end )
				end,
				
				function()
					logger:debugf( "Scheduling onCompleted %s %s", op, tostring( self ) )
					WFRxScheduler.sharedScheduler():schedule( function()
					
						logger:debugf( "Executing onCompleted %s %s", op, tostring( self ) )
						observer:onCompleted()
						
					end )
				end
			)
		
		end )
		
		return function()
			logger:debugf( "Scheduling dispose %s %s", op, tostring( self ) )

			WFRxScheduler.sharedScheduler():schedule( function()
				logger:debugf( "Executing dispose %s %s", op, tostring( self ) )
				
				if savedSubscription then
					savedSubscription:dispose()
					savedSubscription = nil
				end
			end )
		end
	end )
end

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

function WFCatImportManifestProcessorBase:_setupCacheObservble( catalog, manifestStatusDatabase, idCache, messageHandler, 
		queryId, isIdCacheNeeded, idCacheKey )
	
	local setupStream = manifestStatusDatabase:readData( queryId )
	
		:flatMap( function( results )
			
			return Observable.create( function( observer )
				
				for i, v in ipairs( results ) do
					observer:onNext( v )
				end
				
				observer:onCompleted()
			end )
			
		end )
		
		:tap( function( statusInfo )
		
			-- add to the processed cache
			self._processedRows[ statusInfo.local_id ] =  statusInfo
			
		end )
	
	if isIdCacheNeeded then
		assert( idCacheKey )
		
		setupStream = setupStream
			:bufferWithCount( kDocStoreFetchBatchSize )
			:flatMap( function( statuses )
				
				local docIds = {}
				local statusForDocId = {}
				
				for i, statusInfo in ipairs( statuses ) do
					
					if statusInfo.status == WFCatImportManifestConstants.status.success then
						docIds[ #docIds + 1 ] = statusInfo.oz_id
						statusForDocId[ statusInfo.oz_id ] = statusInfo
					end
					
					
				end
				
				-- fill the id cache for the ones which were successful
				return catalog:docStore():bulkDocValue( docIds )
			
					:tap( function( docValues )
			
						for i, docValue in ipairs( docValues ) do
						
							if not docValue.empty then
								idCache:setOzIdForLocalId( 
									idCacheKey, 
									statusForDocId[ docValue._id ].local_id, 
									docValue._id
								)
							end
						end
						
					end )
				
			end )
	end	
	
	setupStream = setupStream
		:ignoreElements() -- Just to tell explicitely that we are not using the results :)
	
	return setupStream
end

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

function WFCatImportManifestProcessorBase:setup( catalog, 
		manifestStatusDatabase, idCache, messageHandler, queryId, cacheId )
	
	local isCacheNeeded = not not cacheId
	
	local returnObservable = self:_setupCacheObservble( catalog, manifestStatusDatabase, idCache, messageHandler, 
				queryId, isCacheNeeded, cacheId )
		
		:ignoreElements()
		
		:catch( function( err )
			logger:debugf( "Error in setup %s", tostring( self ) )

			self:_sendMessage( messageHandler, self._messageCodeGenericError, { err } )
			return Observable.just( false )
		end )
		
		:defaultIfEmpty( true )
		
		:tap( function( success )
			logger:debugf( "Setup completed %s", tostring( self ) )
		end )

	return asyncObservable( returnObservable, 'setup', self )
end

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

function WFCatImportManifestProcessorBase:_maybeSetCount( manifestDatabase, queryId )
	
	return manifestDatabase:readData( queryId )
		
		:map( function( results )
			if results[ 1 ] then
				return results[ 1 ].count
			end
		end )
		:tap( function( count )
			
			assert( type( count ) == 'number' )
			self._totalCount = count
		end )

end

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

function WFCatImportManifestProcessorBase:_isAlreadyProcessed( local_id )
	
	return self._processedRows[ local_id ] 
		and self._processedRows[ local_id ].status ~= WFCatImportManifestConstants.status.pending

end

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

function WFCatImportManifestProcessorBase:process( catalog, profileCatalog, wfCatSynchronizer, 
		manifestDatabase, manifestStatusDatabase, idCache, messageHandler, contextInfo )

	-- If the child class is using this function, then
	-- it is mandatory for it to set these messageCodes
	assert( self._messageCodeCompleted )
	assert( self._messageCodeErrorCount )
	assert( self._messageCodeGenericError )

	local function completeStream( success )
	
		if success then
			messageHandler:updateProgress( self._stageName, 1, true )
		end
			
		self:_sendMessage( messageHandler, self._messageCodeCompleted, { self._totalCount } )
		
		if self._errorCount > 0 then
			self:_sendMessage( messageHandler, self._messageCodeErrorCount, { self._errorCount } )
		end
			
	end
	
	-- Set the work done to 0
	messageHandler:updateProgress( self._stageName, 0, true )
	
	local returnObservable = Observable.pcall( self.processImpObservable, 
							self, catalog, profileCatalog, wfCatSynchronizer, 
							manifestDatabase, manifestStatusDatabase, idCache, 
							messageHandler, contextInfo )
		
		:ignoreElements()
		
		:catch( function( err )
			logger:debugf( "Error in processing %s", tostring( self ) )

			if type( err ) == 'table' and err.pauseType then
				local pauseInfo = {
					status = WFCatImportManifestConstants.importStatus.paused,
					pauseType = err.pauseType,
					errorData = err.errorEntries,
					resumeContext = err.resumeContext,
					errorDecisions = derefOpt( contextInfo, "resumeInfo", "errorDecisions" )
				}
				
				self:_sendStatus( messageHandler, pauseInfo )
			else
				self:_sendMessage( messageHandler, self._messageCodeGenericError, { err } )
			end
			
			return Observable.just( false )
		end )
		
		:defaultIfEmpty( true )
		
		:tap( function( success )
			logger:debugf( "Completed processing %s", tostring( self ) )
			completeStream( success )
		end )
		
	return asyncObservable( returnObservable, 'processor', self )
end

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

function WFCatImportManifestProcessorBase:_sendSummary( messageHandler, manifestStatusDatabase, queryId, dataType )
	
	-- Currently sending only the success and total count.
	
	return manifestStatusDatabase:readData( queryId, { status = WFCatImportManifestConstants.status.success } )
		
		:map( function( results )
			if results[ 1 ] then
				return results[ 1 ].count
			end
		end )
		:tap( function( count )
			
			assert( type( count ) == 'number' )
			
			messageHandler:sendSummary( dataType, count )
		end )
end

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

return Rx.Class 'WFCatImportManifestProcessorBase'( WFCatImportManifestProcessorBase )
