function SpellAlert_OnUpdate()
end

function SpellAlert_OnLoad()
	this:RegisterEvent("CHAT_MSG_SPELL_CREATURE_VS_CREATURE_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_CREATURE_BUFFS");
	this:RegisterEvent("CHAT_MSG_SPELL_CREATURE_VS_CREATURE_BUFF");
	this:RegisterEvent("CHAT_MSG_SPELL_HOSTILEPLAYER_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_HOSTILEPLAYER_BUFF");
	this:RegisterEvent("CHAT_MSG_MONSTER_EMOTE");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_SELF_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_HOSTILEPLAYER_BUFFS");
	this:RegisterEvent("UNIT_SPELLCAST_START");

	AlertFrame:SetTimeVisible(2);
end

function Sa_PlaySound()
	PlaySound("Sound\interface\AuctionWindowClose.wav");
end

function SpellAlert_OnEvent()
	local mob, spell;
	if (event == "CHAT_MSG_SPELL_HOSTILEPLAYER_DAMAGE") then
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_CAST) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
			SpellAlert_alert(mob, spell, mob.."|1이;가; "..spell.."|1을;를; 시전합니다.");
				if ( spell == SA_HAM ) then
					Sa_PlaySound();
				end
			end
			return;
		end
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_NEW1CAST) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
			SpellAlert_alert(mob, spell, mob.."|1이;가; "..spell.." 시전을 시작합니다.");
				if ( spell == SA_HAM ) then
					Sa_PlaySound();
				end
			end
			return;
		end
		for mob, target, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_NEW2CAST) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
			SpellAlert_alert(mob, target, spell, mob.."|1이;가; "..target.."에게 "..spell.."|1을;를; 시전합니다.");
				if ( spell == SA_HAM ) then
					Sa_PlaySound();
				end
			end
			return;
		end
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_PERFORM) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
				SpellAlert_alert(mob, spell, mob.."|1이;가; "..spell.."|1을;를; 사용합니다.");
				if ( spell == SA_SS ) then
					Sa_PlaySound();
				end
			end
			return;
		end
	elseif (event == "CHAT_MSG_SPELL_HOSTILEPLAYER_BUFF") then
		for mob, spell, k in string.gmatch(arg1, SA_PTN_SPELL_GAINS_X) do
			return;
		end
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_CAST) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
			SpellAlert_alert(mob, spell, mob.."|1이;가; "..spell.."|1을;를; 시전합니다.");
			end
			return;
		end
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_NEW1CAST) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
			SpellAlert_alert(mob, spell, mob.."|1이;가; "..spell.." 시전을 시작합니다.");
			end
			return;
		end
		for mob, target, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_NEW2CAST) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
			SpellAlert_alert(mob, target, spell, mob.."|1이;가; "..target.."에게 "..spell.."|1을;를; 시전합니다.");
			end
			return;
		end
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_TOTEM) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
			SpellAlert_alert(mob, spell, mob.."|1이;가; "..spell.." 토템을 시전합니다.");
			end
			return;
		end
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_GAINS) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
			SpellAlert_alert(mob, spell, mob.."|1이;가; "..spell.." 효과를 얻었습니다.");
			end
			return;
		end
	elseif (event == "CHAT_MSG_SPELL_CREATURE_VS_CREATURE_BUFF") then
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_CAST) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
			SpellAlert_alert(mob, spell, mob.."|1이;가; "..spell.."|1을;를; 시전합니다.");
			end
			return;
		end
	elseif (event == "CHAT_MSG_SPELL_PERIODIC_CREATURE_BUFFS") then
		for mob, spell, k in string.gmatch(arg1, SA_PTN_SPELL_GAINS_X) do
			return;
		end
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_GAINS) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
			SpellAlert_alert(mob, spell, mob.."|1이;가; "..spell.." 효과를 얻었습니다.");
			end
			return;
		end
	elseif (event == "CHAT_MSG_SPELL_CREATURE_VS_CREATURE_DAMAGE") then
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_CAST) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
			SpellAlert_alert(mob, spell, mob.."|1이;가; "..spell.."|1을;를; 시전합니다.");
				if ( spell == SA_HAM ) then
					Sa_PlaySound();
				end
			end
			return;
		end
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_NEW1CAST) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
			SpellAlert_alert(mob, spell, mob.."|1이;가; "..spell.." 시전을 시작합니다.");
				if ( spell == SA_HAM ) then
					Sa_PlaySound();
				end
			end
			return;
		end
		for mob, target, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_NEW2CAST) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
			SpellAlert_alert(mob, target, spell, mob.."|1이;가; "..target.."에게 "..spell.."|1을;를; 시전합니다.");
				if ( spell == SA_HAM ) then
					Sa_PlaySound();
				end
			end
			return;
		end
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_PERFORM) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
			SpellAlert_alert(mob, spell, mob.."|1이;가; "..spell.."|1을;를; 시전합니다.");
				if ( spell == SA_SS ) then
					Sa_PlaySound();
				end
			end
			return;
		end
	elseif (event == "CHAT_MSG_SPELL_PERIODIC_HOSTILEPLAYER_BUFFS") then
		for mob, spell, temp in string.gmatch(arg1, SA_PTN_SPELL_GAINS_X) do
			return;
		end
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_GAINS) do
			spell = SpellAlert_color(spell);
			if ((not SpellAlert_isParty(mob)) and (not SpellAlert_isRaid(mob))) then
			SpellAlert_alert(mob, spell, mob.."|1이;가; "..spell.." 효과를 얻었습니다.");
				if ( (spell == SA_NF) or (spell == SA_POM) ) then
					Sa_PlaySound();
				end
			end
			return;
		end
	elseif (event == "CHAT_MSG_EMOTE") then
				SpellAlert_alert("", "", arg1);
	elseif (event == "CHAT_MSG_MONSTER_EMOTE") then
		local name = arg2;
		if (not name) then
			name = "nil";
		end
		if (arg1) then
			if (string.find(arg1,"숨을 깊게 들이쉽니다.")) then
				SpellAlert_alert("오닉시아 딥 브레스 5초 전 : 벽으로!!!");
				AlertFrame:AddMessage(msg, 1, 0, 0, 1, 2);
				PlaySoundFile("Sound\\Character\\Orc\\OrcVocalMale\\OrcMaleOpenFire01.wav");
			end
		end
	end
end

function SpellAlert_alert(mob, spell, msg)
	if (SA_SPELLS_IGNORE[spell] == 1) then
		return;
	end
		SpellAlert_banner(msg, mob);
end

function SpellAlert_color(spell)
	local spellcolor
	if (SA_SPELLS_HEALS[spell] == 1 ) then
		spellcolor = "|cff00ff00"..spell.."|r";
	elseif ((SA_SPELLS_CC[spell] == 1) or (SA_SPELLS_DISPELABLE[spell] == 1)) then
		spellcolor = "|cffff9900"..spell.."|r";
	elseif (SA_SPELLS_DAMAGE[spell] == 1) then
		spellcolor = "|cffbb55ff"..spell.."|r";
	else
                spellcolor = "|cffffffff"..spell.."|r";
	end
	return spellcolor;
end

function SpellAlert_banner(msg, mob)
	local targetname = UnitName("target");
		if (mob == targetname) then
			AlertFrame:AddMessage(msg, 1, 1, 0, 1, 2);
		else
			AlertFrame:AddMessage(msg, 1, 1, 1, 1, 2);
		end
end

function SpellAlert_print(msg)
	if (DEFAULT_CHAT_FRAME) then
		DEFAULT_CHAT_FRAME:AddMessage(msg);
	end
end

function SpellAlert_isParty(name)
	for i = 1, 4, 1 do
		local partyname = UnitName("party" .. i);
		if (name == partyname) then
			return 1;
		end
	end
	return nil;
end

function SpellAlert_isRaid(name)
	for i = 1, 40, 1 do
		local raidname = UnitName("raid" .. i);
		if (name == raidname) then
			return 1;
		end
	end
	return nil;
end