-- 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.HotIconFactory = {}
MereHealingFrames.HotIconFactory.Bucket = {}


function MereHealingFrames.HotIconFactory.New(ParentFrame)
	if #MereHealingFrames.HotIconFactory.Bucket ~= 0 then
		local hotIcon = table.remove(MereHealingFrames.HotIconFactory.Bucket)
		return hotIcon:ReParent(ParentFrame)
	else
		return MereHealingFrames.HotIcon:new(ParentFrame)
	end
end

function MereHealingFrames.HotIconFactory.Delete(hotIcon)
	hotIcon:Reset()
	return table.insert(MereHealingFrames.HotIconFactory.Bucket, hotIcon)
end

MereHealingFrames.HotIcon = {
	Name = "HotIcon",
	Height = 20,
	Width = 20,
	ParentFrame = nil,
	BuffName = nil,
	DebuffName = nil,
	Anchor = "CENTER",
	AnchorTarget = "TOPRIGHT",
	AnchorX = 0,
	AnchorY = 0,
	AnchorFrame = nil,
	BorderSize = 1,
	Buff = nil,
	DeBuff = nil,
	OnlyMySpells = true,
	CounterSize = nil,
	TimerLeft = nil,
	BuffSlot = 0,
    framesLoaded = false,
    LayoutOffset = 0,
    isVisible = false,
	}

function MereHealingFrames.HotIcon:new (ParentFrame)
	if not ParentFrame then
		error("ParentFrame not provided")
		return
	end

	local this = {}

	setmetatable(this, self)
	self.__index = self

	this:CreateUI(ParentFrame)

	return this
end

function MereHealingFrames.HotIcon:CreateUI(ParentFrame)
	self.ParentFrame = ParentFrame

    local requestFrames = {
        Frame = 1,
        Texture = 1,
    }
    Library.LibAsyncFrames.AyncLoadFrames(requestFrames,
        function (...) MereHealingFrames.HotIcon.AsyncUICallback(self, ...) end,
        ParentFrame, "MereHealingFrames")
end

function MereHealingFrames.HotIcon:AsyncUICallback(frames)
    self.Frame = frames:CreateFrame("Frame", "Background", self.ParentFrame)
    self.Icon = frames:CreateFrame("Texture", "HotIcon", self.Frame)
    self.Counter = MereHealingFrames.UI.Widget.ShadowedText:new("IconCounter", self.Frame)
    self.Timer = MereHealingFrames.UI.Widget.ShadowedText:new("IconTimer", self.Frame)
    self.framesLoaded = true

    self:Initialize(self.LayoutOffset)

    if (self.icon ~= nil) then
        self.Icon:SetTexture("Rift", self.icon)
    end
    self:UpdateBorderColor(self.buffState)
    self:UpdateCounter(self.buffStacks)
end

function MereHealingFrames.HotIcon:ReParent(ParentFrame)
	self.ParentFrame = ParentFrame
    if self.framesLoaded then
	    self.Frame:SetParent(ParentFrame)
    end
	return self
end

function MereHealingFrames.HotIcon:Initialize(LayoutOffset)

	self.AnchorFrame = self.AnchorFrame or self.ParentFrame
    self.LayoutOffset = LayoutOffset
    if not self.framesLoaded then return end

	self.Frame:SetBackgroundColor(0,0,0,0)
	self.Frame:SetPoint(self.Anchor, self.AnchorFrame, self.AnchorTarget, self.AnchorX, self.AnchorY)
	self.Frame:SetWidth(self.Width)
	self.Frame:SetHeight(self.Height)
	self.Frame:SetLayer(MereHealingFrames.Layers.CalculateLayer(LayoutOffset, "HotIconBase"))

	self.Icon:SetPoint("CENTER", self.Frame, "CENTER")
	self.Icon:SetWidth(self.Width - (self.BorderSize * 2))
	self.Icon:SetHeight(self.Height - (self.BorderSize * 2))
	self.Icon:SetLayer(MereHealingFrames.Layers.CalculateLayer(LayoutOffset, "HotIconIcon"))

	self.Counter:SetPoint("TOPRIGHT", self.Icon, "TOPRIGHT", 2, -4)
	self.Counter:SetLayer(MereHealingFrames.Layers.CalculateLayer(LayoutOffset, "HotIconText"))

	self.Timer:SetPoint("BOTTOMRIGHT", self.Icon, "BOTTOMRIGHT", 2, 3)
	self.Timer:SetLayer(MereHealingFrames.Layers.CalculateLayer(LayoutOffset, "HotIconText"))

	local textSize = MereHealingFrames.Config.UI.Utilities.CalcPointFromPixelsIgnoreHang(self.Height/2)

	MereHealingFrames.Debug(4, "Hot Text Size: %d, pixel %d", textSize, self.Height/2)
	self.Counter:SetFontSize(textSize)
	self.Counter:SetText("")
	self.Counter:SetFontColor(1,1,1)

	self.Timer:SetFontSize(textSize)
	self.Timer:SetText("")
	self.Timer:SetFontColor(1,1,1)

	self:SetVisible(self.isVisible)
end

function MereHealingFrames.HotIcon:Reset()
	self:SetVisible(false)
    for key, value in pairs(MereHealingFrames.HotIcon) do
        self[key] = nil
    end
	self.ParentFrame = MereHealingFrames.context
end

function MereHealingFrames.HotIcon:Clear()
	self:SetVisible(false)
end

function MereHealingFrames.HotIcon:UpdateIcon(buffName, buffStacks, buffRemaining, buffState)
	local cacheEntry = MereHealingFrames.BuffCache.Cache[buffName]

    self.buffName = buffName
    self.buffStacks = buffStacks
    self.buffState = buffState
    self.icon = cacheEntry.icon

    if not self.framesLoaded then return end

    self.Icon:SetTexture("Rift", self.icon)
	self:UpdateBorderColor(self.buffState)
	self:UpdateCounter(self.buffStacks)
	self:SetVisible(true)
end

local stringFormat = string.format
local mathFloor = math.floor
function MereHealingFrames.HotIcon:UpdateTimer(TimeLeft)
	if not self.framesLoaded or TimeLeft == nil or TimeLeft < 0 then
		--self.Timer:SetAlpha(0)
		MereHealingFrames.Debug(9, "Hoticon has expired, expect to be removed")
		return
	end

	--self.Timer:SetAlpha(1)

	local timeString
	TimeLeft = mathFloor(TimeLeft)
	MereHealingFrames.Debug(9, "HotIcon Timer: %s", tostring(TimeLeft))

	if self.TimerLeft == TimeLeft then
		return
	end

	if (TimeLeft > 60) then
		timeString = stringFormat("%dm", mathFloor(TimeLeft / 60))
	else
		timeString = stringFormat("%ds", TimeLeft)
	end
	MereHealingFrames.Debug(9, "HotIcon Timer: %s", timeString)
	self.Timer:SetText(timeString)
end

function MereHealingFrames.HotIcon:UpdateCounter(counterSize)
	MereHealingFrames.Debug(9, "stack size: %s", tostring(counterSize))
	if counterSize == nil or counterSize <= 1 then
		self.Counter:SetText("")
		self.CounterSize = 0
		return
	end

	if self.CounterSize ~= counterSize then
		self.CounterSize = counterSize
		self.Counter:SetText(tostring(counterSize))
	end
end

function MereHealingFrames.HotIcon:UpdateBorderColor(buffState)

    if not self.framesLoaded then return end

	self.Frame:SetBackgroundColor(0,0,0,1)

	if (buffState == "buff") then
		self.Frame:SetBackgroundColor(0,0,0,1)
	elseif buffState == "debuff" then
		self.Frame:SetBackgroundColor(1,0,0,1)
	elseif buffState == "blocked" then
		self.Frame:SetBackgroundColor(1,1,0,1)
	elseif buffState == "cleansable" then
		self.Frame:SetBackgroundColor(149/255,13/255,235/255,1)
	end
end

function MereHealingFrames.HotIcon:SetVisible(visible)
    self.isVisible = visible

    if not self.framesLoaded then return end

	self.Frame:SetVisible(visible)
end