---------------------------------------------------------------------------------------------------
-- Addon Manager
-- Tooltip
---------------------------------------------------------------------------------------------------
local moduleName = 'Tooltip'
local module = {}
AddonManager[ moduleName ] = module
---------------------------------------------------------------------------------------------------
local RegisterEventHandler, UnRegisterEventHandler = common.RegisterEventHandler, common.UnRegisterEventHandler
local FormatInt = common.FormatInt
local SendEvent, ToWString = userMods.SendEvent, userMods.ToWString
---------------------------------------------------------------------------------------------------
module.GetWidgets = function( self )
	self.widget = mainForm:GetChildChecked( 'Tooltip', false )
	self.container = self.widget:GetChildChecked( 'Container', false )
	
	self.header = self.widget:GetChildChecked( 'SmartLine', false )
	self.smartLineDesc = self.header:GetWidgetDesc()
	
	self.desc = mainForm:CreateWidgetByDesc( self.smartLineDesc )
	self.desc:SetMultiline( true )
	self.desc:SetFormat( ToWString( '<body><tip_golden><r name = \'value\'/></tip_golden></body>' ) )
	
	self.separator = self.widget:GetChildChecked( 'Separator', false )
	self.memUsage = mainForm:CreateWidgetByDesc( self.smartLineDesc )
	self.memUsage:SetFormat( ToWString( '<body><tip_blue><r name = \'value1\'/>: <r name = \'value2\'/>KB</tip_blue></body>' ) )
end

module.GetHandlers = function( self )
	self.handlers = {
		[ 'EVENT_SECOND_TIMER' ] = function() -- old
			SendEvent( 'U_EVENT_ADDON_MEM_USAGE_REQUEST', {} )
			SendEvent( 'SCRIPT_ADDON_MEM_REQUEST', { target = self.target } )
		end,
		
		[ 'SCRIPT_ADDON_MEM_RESPONSE' ] = function( params )
			if params.sender ~= self.target then return end
			if not self.response then
				self.container:PushBack( self.separator )
				self.container:PushBack( self.memUsage )
				self.separator:Show( true )
				self.memUsage:Show( true )
				self.response = true
				if not self.isPlacementUpdated then -- ugly :( again...
					self:SetPlacement( self.sender )
					self.isPlacementUpdated = true
				end
			end
			self.memUsage:SetVal( 'value2', FormatInt( params.memUsage, '%d' ) )
		end,
	}
	self.handlers[ 'U_EVENT_ADDON_MEM_USAGE_RESPONSE' ] = self.handlers[ 'SCRIPT_ADDON_MEM_RESPONSE' ]
end

module.ToggleHandlers = function( self, state )
	local foo = state and RegisterEventHandler or UnRegisterEventHandler
	for eventId, handler in self.handlers do
		if eventId and handler then foo( handler, eventId ) end
	end
end

module.SetPlacement = function( self, widget )
	local placement = self.widget:GetPlacementPlain()
	local source = widget and widget:GetRealRect()
	if source then
		local rect, frameRect = self.widget:GetRealRect(), mainForm:GetRealRect()
		rect.sizeX = rect.x2 - rect.x1
		rect.sizeY = rect.y2 - rect.y1
		frameRect.sizeX = frameRect.x2 - frameRect.x1
		frameRect.sizeY = frameRect.y2 - frameRect.y1
		local placeX = frameRect.x2 - source.x2
		local placeY = frameRect.y2 - source.y1
		placement.alignX = WIDGET_ALIGN_LOW_ABS
		placement.alignY = WIDGET_ALIGN_LOW_ABS
		placement.posX = placeX > rect.sizeX and source.x2 or source.x1 - rect.sizeX
		placement.posY = ( frameRect.y2 - source.y1 ) > rect.sizeY and source.y1 or ( source.y2 > rect.sizeY and source.y2 - rect.sizeY or ( frameRect.sizeY - rect.sizeY ) / 2 )
	else
		placement = self.defaultPlacement
	end
	self.widget:SetPlacementPlain( placement )
end

module.Show = function( self, params )
	if params.header then
		self.target = params.header
		self.header:SetVal( 'value', ToWString( self.target ) )
		self.container:PushBack( self.header )
		self.header:Show( true )
	end
	if params.desc then
		self.desc:SetVal( 'value', ToWString( params.desc ) )
		self.container:PushBack( self.desc )
		self.desc:Show( true )
	end
	self.memUsage:SetVal( 'value1', AddonManager[ 'LStrings' ][ 'Memory Usage' ] )
	self.sender = params.widget
	self:ToggleHandlers( true )
	self.handlers[ 'EVENT_SECOND_TIMER' ]()
	self:SetPlacement( self.sender )
	self.widget:Show( true )
end

module.Reset = function( self )
	self.widget:Show( false )
	self:ToggleHandlers( false )
	for _, widget in { 'header', 'desc', 'separator', 'memUsage' } do
		if self[ widget ] then self[ widget ]:Show( false ) end
	end
	self.container:RemoveItems()
	self.response = nil
	self.sender = nil
	self.isPlacementUpdated = nil
	self.header:ClearValues()
	self.desc:ClearValues()
	self.memUsage:ClearValues()
end

module.Init = function( self )
	self:GetWidgets()
	self:GetHandlers()
	self.defaultPlacement = self.widget:GetPlacementPlain()
end
---------------------------------------------------------------------------------------------------