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

-- Might be worth converting at some point to be a UI.CreateFrame thing
-- for now we'll just wrapper a text object

MereHealingFrames.UI = MereHealingFrames.UI or {}
MereHealingFrames.UI.Widget = MereHealingFrames.UI.Widget or {}
MereHealingFrames.UI.Widget.ShadowedText = {
	Text = nil,
    hasShadows = false,
	Shadow1 = nil,
	Shadow2 = nil,
	Shadow3 = nil,
	Shadow4 = nil,
	Shadowed = true,
	CurrentAlpha = 0,
	TextR = 1,
	TextG = 1,
	TextB = 1,
	ShadowR = 0,
	ShadowG = 0,
	ShadowB = 0,
    TextWidth = nil,
    TextHeight = nil,
    TextColourEvent = nil,
    ShadowColourEvent = nil,
    TextColourFunc = nil,
    ShadowColourFunc = nil,
	}

local ShadowedText = MereHealingFrames.UI.Widget.ShadowedText

local ShadowedTextId = 1

function ShadowedText:new(widgetName, parentFrame)
	local this = {}

	setmetatable(this, self)
	self.__index = self

	this:CreateUI(widgetName, parentFrame)
    this.ShadowTextId = "ShadowText"..ShadowedTextId
    ShadowedTextId = ShadowedTextId + 1

    this.TextColourFunc = function(handler, r, g, b, a) this:SetFontColor(r, g, b) end
    this.ShadowColourFunc = function(handler, r, g, b, a) this:SetShadowColor(r, g, b) end

	return this
end

function ShadowedText:CreateUI(widgetName, parentFrame)
	self.Text = UI.CreateFrame("Text", widgetName, parentFrame)
	self:ShadowedText(self.Shadowed)
end

function ShadowedText:GetContent()
	return self.Text
end

function ShadowedText:SetPoint(...)
	return self.Text:SetPoint(...)
end

function ShadowedText:SetLayer(textLayer)
	self.Text:SetLayer(textLayer)
end

function ShadowedText:SetFontSize(...)
	self.Text:SetFontSize(...)
end

function ShadowedText:SetWidth(textWidth)
    self.TextWidth = textWidth
	self.Text:SetWidth(textWidth)
end

function ShadowedText:GetWidth(...)
	return self.Text:GetWidth(...)
end

function ShadowedText:SetHeight(textHeight)
    self.TextHeight = textHeight
	self.Text:SetHeight(textHeight)
end

function ShadowedText:GetHeight(...)
	return self.Text:GetHeight(...)
end

function ShadowedText:SetVisible(...)
	self.Text:SetVisible(...)
end

function ShadowedText:GetVisible(...)
	return self.Text:GetVisible(...)
end

function ShadowedText:SetText(...)
	self.Text:SetText(...)
end

function ShadowedText:ShadowedText(Shadowed)
    MereHealingFrames.Debug(21, "ShadowedText: Shadow was %s, is now %s", tostring(self.Shadowed), tostring(Shadowed))
	self.Shadowed = Shadowed
	local currentText, html = self.Text:GetText()
    MereHealingFrames.Debug(21, "ShadowedText: currentText: %s", currentText)

	if (self.Shadowed) then
        self.Text:SetEffectGlow(
            {
                colorA = self.currentAlpha,
                colorB = self.ShadowB,
                colorG = self.ShadowG,
                colorR = self.ShadowR,
                strength = 3
            })
    else
        self.Text:SetEffectGlow(nil)
	end
end

function ShadowedText:SetTextColourName(colourName)
    self:SetFontColor(MereHealingFrames.Colours.FetchRGB(colourName))
    MereHealingFrames.Utils.ReplaceEventHandler(self, "TextColourEvent", Event.MereHealingFrames.Colours[colourName], self.TextColourFunc, self.ShadowTextId)

end

function ShadowedText:SetShadowColourName(colourName)
    self:SetShadowColor(MereHealingFrames.Colours.FetchRGB(colourName))
    MereHealingFrames.Utils.ReplaceEventHandler(self, "ShadowColourEvent", Event.MereHealingFrames.Colours[colourName], self.ShadowColourFunc, self.ShadowTextId)
end

function ShadowedText:SetFontColor(r, g, b)
	self.TextR = r
	self.TextG = g
	self.TextB = b
	self.Text:SetFontColor(r, g, b, self.CurrentAlpha)
end

function ShadowedText:SetShadowColor(r, g, b)
	self.ShadowR = r
	self.ShadowG = g
	self.ShadowB = b

    if (self.hasShadows) then
        self.Text:SetEffectGlow(
            {
                colorA = self.currentAlpha,
                colorB = self.ShadowB,
                colorG = self.ShadowG,
                colorR = self.ShadowR,
                strength = 3
            })
    end
end

function ShadowedText:SetAlpha(newAlpha)
	self.CurrentAlpha = newAlpha
	self.Text:SetFontColor(self.TextR, self.TextG, self.TextB, self.CurrentAlpha)
    if (self.hasShadows) then
        self.Text:SetEffectGlow(
            {
                colorA = self.currentAlpha,
                colorB = self.ShadowB,
                colorG = self.ShadowG,
                colorR = self.ShadowR,
                strength = 3
            })
    end
end