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.fmtVal(v)
	if v >= 1e6 then
		return ("%.2fM"):format(v / 1e6):gsub("%.?0+([km])$", "%1")
	elseif v >= 1e3 or v <= -1e3 then
		return ("%.1fK"):format(v / 1e3):gsub("%.?0+([km])$", "%1")
	else
		return v
	end
end

function T.round(number, decimals)
	if not decimals then decimals = 0 end
    return (("%%.%df"):format(decimals)):format(number)
end

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.RGBToHex(r, g, b)
	r = r <= 1 and r >= 0 and r or 0
	g = g <= 1 and g >= 0 and g or 0
	b = b <= 1 and b >= 0 and b or 0
	return string.format("|cff%02x%02x%02x", r*255, g*255, b*255)
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.fmtVal(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 = {.05, .05, .05} end --Just incase none of these pass
	local r, g, b = color[1], color[2], color[3]

	if power.value then
		power.value:SetTextColor(color[1], color[2], color[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.fmtVal(min))
		end
	end

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

function T.KillClassBars(self)
	local toKill = {
		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].Kill then frame[i]:Kill() 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