-- This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
-- To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
-- or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
local MereHealingFrames, privateVars = ...

MereHealingFrames.ToolTip = { showToolTips = false }

function MereHealingFrames.ToolTip:new()
	local this = {}

	setmetatable(this, self)
	self.__index = self
	return this
end

function MereHealingFrames.ToolTip.Initialize(context)

	local tooltip = MereHealingFrames.ToolTip:new()
	tooltip.Frame = UI.CreateFrame("Frame", "HealingPanelToolTip", context)
	tooltip.Frame:SetVisible(false)
	tooltip.Frame:SetBackgroundColor(0,0,0,0.5)
	tooltip.Frame:SetLayer(MereHealingFrames.Layers.ToolTipBase)
	tooltip.Frame:SetWidth(400)
	tooltip.Frame:SetHeight(300)

	tooltip.Text = UI.CreateFrame("Text", "HealingPanelToolTipText", tooltip.Frame)
	tooltip.Text:SetLayer(MereHealingFrames.Layers.ToolTipText)
	tooltip.Text:SetWordwrap(true)

	tooltip.Text:SetPoint("TOPLEFT", tooltip.Frame, "TOPLEFT", 1, 1)
	tooltip.Text:SetPoint("BOTTOMRIGHT", tooltip.Frame, "BOTTOMRIGHT", -1, -1)

	MereHealingFrames.HealingPanelToolTip = tooltip
end

function MereHealingFrames.ToolTip.Show(panel)
	if (MereHealingFrames.ToolTip.showToolTips) then
		MereHealingFrames.HealingPanelToolTip:ShowText(panel)
	end
end

function MereHealingFrames.ToolTip.Hide()
	MereHealingFrames.HealingPanelToolTip:HideText()
end

function MereHealingFrames.ToolTip:ShowText(panel)
	local text = panel:GetToolTipText()
	self.Text:SetText(text)

	self:ResizeToText()
	self.Frame:SetPoint("CENTERLEFT", panel.Frame, "CENTERRIGHT")
	self.Frame:SetVisible(true)
end

function MereHealingFrames.ToolTip:HideText()
	self.Frame:SetVisible(false)
end

function MereHealingFrames.ToolTip:ResizeToText()
	self.Text:ClearAll()
	local TextWidth = math.min(400, self.Text:GetWidth())
	self.Text:SetWidth(TextWidth)

	local WrappedTextHeight = math.min(300, self.Text:GetHeight())

	self.Text:ClearAll()
	self.Text:SetPoint("TOPLEFT", self.Frame, "TOPLEFT", 1, 1)
	self.Text:SetPoint("BOTTOMRIGHT", self.Frame, "BOTTOMRIGHT", -1, -1)

	self.Frame:SetWidth(TextWidth + 2)
	self.Frame:SetHeight(WrappedTextHeight + 2)
end