
local module = _G["BlinkHealthTextModule"]

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

local L_IMPROVED_SINISTER_STRIKE = "Improved Sinister Strike"
local L_DIRTY_DEEDS = "Dirty Deeds"
local L_HEMORRHAGE = "Hemorrhage"
local L_SLAUGHTER_FROM_THE_SHADOWS = "Slaughter from the Shadows"
local L_RIPOSTE = "Riposte"
if GetLocale()=="koKR" then
L_IMPROVED_SINISTER_STRIKE = "사악한 일격 연마"
L_DIRTY_DEEDS = "비열한 행위"
L_HEMORRHAGE = "과다출혈"
L_SLAUGHTER_FROM_THE_SHADOWS = "어둠의 학살자"
L_RIPOSTE = "반격"
end

module.config = {
	[L_IMPROVED_SINISTER_STRIKE] = {2, 2, false},
	[L_DIRTY_DEEDS] = {3, 15, false},
	[L_HEMORRHAGE] = {3, 16, false},
	[L_SLAUGHTER_FROM_THE_SHADOWS] = {3, 27, false},
	[L_RIPOSTE] = {2, 8, 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

	if self.config[L_HEMORRHAGE][3] then
		_, _, _, _, talentrank = GetTalentInfo(self.config[L_SLAUGHTER_FROM_THE_SHADOWS][1], self.config[L_SLAUGHTER_FROM_THE_SHADOWS][2])
		ComboSkill = 35 - talentrank
	elseif self.config[L_IMPROVED_SINISTER_STRIKE] then
		_, _, _, _, talentrank = GetTalentInfo(self.config[L_IMPROVED_SINISTER_STRIKE][1], self.config[L_IMPROVED_SINISTER_STRIKE][2])
		if talentrank == 1 then
			ComboSkill = 42
		elseif talentrank == 2 then
			ComboSkill = 40
		else
			ComboSkill = 45
		end
	else
		ComboSkill = 45
	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 = self:getComboText()
	--text = text .. self:getAuraCount("target", "^맹독 (.+)", "HARMFUL") -- 이런!! 렙에따라 디법이름이 다르다니..
	return text
end

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

	if self.config[L_RIPOSTE][3] then
		local usable, nomana = IsUsableSpell(L_RIPOSTE)
		if usable then
			return true, 0.4, 0.4, 1.0
		end
	end

	if self.config[L_DIRTY_DEEDS][3] then
		local c = UnitHealth("target") / UnitHealthMax("target")
		if c <= .35 then
			return true, 1.0, 0.0, 0.2 -- 붉은색, 추가뎀 20%
		end
	end
end
