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

WFPerformanceReportImp.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.

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

This file should be accessed only by WFPerformanceReport.lua.

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

	-- RxLua
local Rx = require 'Rx'
local Observable = Rx.Observable

	-- WF.core.utils
local WFArguments = require 'WFArguments'
local WFCloseSupport = require 'WFCloseSupport'
local KSTableUtils = require "KSTableUtils"

	-- WF.core.rxlua
local WFRxScheduler = require 'WFRxScheduler'

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

local check = assert( WFArguments.check )

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

-- @mark WFPerformanceReportImp class

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

local WFPerformanceReportImp = {

	_perfReport = false,
	
	WFCloseSupport.addCloseSupportMethods,

}

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

function WFPerformanceReportImp:__init()

	check( 'WFPerformanceReportImp' )

	self._perfReport = {}
	
end

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

function WFPerformanceReportImp:recordSingleValue( key, value )

	check( 'WFPerformanceReportImp', '+string', 'table' )

	self._perfReport[ #self._perfReport + 1 ] = { key = key, value = value }

	return Observable.empty()

end

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

function WFPerformanceReportImp:retrieve( optClear )

	check( 'WFPerformanceReportImp', '?boolean' )

	local result = self._perfReport or {}

	if optClear then
		self._perfReport = {}
	end

	return Observable.just( result )

end

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

function WFPerformanceReportImp:clearPerfReport()

	check( 'WFPerformanceReportImp' )
	self._perfReport = {}

	return Observable.empty()

end

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

function WFPerformanceReportImp:newPerformanceReport( reportName )

	check( 'WFPerformanceReportImp', '?string' )
	
	return Rx.Observable.empty()
	
end

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

function WFPerformanceReportImp:sendFormattedMessage( optClear )

	local tempCopyReport = { messages = KSTableUtils.shallowcopy( self._perfReport ), kind = "WFMessage" }
	
	if optClear then
		self._perfReport = {}
	end
	
	if next( tempCopyReport.messages ) == nil then --don't transmit table if it is "empty"
		return Observable.empty()
	else
		return Observable.just( tempCopyReport )
	end

end

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

function WFPerformanceReportImp:sendPerfData( interval )

	check( 'WFPerformanceReportImp', 'number' )
			
	return Observable.timer( 0, interval, WFRxScheduler.sharedScheduler() )
	
		:flatMapLatest( function()
			
			return self:sendFormattedMessage( true )

		end )

end

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

return Rx.Class 'WFPerformanceReportImp'( WFPerformanceReportImp )
