Quantcast
local T, C, L = unpack(Tukui) -- Import: T - functions, constants, variables; C - config; L - locales
if C.classbar.fulmTracker[1] ~= true or T.myclass ~= "SHAMAN" then return end

local tbWidth, tbHeight = C.classbar.fulmTracker[2], C.classbar.fulmTracker[3]

local colors = {
	{ 0/255, 75/255, 1, 1},
	{ 0/255, 100/255, 1, 1},
	{ 0/255, 125/255, 1, 1},
	{ 0/255, 150/255, 1, 1},
	{ 0/255, 175/255, 1, 1},
	{ 0/255, 200/255, 1, 1},
}


local mover = CreateFrame("Frame", "TukuiFulminationMover", UIParent)
mover:Width(tbWidth+4)
mover:Height(tbHeight+4)
if C.classbar.totemTimers then
	mover:Point("BOTTOM", TukuiTotemTimers, "TOP", 0, 6)
else
	mover:Point("TOP", UIParent, "CENTER", 0, -150)
end
mover:SetTemplate("Thin")
mover:SetBackdropBorderColor(1,0,0)
mover:SetClampedToScreen(true)
mover:SetMovable(true)
mover:SetFrameStrata("HIGH")
mover:AddText("text", "Move Fulmination Tracker", "CENTER")
mover:Hide()
tinsert(T.AllowFrameMoving, mover)

local fTracker = CreateFrame("Frame", "TukuiFulminationTracker", UIParent)
fTracker:Width(tbWidth)
fTracker:Height(tbHeight)
fTracker:Point("CENTER", mover)

local stacks = {}

for i=1,6 do
	stacks[i] = CreateFrame("Frame", "TukuiFulminationTracker_Stack"..(i), fTracker)
	stacks[i]:SetWidth((tbWidth-25) / 6)
	stacks[i]:SetHeight(tbHeight)
	stacks[i]:CreateBackdrop("Transparent")
	stacks[i].backdrop:ThickBorder()
	stacks[i].tex = stacks[i]:CreateTexture(nil, "OVERLAY")
	stacks[i].tex:SetTexture(C.media.normTex)
	stacks[i].tex:SetVertexColor(unpack(colors[i]))
	stacks[i].tex:AllPoints(stacks[i])
	if i==1 then
		stacks[i]:SetPoint("LEFT", fTracker, "LEFT", 0, 0)
	else
		stacks[i]:SetPoint("LEFT", stacks[i-1], "RIGHT", 5, 0)
	end
end

local function HasFulmination()
	local hasFulm
	for i=1, GetNumTalents(1) do
		nameTalent, icon, tier, column, currRank, maxRank= GetTalentInfo(1,i);
		if nameTalent == "Fulmination" then
			if currRank > 0 then
				hasFulm = true
			end
		end
	end
	return hasFulm
end



local function UpdateStacks(self, event, unit)
	if (event ~= "PLAYER_ENTERING_WORLD") and (unit ~= "player") then return end
	local _, _, _, numStacks, _, duration, expires = UnitBuff("player", "Lightning Shield")
	if numStacks == nil then
		numStacks = 0
	else
		numStacks = numStacks - 3
	end
	for i = 1, 6 do
		if (i <= numStacks) then
			stacks[i].tex:Show()
		else
			stacks[i].tex:Hide()
		end
	end
	if HasFulmination() then
		fTracker:Show()
	else
		fTracker:Hide()
	end
end

fTracker:RegisterEvent("UNIT_AURA")
fTracker:RegisterEvent("PLAYER_ENTERING_WORLD")
fTracker:RegisterEvent("HEALTH_REGEN_ENABLED")
fTracker:SetScript("OnEvent", UpdateStacks)

local vehicle = CreateFrame("Frame")
vehicle:RegisterEvent("VEHICLE_UPDATE")
vehicle:SetScript("OnEvent", function()
	if UnitHasVehicleUI("player") then
		fTracker:Hide()
	else
		fTracker:Show()
	end
end)