
local module = _G["BlinkHealthTextModule"]

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

local L_FINGERS_OF_FROST = "Fingers of Frost"
local L_FIREBALL1 = "Fireball!"
local L_COPY_OF_FROSTBITE = "Copy of Frostbite"
local L_SHATTER = "Shatter"
local L_IMPROVED_SCORCH = "Improved Scorch"
local L_MISSILE_BARRAGE = "Missile Barrage"
local L_WINTERS_CHILL = "Winter's Chill"
local L_CLEARCASTING = "Clearcasting"
local L_FROST_NOVA = "Frost Nova"
if GetLocale()=="koKR" then
L_FINGERS_OF_FROST = "겨울의 손아귀"
L_FIREBALL1 = "화염구!"
L_COPY_OF_FROSTBITE = "동상"
L_SHATTER = "산산조각"
L_IMPROVED_SCORCH = "불태우기 연마"
L_MISSILE_BARRAGE = "화살 탄막"
L_WINTERS_CHILL = "혹한의 추위"
L_CLEARCASTING = "정신 집중"
L_FROST_NOVA = "얼음 회오리"
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_FINGERS_OF_FROST] = {3, 23, false},
	[L_FIREBALL1] = {3, 24, false},
	[L_COPY_OF_FROSTBITE] = {3, 1, false},
	[L_SHATTER] = {3, 13, false},
	[L_IMPROVED_SCORCH] = {2, 11, false},
	[L_MISSILE_BARRAGE] = {1, 27, false},
	[L_WINTERS_CHILL] = {3, 18, false},
	[L_CLEARCASTING] = {1, 6, 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])
		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
				ChatFrame1:AddMessage("RegisterEvent "..t[4], 1.0, 1.0, 0.4)
				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:getTargetText()
function module:getPlayerText()
	local text = ""
	local size = self.addon.db.fontSizeHealth
	local name, rank, icon, count, debufType, duration, expirationTime
	local r, g, b = 1, 1, 1

	if self.config[L_IMPROVED_SCORCH][3] then
		text = text .. self:getAuraCount("target", L_IMPROVED_SCORCH, "HARMFUL")
	end
	
	if self.config[L_WINTERS_CHILL][3] then
		text = text .. self:getAuraCount("target", L_WINTERS_CHILL, "HARMFUL")
	end

	if self.config[L_CLEARCASTING][3] then
		name, rank, icon, count, debufType, duration, expirationTime = self:getAuraInfo("player", L_CLEARCASTING, "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)
			text = text .. ("|T%s:%d:%d:0:0|t"):format(icon, size, size)
		end
	end

	if self.config[L_FIREBALL1][3] then
		name, rank, icon, count, debufType, duration, expirationTime = self:getAuraInfo("player", L_FIREBALL1, "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)
			text = text .. ("|T%s:%d:%d:0:0|t"):format(icon, size, size)
		end
	end

	if self.config[L_MISSILE_BARRAGE][3] then
		name, rank, icon, count, debufType, duration, expirationTime = self:getAuraInfo("player", L_MISSILE_BARRAGE, "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)
			text = text .. ("|T%s:%d:%d:0:0|t"):format(icon, size, size)
		end
	end

	return text
end

--function module:getTargetHealthColor()
function module:getPlayerHealthColor()
	local name
--	if self.config[L_FIREBALL1][3] then
--		name = self:getAuraInfo("player", L_FIREBALL1, "HELPFUL")
--		if name then
--			return true, 1.0, 0.0, 0.2 -- 붉은색
--		end
--	end
--
--	if self.config[L_MISSILE_BARRAGE][3] then
--		name = self:getAuraInfo("player", L_MISSILE_BARRAGE, "HELPFUL")
--		if name then
--			return true, 1.0, 0.2, 1.0 -- 보라색
--		end
--	end

	if self.config[L_SHATTER][3] then
		if self.config[L_FINGERS_OF_FROST][3] then
			name = self:getAuraInfo("player", L_FINGERS_OF_FROST, "HELPFUL")
			if name then
				return true, 0.75, 0.75, 1.0 -- 파란색? 하늘색?
			end
		end
		name = self:getAuraInfo("target", L_FROST_NOVA, "HARMFUL")
		if name then
			return true, 0.75, 0.75, 1.0 -- 파란색? 하늘색?
		end
		name = self:getAuraInfo("target", L_COPY_OF_FROSTBITE, "HARMFUL")
		if name then
			return true, 0.75, 0.75, 1.0 -- 파란색? 하늘색?
		end
	end
end
