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

WFSQLiteCorruptionLogger.lua

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

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

------------------------------------------------------------------------------]]
	
local WFArguments = require "WFArguments"

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

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

local logger = require "WFLogger"( "WFSQLiteCorruptionLogger" )

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

local _wfSQLiteCorruptionLoggerThread

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

local function getWFSQLiteCorruptionLoggerThread()

	if not _wfSQLiteCorruptionLoggerThread then

		logger:info( "creating WFSQLiteCorruptionLoggerImp thread" )

	end

	_wfSQLiteCorruptionLoggerThread = 
		_wfSQLiteCorruptionLoggerThread or
		WFWorkerThread( "WFSQLiteCorruptionLoggerImp.lua" )

	return _wfSQLiteCorruptionLoggerThread

end

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

local function maybeLoadWFSQLiteCorruptionLogger_private_fireCorruptionEvent()

	local ok, maybeWFSQLiteCorruptionLogger_private =
				pcall( require, "WFSQLiteCorruptionLogger_private" )

	if not ok then

		return maybeWFSQLiteCorruptionLogger_private

	end

	local WFSQLiteCorruptionLogger_private = 
			maybeWFSQLiteCorruptionLogger_private 

	if type( WFSQLiteCorruptionLogger_private.fireCorruptionEvent ) ~= 
			"function" then

		return "no WFSQLiteCorruptionLogger_private.fireCorruptionEvent function!"

	end

	return WFSQLiteCorruptionLogger_private.fireCorruptionEvent

end

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

local WFSQLiteCorruptionLogger_private_fireCorruptionEvent

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

local function fireCorruptionEvent( errorType, path, msg, sql )

	WFSQLiteCorruptionLogger_private_fireCorruptionEvent =
		WFSQLiteCorruptionLogger_private_fireCorruptionEvent or
		maybeLoadWFSQLiteCorruptionLogger_private_fireCorruptionEvent()

	if type( WFSQLiteCorruptionLogger_private_fireCorruptionEvent ) ~= 
			"function" then

		logger:warnf( "Will not log corruption event because: %s. Corruption event was: %s, %s, %s, %s", 
						WFSQLiteCorruptionLogger_private_fireCorruptionEvent, 
						errorType, path, msg, sql )

		return

	end

	local ok, didFireOrErrStr =
		pcall( WFSQLiteCorruptionLogger_private_fireCorruptionEvent,
				"WFSQLiteCorruptionLoggerImp.lua", errorType, path, msg, sql )

	if not ok then

		logger:warnf( "Will not log corruption event because: %s. Corruption event was: %s, %s, %s, %s", 
						didFireOrErrStr, errorType, path, msg, sql )

		return

	end

	local didFire = didFireOrErrStr

	if not didFire then

		logger:warnf( "Will not log corruption event, probably because the WFSQLiteCorruptionLoggerImp was not running due to the exception occuring before there were any listeners. Corruption event was: %s, %s, %s, %s", 
						didFireOrErrStr, errorType, path, msg, sql )

		return

	end

	logger:infof( "Fired corruption event: %s, %s, %s, %s", 
					errorType, path, msg, sql )

end

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

local function addDb( wfSQLiteDBImpInstance )

	WFArguments.check( "userdata" )

	logger:infof( "Adding corruption hook to %s", wfSQLiteDBImpInstance )

	local hadHandlers = 
			wfSQLiteDBImpInstance
				:setFatalErrorHook( fireCorruptionEvent );

	if hadHandlers then

		logger:warnf( "Clobbered existing error handlers on %s", 
						wfSQLiteDBImpInstance )

	end

	return wfSQLiteDBImpInstance

end

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

local function forwardToWorkerThread( apiName )

	return function( ... )

		return getWFSQLiteCorruptionLoggerThread()
			:request( apiName, ... )

	end

end

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

return {

	addDb = addDb,

	getCorruptionEventStream = 
		forwardToWorkerThread( "getCorruptionEventStream" ),

	private = {

		workerThreadIsActiveStream = 
			forwardToWorkerThread( "workerThreadIsActiveStream" ),

	},

}
