
local module = _G["BlinkHealthTextModule"]

if select(2, UnitClass("player")) ~= "DEATHKNIGHT" or not module then return end

local RUNETYPE_BLOOD = 1
local RUNETYPE_UNHOLY = 2
local RUNETYPE_FROST = 3
local RUNETYPE_DEATH = 4
local iconTextures = {}
iconTextures[RUNETYPE_BLOOD] = "Interface\\PlayerFrame\\UI-PlayerFrame-Deathknight-Blood"
iconTextures[RUNETYPE_UNHOLY] = "Interface\\PlayerFrame\\UI-PlayerFrame-Deathknight-Unholy"
iconTextures[RUNETYPE_FROST] = "Interface\\PlayerFrame\\UI-PlayerFrame-Deathknight-Frost"
iconTextures[RUNETYPE_DEATH] = "Interface\\PlayerFrame\\UI-PlayerFrame-Deathknight-Death"

local L_SUDDEN_DOOM = "Sudden Doom"
local L_DEATH_TRANCE = "Death Trance!"
local L_RUNE_STRIKE = "Rune Strike"
if GetLocale()=="koKR" then
L_SUDDEN_DOOM = "불시의 파멸"
L_DEATH_TRANCE = "죽음의 무아지경"
L_RUNE_STRIKE = "룬의 일격"
end

local text
local name, rank, icon, count, debufType, duration, expirationTime
local size
local auraCount
local usable, nomana
local aCount

local function getSize(baseSize, expirationTime, duration)
	local size = (expirationTime-GetTime()) / duration
	if size < 0.3 then
		size = baseSize * .4
	elseif size < 0.6 then
		size = baseSize * .5
	else
		size = baseSize * .6
	end
	return size
end

module.config = {
	[L_SUDDEN_DOOM] = {1, 21, false},
}

function module:init()
	self.addon.mainFrame:RegisterEvent("PLAYER_ALIVE")
	self.addon.mainFrame:RegisterEvent("LEARNED_SPELL_IN_TAB")
	self.addon.mainFrame:RegisterEvent("CHARACTER_POINTS_CHANGED")
	self.addon.mainFrame:RegisterEvent("RUNE_POWER_UPDATE")
	self.addon.mainFrame:RegisterEvent("RUNE_TYPE_UPDATE")

	self:update()

end

function module:update()
	local talentrank = 0
	for auraName, t in pairs(self.config) do
		_, _, _, _, talentrank = GetTalentInfo(t[1], t[2])
		self.config[auraName][3] = false
		if talentrank > 0 then
			self.config[auraName][3] = true
			if t[4] and self.addon and self.addon.mainFrame then
				self.addon.mainFrame:RegisterEvent(t[4])
			end
		end
	end
end

function module:LEARNED_SPELL_IN_TAB(...)
	self:update()
end
module.CHARACTER_POINTS_CHANGED = module.LEARNED_SPELL_IN_TAB
function module:PLAYER_ALIVE(...)
	self.addon.mainFrame:UnregisterEvent("PLAYER_ALIVE")
	self:update()
end

function module:RUNE_POWER_UPDATE(...)
	self:updateRunes()
end
module.RUNE_TYPE_UPDATE = module.RUNE_POWER_UPDATE
module.PLAYER_REGEN_DISABLED = module.RUNE_POWER_UPDATE



function module:getPlayerText()
	text = ""
	size = self.addon.db.fontSizeHealth
	auraCount = self:getAuraTypeCount("target", "Disease", "PLAYER|HARMFUL")

	if auraCount > 0 then
		text = ("%d"):format(auraCount)
	end

	if self.config[L_SUDDEN_DOOM][3] then
		name, rank, icon, count, debufType, duration, expirationTime = self:getAuraInfo("player", L_DEATH_TRANCE, "HELPFUL")
		if name then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. ("|T%s:%d:%d:0:0|t"):format(icon, size, size)
		end
	end

	usable, nomana = IsUsableSpell(L_RUNE_STRIKE)
	_, _, icon = GetSpellInfo(L_RUNE_STRIKE)
	if usable then
		text = text .. ("|T%s:%d:%d:0:0|t"):format(icon, self.addon.db.fontSizeHealth*1.2, self.addon.db.fontSizeHealth*1.2)
	end

	return text
end


