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

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},
}

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

local total = 0

local mover = CreateFrame("Frame", "TukuiThunderclapMover", UIParent)
mover:Width(tbWidth+4)
mover:Height(tbHeight+4)
mover:Point("TOP", UIParent, "CENTER", 0, -150)
mover:SetTemplate("Thick")
mover:SetBackdropBorderColor(1,0,0)
mover:SetClampedToScreen(true)
mover:SetMovable(true)
mover:SetFrameStrata("HIGH")
mover:AddText("text", "Move Thunderclap Tracker", "CENTER")
mover:Hide()
tinsert(T.AllowFrameMoving, mover)

local tClap = CreateFrame("Frame", "TukuiThunderclapTracker", UIParent)
tClap:Width(tbWidth)
tClap:Height(tbHeight)
tClap:Point("CENTER", mover)
tClap:CreateBackdrop("Transparent")


local stacks = {}

for i=1,3 do
	stacks[i] = CreateFrame("Frame", "TukuiThunderclap_Stack"..i, tClap)
	stacks[i]:SetWidth((tbWidth-2) / 3)
	stacks[i]:SetHeight(tbHeight)
	stacks[i]:CreateBackdrop("Transparent")
	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", tClap, "LEFT", 0, 0)
	else
		stacks[i]:SetPoint("LEFT", stacks[i-1], "RIGHT", 5, 0)
	end
	stacks[i].tex:Hide()
end

local function HasThunderstruckTalent()
	local hasThunderstruck
	for i=1, GetNumTalents(3) do
		nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(3,i);
		if nameTalent == "Thunderstruck" then
			if currRank > 0 then
				hasThunderstruck = true
			end
		end
	end
	return hasThunderstruck
end

local function UpdateStacks()
	local _, _, _, tClapStacks, _, tClapDuration, tClapExpires = UnitBuff("player", "Thunderstruck")
	if tClapStacks == nil then tClapStacks = 0 end
	for i = 1, 3 do
		if (i <= tClapStacks) then
			stacks[i].tex:Show()
		else
			stacks[i].tex:Hide()
		end
	end
end

-- Red border if target is missing rend, then shows blue if 3 stacks of thunderclap, then shows green if thunderclap is off CD, then shows regular border
local function UpdateBorder()
	if not InCombatLockdown() then return end
	local _, _, _, _, _, rendDuration, rendExpires = UnitDebuff("target", "Rend")
	local _, _, _, tClapStacks, _, tClapDuration, tClapExpires = UnitBuff("player", "Thunderstruck")
	local tClapUsable = GetSpellCooldown("Thunderclap")
	if not rendDuration then
		tClap.backdrop:SetBackdropBorderColor(1, 0, 0)
	elseif tClapStacks == 3 then
		tClap.backdrop:SetBackdropBorderColor(0, 1, 0)
	elseif tClapUsable then
		tClap.backdrop:SetBackdropBorderColor(0, 1, 1)
	else
		tClap.backdrop:SetTemplate("ThickTransparent")
	end
end

local function UpdateFrameState()
	if HasThunderstruckTalent() then
		tClap:Show()
		tClap:SetScript("OnUpdate", OnUpdate)
	else
		tClap:Hide()
		tClap:SetScript("OnUpdate", nil)
	end
end

local function OnEvent(self, event, ...)
	if event == "PLAYER_ENTERING_WORLD" or event == "PLAYER_TALENT_UPDATE" or event == "ACTIVE_TALENT_GROUP_cHANGED" then
		UpdateFrameState()
	elseif event == "UNIT_AURA" then
		UpdateStacks()
		UpdateBorder()
	elseif event == "PLAYER_TARGET_CHANGED" then
		UpdateBorder()
	end
end

tClap:RegisterEvent("PLAYER_ENTERING_WORLD")
tClap:RegisterEvent("PLAYER_TALENT_UPDATE")
tClap:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
tClap:RegisterEvent("PLAYER_TARGET_CHANGED")
tClap:RegisterEvent("UNIT_AURA")
tClap:SetScript("OnEvent", OnEvent)

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