Quantcast
local T, C, L = unpack(Tukui)

local oUF = oUFTukui or oUF

local mult = T.mult
local inset = 0
local noinset = C.media.noinset
if noinset then inset = mult end
T.backdrop = {
	bgFile = C["media"].blank,
	edgeFile = C["media"].blank,
	tile = false, tileSize = 0, edgeSize = mult,
	insets = { left = -mult + inset + inset, right = -mult + inset + inset, top = -mult + inset + inset, bottom = -mult + inset + inset}
}

function T.GetPixelFont()
	return C.media.pixelfont, 8, "MONOCHROMEOUTLINE"
end

function T.GetRegularFont()
	return C.media.font, 12, "OUTLINE"
end

function T.GetBarTexture()
	return C.unitframes.FlatBars and C.media.blank or C.media.normTex
end

------------------------------------------------------------------
-- General Functions ---------------------------------------------
------------------------------------------------------------------

function T.FormatTime(s)
	local day, hour, minute = 86400, 3600, 60
	if s >= day then
		return format("%dd", ceil(s / day))
	elseif s >= hour then
		return format("%dh", ceil(s / hour))
	elseif s >= minute then
		return format("%dm", ceil(s / minute))
	elseif s >= minute / 12 then
		return floor(s)
	end
	return format("%.1f", s)
end


function T.ColorGradient(perc, ...)
	-- Translate divison by zeros into 0, so we don't blow select.
	-- We check perc against itself because we rely on the fact that NaN can't equal NaN.
	if(perc ~= perc or perc == math.hug) then perc = 0 end

	if perc >= 1 then
		local r, g, b = select(select('#', ...) - 2, ...)
		return r, g, b
	elseif perc <= 0 then
		local r, g, b = ...
		return r, g, b
	end

	local num = select('#', ...) / 3
	local segment, relperc = math.modf(perc*(num-1))
	local r1, g1, b1, r2, g2, b2 = select((segment*3)+1, ...)

	return r1 + (r2-r1)*relperc, g1 + (g2-g1)*relperc, b1 + (b2-b1)*relperc
end

------------------------------------------------------------------
-- Unitframe Functions -------------------------------------------
------------------------------------------------------------------

function T.UpdateDetailColor(self)
	if not self.Detail then return end
	local r, g, b = 1, 1, 1
	local _, class = UnitClass(self.unit)

	if not class or not UnitIsPlayer(self.unit) then
		local reaction = UnitReaction(self.unit, "player")
		if not reaction then reaction = 5 end
		local c = T.UnitColor.reaction[reaction]
		r, g, b = c[1], c[2], c[3]
	else
		local c = T.UnitColor.class[class]
		r, g, b = c[1], c[2], c[3]
	end
	self.Detail:SetBackdropColor(r, g, b, C.unitframes.fillAlpha)
end

function T.GetUnitConfig(unit)
	local enable = C.unitframes.Units[unit].enable
	local width = C.unitframes.Units[unit].width
	local height = C.unitframes.Units[unit].height
	return enable, width, height
end

function T.ReverseBar(bar, unit, min, max, colorTable)
	if (bar:GetParent():GetCenter()) < (UIParent:GetWidth()/2) then return end --only continue if unit frame is on right side of screen

	if UnitIsDead(unit) or UnitIsGhost(unit) then
		bar:SetValue(max)
	else
		bar:SetValue(max-min)
	end
	if not bar.fill then bar:CreateFiller() end
	bar.fill:SetVertexColor(unpack(colorTable))
	bar:SetStatusBarColor(0, 0, 0, 0)
end

T.GetHealthColor = function(self, unit)
	local r, g, b, a = 0, 0, 0, 1
	if C.unitframes.unicolor == true then
		local c = C.unitframes.statusbarcolor
		r, g, b, a = c[1], c[2], c[3], (c[4] or 1)
	else
		if (C["unitframes"].enemyhcolor and UnitIsEnemy(unit, "player") and UnitIsPlayer(unit)) or (not UnitIsPlayer(unit) and UnitIsFriend(unit, "player")) or (not select(2, UnitClass(unit))) then
			local c = T.UnitColor.reaction[UnitReaction(unit, "player") or 5]
			r, g, b = c[1], c[2], c[3]
		else
			local c = T.UnitColor.class[select(2, UnitClass(unit))]
			r, g, b = c[1], c[2], c[3]
		end
	end

	return r, g, b, a
end
T.PostUpdateHealth = function(self, unit, min, max)
	local status = not UnitIsConnected(unit) and "DC" or UnitIsDead(unit) and "Dead" or UnitIsGhost(unit) and "Ghost"
	if self.value then
		if status then
			if status == "Dead" or "Ghost" then
				self:SetValue(0)
			end
			self.value:SetText("<"..status..">")
		elseif min == max then
			self.value:SetText("")
		else
			local r, g, b = oUF.ColorGradient(min/max, 0.69, 0.31, 0.31, 0.65, 0.63, 0.35, 0.33, 0.59, 0.33)
			self.value:SetFormattedText("|cff%02x%02x%02x%s|r", r*255, g*255, b*255, T.ShortValue(min))
		end
	end

	self:SetStatusBarColor(T.GetHealthColor(self, unit))
	T.ReverseBar(self, unit, min, max, {T.GetHealthColor(self, unit)})
end


function T.GetPowerColor(self, unit)
	local r, g, b, a = 0, 0, 0, 1
	if C.unitframes.unicolor then
	end
end
T.PostUpdatePower = function(power, unit, min, max)
	local self = power:GetParent()
	local pType, pToken = UnitPowerType(unit)
	local color
	--Overwrite oUF colors with Tukui's colors for consistency
	if power.colorTapping and UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit) then
		color = T.UnitColor.tapped
	elseif(power.colorDisconnected and not UnitIsConnected(unit)) then
		color = T.UnitColor.disconnected
	elseif power.colorPower then
		color = pToken and T.UnitColor.power[pToken]
	elseif(power.colorClass and UnitIsPlayer(unit)) or
	(power.colorClassNPC and not UnitIsPlayer(unit)) or
	(power.colorClassPet and UnitPlayerControlled(unit) and not UnitIsPlayer(unit)) then
		local _, class = UnitClass(unit)
		color = class and T.UnitColor.class[class]
	elseif(power.colorReaction and UnitReaction(unit, 'player')) then
		color = T.UnitColors.reaction[UnitReaction(unit, "player")]
	end
	if not color then color = {1, 1, 1} end
	local r, g, b = color[1], color[2], color[3]

	if power.value then
		local tColor = {1, 1, 1}
		if power.value.colorPower then
			tColor = pToken and T.UnitColor.power[pToken]
		end
		power.value:SetTextColor(tColor[1], tColor[2], tColor[3])

		if not UnitIsPlayer(unit) and not UnitPlayerControlled(unit) or not UnitIsConnected(unit) then
			power.value:SetText("")
		elseif UnitIsDead(unit) or UnitIsGhost(unit) then
			power.value:SetText("")
		elseif min == max then
			power.value:SetText("")
		else
			power.value:SetText(T.ShortValue(min))
		end
	end

	T.ReverseBar(power, unit, min, max, {r, g, b})
end

function T.KillClassBars(self)
	local toSkin = {
		self.CPoints,
		self.EclipseBar,
		self.SoulShards,
		self.HolyPower,
		self.Runes,
		self.TotemBar,
	}

	for _,frame in pairs(toKill) do
		if frame then
			if frame[1] then
				for i=1, #frame do
					if frame[i].SetTemplate then frame[i]:SetTemplate(); frame[i]:ThickBorder() end
				end
			end
			if frame.Kill then frame:Kill() end
		end
	end
end

function T.SkinAura(self, button)
	button:ThickBorder()
	button.count:SetFont(T.GetPixelFont())
	button.count:SetShadowOffset(0, 0)
	button.remaining:SetFont(T.GetPixelFont())
	button.remaining:SetShadowOffset(0, 0)
	button.Glow:Kill()
end

T.classFuncs = {}

function T.classFuncs.SHAMAN(self)
	if not self.TotemBar then return end

	local tb = self.TotemBar
	for i=1, 4 do
		tb[i]:SetBackdrop(nil)
		tb[i]:CreateBackdrop()
		tb[i].backdrop:ThickBorder()
		tb[i]:SetWidth((self.width-19)/4)
		tb[i]:SetHeight(5)
		tb[i]:ClearAllPoints()
	end
	tb[2]:SetPoint("BOTTOMLEFT", self, "TOPLEFT", 2, 5)
	tb[1]:SetPoint("LEFT", tb[2], "RIGHT", 5, 0)
	tb[3]:SetPoint("LEFT", tb[1], "RIGHT", 5, 0)
	tb[4]:SetPoint("LEFT", tb[3], "RIGHT", 5, 0)
end

function T.classFuncs.PALADIN(self)
	if not self.HolyPower then return end

	local hp = self.HolyPower
	hp:SetTemplate()
	hp:ThickBorder()
	hp:SetHeight(9)
	hp:SetWidth(self.width)
	hp:ClearAllPoints()
	hp:SetPoint("BOTTOM", self, "TOP", 0, 3)

	for i=1, 3 do
		hp[i]:ClearAllPoints()
		if i == 1 then
			hp[i]:SetPoint("LEFT", hp, "LEFT", 2, 0)
		else
			hp[i]:SetPoint("LEFT", hp[i-1], "RIGHT", 1, 0)
		end
		hp[i]:SetWidth((self.width-6)/3)
		hp[i]:SetHeight(hp:GetHeight()-4)
	end
end

function T.classFuncs.WARLOCK(self)
	if not self.SoulShards then return end

	local ss = self.SoulShards
	ss:SetTemplate()
	ss:ThickBorder()
	ss:SetHeight(9)
	ss:SetWidth(self.width)
	ss:ClearAllPoints()
	ss:SetPoint("BOTTOM", self, "TOP", 0, 3)

	for i=1, 3 do
		ss[i]:ClearAllPoints()
		if i == 1 then
			ss[i]:SetPoint("LEFT", ss, "LEFT", 2, 0)
		else
			ss[i]:SetPoint("LEFT", ss[i-1], "RIGHT", 1, 0)
		end
		ss[i]:SetWidth((self.width-6)/3)
		ss[i]:SetHeight(ss:GetHeight()-4)
	end
end

function T.classFuncs.DEATHKNIGHT(self)
	if not self.Runes then return end

	local rs = self.Runes
	rs:SetTemplate()
	rs:ThickBorder()
	rs:SetHeight(9)
	rs:SetWidth(self.width)
	rs:ClearAllPoints()
	rs:SetPoint("BOTTOM", self, "TOP", 0, 3)

	for i=1, 6 do
		rs[i]:ClearAllPoints()
		if i == 1 then
			rs[i]:SetPoint("LEFT", rs, "LEFT", 2, 0)
		else
			rs[i]:SetPoint("LEFT", rs[i-1], "RIGHT", 1, 0)
		end
		rs[i]:SetWidth((self.width-9)/6)
		rs[i]:SetHeight(rs:GetHeight()-4)
	end
end

function T.classFuncs.DRUID(self)
	if not self.EclipseBar then return end

	local eb = self.EclipseBar
	eb:ClearAllPoints()
	eb:SetPoint("BOTTOM", self, "TOP", 0, 3)
	eb:ThickBorder()
	eb:SetTemplate()
	eb:SetWidth(self.width)
	eb.LunarBar:ClearAllPoints()
	eb.LunarBar:SetPoint('LEFT', eb, 'LEFT', 2, 0)
	eb.LunarBar:SetHeight(eb:GetHeight()-4)
	eb.LunarBar:SetWidth(eb:GetWidth()-4)
	eb.SolarBar:SetHeight(eb:GetHeight()-4)
	eb.SolarBar:SetWidth(eb:GetWidth()-4)

	-- local dmb =
	self.DruidManaBackground:Hide()
	-- dmb:ClearAllPoints()
	-- dmb:SetPoint("BOTTOM", self, "TOP", 0, 3)
	-- dmb:SetTemplate()
	-- dmb:ThickBorder()
	-- dmb:SetWidth(self.width)
	-- dmb:SetHeight(9)

	local dm = self.DruidMana
	dm:AllPoints(dmb, 2)
	dm:SetWidth(self.width-4)
	dm:SetPoint("BOTTOM", self, "TOP", 0, 5)
	dm:CreateBackdrop()
	dm.backdrop:ThickBorder()
end

function T.classFuncs.GENERAL(self)
	if TukuiCombo then
		local parent = TukuiCombo:GetParent()
		local parentUnit = parent=="TukuiPlayer" and "player" or "target"

		local tc = TukuiCombo
		tc:ClearAllPoints()
		tc:SetPoint("BOTTOM", self, "TOP", 0, 3)
		tc:SetWidth(self.width)
		tc:SetHeight(9)

		tc:SetBackdrop(nil)

		for i=1, 5 do
			tc[i]:CreateBackdrop("Transparent")
			tc[i].backdrop:ThickBorder()
			tc[i]:SetMinMaxValues(0, 1)
			tc[i]:SetValue(0)
			tc[i]:SetAlpha(1)

			tc[i]:SetWidth((self.width-24)/5)
			tc[i]:SetHeight(5)


			tc[i]:ClearAllPoints()
			if i == 1 then
				tc[i]:SetPoint("LEFT", tc, "LEFT", 2, 0)
			else
				tc[i]:SetPoint("LEFT", tc[i-1], "RIGHT", 5, 0)
			end
		end

		tc:SetScript("OnEvent", function(self)
			local points

			if UnitHasVehicleUI("player") then
				points = GetComboPoints("vehicle", "target")
			else
				points = GetComboPoints("player", "target")
			end

			if points then
				-- update combos display
				for i = 1, MAX_COMBO_POINTS do
					if i <= points then
						self[i]:SetValue(1)
					else
						self[i]:SetValue(0)
					end
					self:SetAlpha(1)
				end
			end
		end)
	end
end