
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"
local L_DEEP_FREEZE = "DeepFreeze"
local L_IMPACT = "Impact"
local L_HOT_STREAK = "Hot Streak"

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 = "얼음 회오리"
L_DEEP_FREEZE = "동결"
L_IMPACT = "충돌"
L_HOT_STREAK = "몰아치는 열기"
L_ = "화염촉발"
end

local function getSize(baseSize, expirationTime, duration)
	local size = (expirationTime-GetTime()) / duration
	if size < 0.3 then
		size = baseSize * .8
	elseif size < 0.6 then
		size = baseSize * 1.0
	else
		size = baseSize * 1.2
	end
	return size / 2
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},
	[L_DEEP_FREEZE] = {3, 28, false},
	[L_IMPACT] = {2, 8, false},
	[L_HOT_STREAK] = {2, 26, 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
				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()
	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 = UnitAura("player", L_CLEARCASTING, nil, "HELPFUL")
		if name then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|T%s:%d|t"):format(icon, size)
		end
	end

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

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

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

	if self.config[L_DEEP_FREEZE][3] then
		name = nil
		if self.config[L_FINGERS_OF_FROST][3] then
			name = UnitAura("player", L_FINGERS_OF_FROST, nil, "HELPFUL")
		end
		if IsUsableSpell(L_DEEP_FREEZE) and (name or UnitAura("target", L_FROST_NOVA, nil, "HARMFUL") or UnitAura("target", L_COPY_OF_FROSTBITE, nil, "HARMFUL")) then
			text = text .. (":|T%s:%d|t"):format("Interface\\Icons\\Ability_Mage_DeepFreeze", size / 2)
		end
	end

	if self.config[L_IMPACT][3] then
		name, rank, icon, count, debufType, duration, expirationTime = UnitAura("player", L_IMPACT, nil, "HELPFUL")
		if name then
			size = self.addon.db.fontSizeHealth * 1.2 / 2 --getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|T%s:%d|t"):format(icon, size)
		end
	end

	return text
end

function module:getTargetHealthColor()
	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 = UnitAura("player", L_FINGERS_OF_FROST, nil, "HELPFUL")
			if name then
				return true, 0.75, 0.75, 1.0 -- 파란색? 하늘색?
			end
		end
		name = UnitAura("target", L_FROST_NOVA, nil, "HARMFUL")
		if name then
			return true, 0.75, 0.75, 1.0 -- 파란색? 하늘색?
		end
		name = UnitAura("target", L_COPY_OF_FROSTBITE, nil, "HARMFUL")
		if name then
			return true, 0.75, 0.75, 1.0 -- 파란색? 하늘색?
		end
	end
end
