
local module = _G["BlinkHealthTextModule"]

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

local hasRevengeImprovedGlyph = false
local function getSize(baseSize, expirationTime, duration)
	local size = (expirationTime-GetTime()) / duration
	if size < 0.3 then
		size = baseSize * 0.4
	elseif size < 0.6 then
		size = baseSize * 0.5
	else
		size = baseSize * 0.6
	end
	return size
end

local L_SWORD_AND_BOARD = "Sword and Board"
local L_OVERPOWER = "Overpower"
local L_SUDDEN_DEATH = "Sudden Death"
local L_BLOODSURGE = "Bloodsurge"
local L_TASTE_FOR_BLOOD = "Taste for Blood"
local L_SLAM = "Slam"
local L_EXECUTE = "Execute"
local L_GLYPH_OF_REVENGE = "Glyph of Revenge"
local L_SUNDER_ARMOR = "Sunder Armor"
if GetLocale()=="koKR" then
L_SWORD_AND_BOARD = "검과 방패"
L_OVERPOWER = "제압"
L_SUDDEN_DEATH = "급살"
L_BLOODSURGE = "피의 쇄도"
L_TASTE_FOR_BLOOD = "피의 맛"
L_SLAM = "격돌"
L_EXECUTE = "마무리 일격"
L_GLYPH_OF_REVENGE = "복수의 문양"
L_SUNDER_ARMOR = "방어구 가르기"
end

module.config = {
	[L_SWORD_AND_BOARD] = {3, 25, false},
	[L_OVERPOWER] = {1, 7, false},
	[L_SUDDEN_DEATH] = {1, 26, false},
	[L_BLOODSURGE] = {2, 25, 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("GLYPH_ADDED")
	self.addon.mainFrame:RegisterEvent("GLYPH_REMOVED")
	--self.addon.mainFrame:RegisterEvent("VARIABLES_LOADED")
	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

	hasRevengeImprovedGlyph = false
	for i=1, 6 do
		local enabled, glyphType, glyphSpell, iconFilename = GetGlyphSocketInfo(i)
		if enabled and glyphSpell == 58364 then -- 58364 == 복수의 문양 id
			hasRevengeImprovedGlyph = true
		end
	end
end

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

--function module:getTargetText()
function module:getPlayerText()
	local size = self.addon.db.fontSizeHealth
	local icon, text = "", ""
	local aura, rank, icon, count, debufType, duration, expirationTime

	if self.config[L_OVERPOWER][3] then
		aura = self:getAuraInfo("player", L_TASTE_FOR_BLOOD, "HELPFUL")
		local usable, nomana = IsUsableSpell(L_OVERPOWER)
		_, _, icon = GetSpellInfo(L_OVERPOWER)
		if aura or usable then
			--text = (":|T%s:%d:%d:0:0|t"):format(icon, size*1.2, size*1.2)
			text = ("|T%s:%d:%d:0:0|t"):format(icon, size*1.2, size*1.2)
		end
	end

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

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

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

--	if hasRevengeImprovedGlyph then
--		aura, rank, icon, count, debufType, duration, expirationTime = self:getAuraInfo("player", L_GLYPH_OF_REVENGE, "HELPFUL")
--		if aura then
--			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
--			text = text .. (":|T%s:%d:%d:0:0|t"):format(icon, size, size)
--		end
--	end

	return self:getAuraCount("target", L_SUNDER_ARMOR, "HARMFUL") .. text
end

--function module:getTargetHealthColor()
function module:getPlayerHealthColor()
	local name

	if self.config[L_SUDDEN_DEATH][3] then
		name = self:getAuraInfo("player", L_SUDDEN_DEATH, "HELPFUL")
		local usable, nomana = IsUsableSpell(L_EXECUTE)
		if name or usable then
			return true, 1.0, 0.0, 0.2 -- red?
		end
	end
end
