
local module = _G["BlinkHealthTextModule"]

if select(2, UnitClass("player")) ~= "DRUID" or not module then return end
	
local ComboSkill = 45
local FinishSkill = 35

local L_FEROCITY = "Ferocity"
local L_OWLKIN_FRENZY = "Owlkin Frenzy"
local L_ECLIPSE = "Eclipse"
local L_MANGLE = "Mangle"
local L_MANGLE_CAT = "Mangle - Cat"
local L_MANGLE_BEAR = "Mangle - Bear"
local L_IMPROVED_MANGLE = "Improved Mangle"
local L_SWIFTMEND = "Swiftmend"
local L_LACERATE = "Lacerate"
local L_LIFEBLOOM = "Lifebloom"
local L_REJUVENATION = "Rejuvenation"
local L_REGENERATION = "Regeneration"
if GetLocale()=="koKR" then
L_FEROCITY = "야수의 본성"
L_OWLKIN_FRENZY = "올빼미야수의 광기"
L_ECLIPSE = "월식"
L_MANGLE = "짓이기기"
L_MANGLE_CAT = "짓이기기 (표범)"
L_MANGLE_BEAR = "짓이기기 (곰)"
L_IMPROVED_MANGLE = "짓이기기 연마"
L_SWIFTMEND = "신속한 치유"
L_LACERATE = "가르기"
L_LIFEBLOOM = "피어나는 생명"
L_REJUVENATION = "회복"
L_REGENERATION = "재생"
end

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_FEROCITY] = {2, 1, false},
	[L_OWLKIN_FRENZY] = {1, 21, false},
	[L_ECLIPSE] = {1, 23, false},
	[L_MANGLE] = {2, 26, false},
	[L_IMPROVED_MANGLE] = {2, 27, false},
	[L_SWIFTMEND] = {3, 18, 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:update()
end

function module:update()
	local talentrank = 0
	for auraName, t in pairs(self.config) do
		_, _, _, _, talentrank = GetTalentInfo(t[1], t[2])
		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
	ComboSkill = 45
	if self.config[L_MANGLE][3] then
		if self.config[L_IMPROVED_MANGLE][3] then
			_, _, _, _, talentrank = GetTalentInfo(self.config[L_IMPROVED_MANGLE][1], self.config[L_IMPROVED_MANGLE][2])
			ComboSkill = 45 - (talentrank * 2)
		end
	end
	if self.config[L_FEROCITY][3] then
		_, _, _, _, talentrank = GetTalentInfo(self.config[L_FEROCITY][1], self.config[L_FEROCITY][2])
		ComboSkill = ComboSkill - talentrank
	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:getComboText()
	local combo = GetComboPoints("player")
	local comboText, r, g, b = "", 1.0, 0.5, 0.1

	if( combo <= 0 )then
		return ""
	end

	if ( UnitPowerType("player") == 3 ) then -- energy
		local mana = UnitMana("player")
		if( mana >= ComboSkill )then
			r = 0.1
			g = 1.0
			b = 0.1 -- green
		elseif( mana >= FinishSkill )then
			r = 0.0
			g = 0.39
			b = 0.88 -- blue
		else
			r = 1.0
			g = 0.1
			b = 0.1 -- red
		end
	end
	--return (":|cff%02x%02x%02x%d|r"):format(r * 255, g * 255, b * 255, combo)
	return ("|cff%02x%02x%02x%d|r"):format(r * 255, g * 255, b * 255, combo)
end

--function module:getTargetText()
function module:getPlayerText()
	local text = ""
	local m_cat, m_bear, aura, rank, icon, count, debufType, duration, expirationTime
	local size = self.addon.db.fontSizeHealth
	if self.config[L_MANGLE][3] then
		local icon, m_cat, m_bear
		m_cat, _, icon, count, debufType, duration, expirationTime = self:getAuraInfo("target", L_MANGLE_CAT, "HARMFUL")
		if m_cat then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			--text = (":|T%s:%d:%d:0:0|t"):format(icon, size, size)
			text = ("|T%s:%d:%d:0:0|t"):format(icon, size, size)
		end
		if text == "" then
			m_bear, _, icon, count, debufType, duration, expirationTime = self:getAuraInfo("target", L_MANGLE_BEAR, "HARMFUL")
			if m_bear then
				size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
				--text = (":|T%s:%d:%d:0:0|t"):format(icon, size, size)
				text = ("|T%s:%d:%d:0:0|t"):format(icon, size, size)
			end
		end
	end

	if GetShapeshiftForm() == 1 then
		return self:getAuraCount("target", L_LACERATE, "HARMFUL") .. text
	elseif GetShapeshiftForm() == 3 then
		return self:getComboText() .. text
	else
		text = self:getAuraCount("target", L_LIFEBLOOM, "HELPFUL")
		if self.config[L_SWIFTMEND][3] then
			aura, rank, icon, count, debufType, duration, expirationTime = self:getAuraInfo("target", L_REJUVENATION, "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
			aura, rank, icon, count, debufType, duration, expirationTime = self:getAuraInfo("target", L_REGENERATION, "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_OWLKIN_FRENZY][3] then
			aura, rank, icon, count, debufType, duration, expirationTime = self:getAuraInfo("player", L_OWLKIN_FRENZY, "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_ECLIPSE][3] then
			aura, rank, icon, count, debufType, duration, expirationTime = self:getAuraInfo("player", L_ECLIPSE, "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
		return text
	end
end
