Quantcast

-- Grab a copy of the local data
local addonName, addonData = ...
local Translate = addonData.Translate

addonData.UpdateCooldown = function(self,button,fontStructure)
	local spellName = button:GetAttribute("spell")
	if (not spellName) then return end
	local spellKnown = ( GetSpellInfo(spellName) ~= nil)
	if ( not spellKnown ) then return end
	local cdStart,cdDuration,cdEnabled = GetSpellCooldown(spellName)
	if ( cdDuration < 2 ) then return end

	local fontPath, fontSize, fontFlags
	fontPath, fontSize, fontFlags = button.Counter:GetFont()
	fontSize = XMagePortsDB.CooldownSize
	button.Counter:SetFont(fontPath,XMagePortsDB.CooldownSize,fontFlags);

	local timeNow = GetTime()
	local diffTime = timeNow - cdStart
	local cdCounter = math.floor(cdDuration - diffTime)
	if ( cdStart > 0 and cdEnabled > 0 and cdDuration > 0 ) then
		button.cooldown:SetCooldown(cdStart,cdDuration)
		button.Counter:SetText(cdCounter)
	end
	button.Counter:Show()
	button.cooldown:Show()
end


addonData.CreateCooldown = function(self,button,texturePath,fontStructure)
	button.icon:SetAllPoints(button)
	button.icon:SetTexture(texturePath)
	button.icon:Show()

	button.cooldown:SetAllPoints(button)
	button.cooldown:Show()

	button.Counter = button.cooldown:CreateFontString(button:GetName().."Counter","ARTWORK",fontStructure.Template)
	fontStructure.Path,dumpVar,fontStructure.Flags = button.Counter:GetFont()
	button.Counter:SetFont(fontStructure.Path,fontStructure.Size,fontStructure.Flags)
	button.Counter:SetAllPoints(button.cooldown)
	button.Counter:SetText("")
	button.Counter:SetJustifyV("CENTER")
	button.Counter:SetJustifyH("CENTER")
	button.Counter:SetTextColor(fontStructure.Color.r,fontStructure.Color.g,fontStructure.Color.b,fontStructure.Color.a)
	button.Counter:Show()

	button.cooldown:RegisterEvent("SPELL_UPDATE_COOLDOWN")

	button.cooldown:SetScript("OnEvent",function(self,event,...) addonData:UpdateCooldown(button,fontStructure) end)
	button.cooldown:SetScript("OnUpdate",function(self,timeElapsed) addonData:UpdateCooldown(button,fontStructure) end)

	button.cooldown:Hide()
end