-------------------------------------------------------------------------------
--
--  Mod Name : BlinkHealthText 0.2
--  Author   : Blink
--  Date     : 2005/05/10
--  LastUpdate : 2006/12/06
--
-------------------------------------------------------------------------------

BHT_TextAlphaValue = 0.9;
BHT_TextHeight = 30;
BHT_InCombat = 0;

BHT_Thunder = 0;
BHT_Shout = 0;
BHT_Combo = 0;

-- sunder count 추가

TankCombo_Filter = {
	{ pattern = "(.+)|1이;가; 방어구 가르기에 걸렸습니다."},
	{ pattern = "(.+)|1이;가; 방어구 가르기에 걸렸습니다. (.+)"},
	{ pattern = "(.+)|1이;가; 사기의 외침에 걸렸습니다."},
	{ pattern = "(.+)|1이;가; 사기의 외침에 걸렸습니다. (.+)"},
	{ pattern = "(.+)|1이;가; 천둥벼락에 걸렸습니다."},
	{ pattern = "(.+)|1이;가; 천둥벼락에 걸렸습니다. (.+)"},
	{ pattern = "(.+)의 몸에서 사기의 외침 효과가 사라졌습니다."},
	{ pattern = "(.+)의 몸에서 천둥벼락 효과가 사라졌습니다."},
	{ pattern = "(.+)의 몸에서 방어구 가르기 효과가 사라졌습니다."}
};

function TankComboSearch()
	local sunders = 0;
	local thunder = 0;
	local shout = 0;
	local rank = 0
    for i = 1,40  do
        name, rank, _, count, _ = UnitDebuff("target", i);
                      	
       	if (name == "방어구 가르기") then
	 				sunders = count;
	 			elseif (name == "천둥벼락") then
	 				thunder = 1;
	 			elseif (name == "사기의 외침") then
	 				shout = 1; 			
	 			elseif (name == nil) then
	 				break;
	 			end
    end
        
		return sunders, thunder, shout;
end

function BlinkHealthText_OnLoad()
	this:RegisterEvent("PLAYER_REGEN_DISABLED");
	this:RegisterEvent("PLAYER_REGEN_ENABLED");
	this:RegisterEvent("PLAYER_TARGET_CHANGED");
	this:RegisterEvent("PLAYER_ENTERING_WORLD");
	
	BlinkHealthTextPlayerText:SetAlpha(BHT_TextAlphaValue);
	BlinkPowerTextPlayerText:SetAlpha(BHT_TextAlphaValue);

	BlinkHealthTextTargetText:SetAlpha(BHT_TextAlphaValue);
	BlinkPowerTextTargetText:SetAlpha(BHT_TextAlphaValue);
end

function BlinkHealthText_OnEvent()
	if ( event == "PLAYER_TARGET_CHANGED" )then
		if( UnitExists("target") and BHT_InCombat == 1 ) then
			BHT_DisplayTargetHealthText_Combo();
			BHT_DisplayTargetPowerText();
			BlinkHealthTextTarget:Show();
			BlinkPowerTextTarget:Show();
		else
			BlinkHealthTextTarget:Hide();
			BlinkPowerTextTarget:Hide();
		end
	elseif ( event == "PLAYER_REGEN_DISABLED" ) then
		BHT_DisplayPlayerHealthText();
		BHT_DisplayPlayerPowerText();
		BlinkHealthTextPlayer:Show();
		BlinkPowerTextPlayer:Show();
		BHT_InCombat = 1;
		if( UnitExists("target") )then
			BHT_DisplayTargetHealthText();
			BHT_DisplayTargetPowerText();
			BlinkHealthTextTarget:Show();
			BlinkPowerTextTarget:Show();
		end
	elseif ( event == "PLAYER_REGEN_ENABLED" or event == "PLAYER_ENTERING_WORLD" ) then
		BlinkHealthTextPlayer:Hide();
		BlinkHealthTextTarget:Hide();
		BlinkPowerTextPlayer:Hide();
		BlinkPowerTextTarget:Hide();
		BHT_InCombat = 0;
	end
end

-------------------------------------------------------------------------------

function BlinkHealthTextPlayer_OnLoad()
	this:RegisterEvent("UNIT_HEALTH");
end

function BlinkHealthTextPlayer_OnEvent(event)
	if( arg1 == "player" )then
		BHT_DisplayPlayerHealthText();
	end
end

function BHT_DisplayPlayerHealthText()
	local perH = UnitHealth("player") / UnitHealthMax("player") * 100;
	if ( perH < 20 )then
		BlinkHealthTextPlayerText:SetTextColor(1.0, 0.0, 0.2);
	elseif( perH < 80 and perH >= 20 )then
		BlinkHealthTextPlayerText:SetTextColor(1.0, 1.0, 0.2);
	else
		BlinkHealthTextPlayerText:SetTextColor(0.0, 1.0, 0.2);
	end
	BlinkHealthTextPlayerText:SetText(format("%d",perH));
	BlinkHealthTextPlayerText:SetAlpha(BHT_TextAlphaValue);
end

-------------------------------------------------------------------------------

function BlinkPowerTextPlayer_OnLoad()
	-- Mana Events
	this:RegisterEvent("UNIT_MANA");
	this:RegisterEvent("UNIT_RAGE");
	this:RegisterEvent("UNIT_FOCUS");
	this:RegisterEvent("UNIT_ENERGY");
	this:RegisterEvent("UNIT_HAPPINESS");
	this:RegisterEvent("UNIT_MAXMANA");
	this:RegisterEvent("UNIT_MAXRAGE");
	this:RegisterEvent("UNIT_MAXFOCUS");
	this:RegisterEvent("UNIT_MAXENERGY");
	this:RegisterEvent("UNIT_MAXHAPPINESS");
	this:RegisterEvent("UNIT_DISPLAYPOWER");
end

function BlinkPowerTextPlayer_OnEvent(event)
	if( arg1 == "player" )then
		BHT_DisplayPlayerPowerText();
	end
end

function BHT_DisplayPlayerPowerText()
	local currValue = UnitMana("player");
	--local info = ManaBarColor[UnitPowerType(arg1)];
	BlinkPowerTextPlayerText:SetTextColor(0.0,1.0,0.2);
	BlinkPowerTextPlayerText:SetText(currValue);
	BlinkPowerTextPlayerText:SetAlpha(BHT_TextAlphaValue);
end

-------------------------------------------------------------------------------

function BlinkHealthTextTarget_OnLoad()
	this:RegisterEvent("UNIT_HEALTH");
	if ( select(2,UnitClass("player")) == "DRUID" or select(2,UnitClass("player")) == "ROGUE" ) then
		this:RegisterEvent("PLAYER_COMBO_POINTS");
	elseif (select(2,UnitClass("player")) == "WARRIOR") then
		this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_CREATURE_DAMAGE") --for afflition of Sunder Armor   
	  this:RegisterEvent("CHAT_MSG_SPELL_AURA_GONE_OTHER") -- for the fade	
	end
end

function BlinkHealthTextTarget_OnEvent(event)
	if( (event == "UNIT_HEALTH" and arg1 == "target") or event == "PLAYER_COMBO_POINTS" )then
		BHT_DisplayTargetHealthText_Combo();
	end
	
	if (event == "CHAT_MSG_SPELL_PERIODIC_CREATURE_DAMAGE" or event == "CHAT_MSG_SPELL_AURA_GONE_OTHER")then
		for i = 1,9 do
			found = string.find(arg1, TankCombo_Filter[i].pattern)
			if (found) then
				BHT_DisplayTargetHealthText_Combo();
			end
		end
	end
end

function BHT_DisplayTargetHealthText_Combo()
	local perH = UnitHealth("target") / UnitHealthMax("target") * 100;
	if ( perH < 20 )then
		BlinkHealthTextTargetText:SetTextColor(1.0, 0.0, 0.2);
	elseif( perH < 80 and perH >= 20 )then
		BlinkHealthTextTargetText:SetTextColor(1.0, 1.0, 0.2);
	else
		BlinkHealthTextTargetText:SetTextColor(0.0, 1.0, 0.2);
	end
		
	if (select(2,UnitClass("player")) == "WARRIOR") then
		BHT_Combo, BHT_Thunder, BHT_Shout = TankComboSearch();
	else
	 	BHT_Combo = GetComboPoints();
	end
	
	local perS = "";
	if( BHT_Combo > 0 )then
		perS = string.format("%d",perH) .. ": |cFFFF6622"..BHT_Combo.."|r";
	else
		perS = string.format("%d",perH);
	end
	
	if (select(2,UnitClass("player")) == "WARRIOR") then
		if (BHT_Thunder == 1) then
			perS = perS .. ": |cFFFF6622".."천둥".."|r";
		end
		
		if (BHT_Shout == 1) then
			perS = perS .. ": |cFFFF6622".."사기".."|r";
		end
	end
		
	BlinkHealthTextTargetText:SetText(perS);
	BlinkHealthTextTargetText:SetAlpha(BHT_TextAlphaValue);
end

function BHT_DisplayTargetHealthText()
	local perH = UnitHealth("target") / UnitHealthMax("target") * 100;
	if ( perH < 20 )then
		BlinkHealthTextTargetText:SetTextColor(1.0, 0.0, 0.2);
	elseif( perH < 80 and perH >= 20 )then
		BlinkHealthTextTargetText:SetTextColor(1.0, 1.0, 0.2);
	else
		BlinkHealthTextTargetText:SetTextColor(0.0, 1.0, 0.2);
	end
		
	local perS = "";
	if( BHT_Combo > 0 )then
		perS = string.format("%d",perH) .. ": |cFFFF6622"..BHT_Combo.."|r";
	else
		perS = string.format("%d",perH);
	end
	
	if (select(2,UnitClass("player")) == "WARRIOR") then
		if (BHT_Thunder == 1) then
			perS = perS .. ": |cFFFF6622".."T".."|r";
		end
		
		if (BHT_Thunder == 1) then
			perS = perS .. ": |cFFFF6622".."S".."|r";
		end
	end
		
	BlinkHealthTextTargetText:SetText(perS);
	BlinkHealthTextTargetText:SetAlpha(BHT_TextAlphaValue);
end


-------------------------------------------------------------------------------

function BlinkPowerTextTarget_OnLoad()
	this:RegisterEvent("UNIT_MANA");
	this:RegisterEvent("UNIT_RAGE");
	this:RegisterEvent("UNIT_FOCUS");
	this:RegisterEvent("UNIT_ENERGY");
	this:RegisterEvent("UNIT_HAPPINESS");
	this:RegisterEvent("UNIT_MAXMANA");
	this:RegisterEvent("UNIT_MAXRAGE");
	this:RegisterEvent("UNIT_MAXFOCUS");
	this:RegisterEvent("UNIT_MAXENERGY");
	this:RegisterEvent("UNIT_MAXHAPPINESS");
	this:RegisterEvent("UNIT_DISPLAYPOWER");
end

function BlinkPowerTextTarget_OnEvent(event)
	if( arg1 == "target" )then
		BHT_DisplayTargetPowerText();
	end
end

function BHT_DisplayTargetPowerText()
	--local min, maxValue = TargetFrameManaBar:GetMinMaxValues();
	--local info = BHT_ManaBarColor[UnitPowerType(arg1)];
	local min, maxValue = UnitMana("target"),UnitManaMax("target");
	if ( maxValue == 0 ) then
		if ( BlinkPowerTextTarget:IsVisible() ) then
			BlinkPowerTextTarget:Hide();
		end
		return;
	elseif ( maxValue > 0 ) then
		if ( not BlinkPowerTextTarget:IsVisible() and BHT_InCombat == 1 ) then
			BlinkPowerTextTarget:Show();
		end
	end

	local pvalue;
	if ((min == 0) and (maxValue == 0)) then
		pvalue= "";
	else
		currValue = TargetFrameManaBar:GetValue();
		if( select(2,UnitClass("target")) == "ROGUE" and maxValue > 100 )then
			pvalue = currValue .. "/" .. maxValue;
		else
			pvalue = currValue;
		end
	end

	BlinkPowerTextTargetText:SetTextColor(0.0,1.0,0.2);
	BlinkPowerTextTargetText:SetText(pvalue);
	BlinkPowerTextTargetText:SetAlpha(BHT_TextAlphaValue);
end

-------------------------------------------------------------------------------
