-----------------------------------------------------------------------------------------------
--				어그로 관련 사항
-----------------------------------------------------------------------------------------------
SpellAlert_AggroName = nil;
Aggro_i = 1;
AssistMod = false;

Boss_Select = "보스 선택";
Fi_Boss_List = {"보스 선택", "아주어고스", "군주 카자크", "타에라", "이손드레", "레손", "에메리스"};
On_Boss_List = {"보스 선택", "오닉시아"};
Co_Boss_List = {"보스 선택", "루시프론", "마그마다르", "게헨나스", "가르", "남작 게돈", "샤즈라", "설퍼론 사자", "초열의 골레마그", "청지기 이그젝큐투스", "라그나로스"};
Bl_Boss_List = {"보스 선택", "폭군 서슬송곳니", "타락한 밸라스트라즈", "용기대장 래쉬레이어", "화염아귀", "에본로크", "플레임고르", "크로마구스"};
Zu_Boss_List = {"보스 선택", "가즈란카", "철혈의 그리렉", "폭풍마녀 우슐레이", "몽술사 하자라", "천검 레나타키", "대여사제 제클릭", "대사제 베녹시스", "대여사제 말리", "혈군주 만도키르", "대사제 데칼", "대여사제 알로크", "주술사 진도", "학카르"};
AR_Boss_List = {"보스 선택", "쿠린낙스", "장군 라작스", "먹보 부루", "사냥꾼 아야미스", "모암", "무적의 오시리안"};
AT_Boss_List = {"보스 선택", "예언자 스케람", "군주 크리", "공주 야우즈", "벰", "전투감시병 살투라", "불굴의 판크리스", "바이사이더스", "공주 후후란", "제왕 베클로어", "제왕 베크닐라쉬", "아우로", "쑨의 눈"};
-----------------------------------------------------------------------------------------------
--				어그로 함수 관련
-----------------------------------------------------------------------------------------------
function SpellAlert_Aggro_Load()
	this:RegisterEvent("PLAYER_ENTERING_WORLD");
	this:RegisterEvent("UNIT_HEALTH");

	SlashCmdList["MRTAGGRO"] = MRT_Aggro_Function;
	SLASH_MRTAGGRO1 = "/마법경고어그로";
end

function SpellAlert_Aggro_Event(event)
	if (event == "PLAYER_ENTERING_WORLD") then
		SpellAlert_AggroName = nil;
		Aggro_i = 1;		
	elseif (event == "UNIT_HEALTH") then
		if (AssistMod) then
			SpellAlert_Aggro_AssiMod();
		else
			SpellAlert_Aggro_Update();
		end		
	end
end

function Reset_Aggro()
	UIDropDownMenu_SetSelectedID(Bl_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Co_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Fi_Aggro_Boss_List, 1);	
	UIDropDownMenu_SetSelectedID(On_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Zu_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(AR_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(AT_Aggro_Boss_List, 1);
	SpellAlert_AggroName = nil;
	Boss_Select = "보스 선택";
	UIDropDownMenu_SetSelectedID(Bl_Aggro_Boss_List, 1);
end

function SpellAlert_Aggro_Function(msg)
	msg = string.upper(msg);
	local _,_,index2 = string.find(msg, "(.+)");
	if (index2 == "어시모드") then
		 AssistMod = true;
	elseif (index2 == "알림모드") then
		AssistMod = false;
	end
	local command, arg;
	local index = string.find(msg, " ");

	if( index) then
		command = string.sub(msg, 1, (index - 1));
		param = string.sub(msg, (index + 1)  );
	else
		command = msg;
	end
   
	if ( command == "") then
		if (SpellAlert_Aggro_Option:IsVisible()) then
			SpellAlert_Aggro_Option:Hide();
		else
			SpellAlert_Aggro_Option:Show();
		end
		
	end
end

function Bl_Aggro_Boss_List_Type()
	local info = {};
	local selectedIdx = 1;
	for i=1,table.getn(Bl_Boss_List) do
		info.text = Bl_Boss_List[i];
		info.func = Bl_Aggro_Boss_List_Click;
		if (info.text == Boss_Select) then
			selectedIdx = i;			
			info.checked = 1;
		end
		UIDropDownMenu_AddButton(info);
	end	
	UIDropDownMenu_SetSelectedID(Bl_Aggro_Boss_List, selectedIdx);	
end

function Bl_Aggro_Boss_List_Click()
	local selectedIdx = this:GetID();	
	UIDropDownMenu_SetSelectedID(Bl_Aggro_Boss_List, selectedIdx);
	UIDropDownMenu_SetSelectedID(Co_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Fi_Aggro_Boss_List, 1);	
	UIDropDownMenu_SetSelectedID(On_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Zu_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(AR_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(AT_Aggro_Boss_List, 1);
	SpellAlert_AggroName = nil;	
	if not(this:GetText() == "보스 선택") then
		Boss_Select = this:GetText();
		DEFAULT_CHAT_FRAME:AddMessage(this:GetText().." 어그로 알리미", 1.0, 0.5, 0.5);
	end	
end

function Co_Aggro_Boss_List_Type()
	local info = {};
	local selectedIdx = 1;
	for i=1,table.getn(Co_Boss_List) do
		info.text = Co_Boss_List[i];
		info.func = Co_Aggro_Boss_List_Click;
		if (info.text == Boss_Select) then
			selectedIdx = i;			
			info.checked = 1;
		end
		UIDropDownMenu_AddButton(info);
	end
	UIDropDownMenu_SetSelectedID(Co_Aggro_Boss_List, selectedIdx);
end

function Co_Aggro_Boss_List_Click()
	local selectedIdx = this:GetID();
	UIDropDownMenu_SetSelectedID(Co_Aggro_Boss_List, selectedIdx);	
	UIDropDownMenu_SetSelectedID(Bl_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Fi_Aggro_Boss_List, 1);	
	UIDropDownMenu_SetSelectedID(On_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Zu_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(AR_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(AT_Aggro_Boss_List, 1);
	SpellAlert_AggroName = nil;	
	if not(this:GetText() == "보스 선택") then			
		Boss_Select = this:GetText();
		DEFAULT_CHAT_FRAME:AddMessage(this:GetText().." 어그로 알리미", 1.0, 0.5, 0.5);
	end
end

function Fi_Aggro_Boss_List_Type()
	local info = {};
	local selectedIdx = 1;
	for i=1,table.getn(Fi_Boss_List) do
		info.text = Fi_Boss_List[i];
		info.func = Fi_Aggro_Boss_List_Click;
		if (info.text == Boss_Select) then
			selectedIdx = i;			
			info.checked = 1;
		end
		UIDropDownMenu_AddButton(info);
	end
	UIDropDownMenu_SetSelectedID(Fi_Aggro_Boss_List, selectedIdx);
end

function Fi_Aggro_Boss_List_Click()
	local selectedIdx = this:GetID();
	UIDropDownMenu_SetSelectedID(Fi_Aggro_Boss_List, selectedIdx);	
	UIDropDownMenu_SetSelectedID(Bl_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Co_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(On_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Zu_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(AR_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(AT_Aggro_Boss_List, 1);
	SpellAlert_AggroName = nil;	
	if not(this:GetText() == "보스 선택") then			
		Boss_Select = this:GetText();
		DEFAULT_CHAT_FRAME:AddMessage(this:GetText().." 어그로 알리미", 1.0, 0.5, 0.5);
	end	
end

function On_Aggro_Boss_List_Type()
	local info = {};
	local selectedIdx = 1;
	for i=1,table.getn(On_Boss_List) do
		info.text = On_Boss_List[i];
		info.func = On_Aggro_Boss_List_Click;
		if (info.text == Boss_Select) then
			selectedIdx = i;			
			info.checked = 1;
		end
		UIDropDownMenu_AddButton(info);
	end
	UIDropDownMenu_SetSelectedID(On_Aggro_Boss_List, selectedIdx);
end

function On_Aggro_Boss_List_Click()
	local selectedIdx = this:GetID();
	UIDropDownMenu_SetSelectedID(On_Aggro_Boss_List, selectedIdx);	
	UIDropDownMenu_SetSelectedID(Bl_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Co_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Fi_Aggro_Boss_List, 1);	
	UIDropDownMenu_SetSelectedID(Zu_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(AR_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(AT_Aggro_Boss_List, 1);
	SpellAlert_AggroName=nil;	
	if not(this:GetText() == "보스 선택") then			
		Boss_Select = this:GetText();
		DEFAULT_CHAT_FRAME:AddMessage(this:GetText().." 어그로 알리미", 1.0, 0.5, 0.5);
	end
end

function Zu_Aggro_Boss_List_Type()
	local info = {};
	local selectedIdx = 1;
	for i=1,table.getn(Zu_Boss_List) do
		info.text = Zu_Boss_List[i];
		info.func = Zu_Aggro_Boss_List_Click;
		if (info.text == Boss_Select) then
			selectedIdx = i;			
			info.checked = 1;
		end
		UIDropDownMenu_AddButton(info);
	end
	UIDropDownMenu_SetSelectedID(Zu_Aggro_Boss_List, selectedIdx);
end

function Zu_Aggro_Boss_List_Click()
	local selectedIdx = this:GetID();
	UIDropDownMenu_SetSelectedID(Zu_Aggro_Boss_List, selectedIdx);
	UIDropDownMenu_SetSelectedID(Co_Aggro_Boss_List, 1);	
	UIDropDownMenu_SetSelectedID(Bl_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Fi_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(On_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(AR_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(AT_Aggro_Boss_List, 1);
	SpellAlert_AggroName = nil;	
	if not(this:GetText() == "보스 선택") then			
		Boss_Select = this:GetText();
		DEFAULT_CHAT_FRAME:AddMessage(this:GetText().." 어그로 알리미", 1.0, 0.5, 0.5);
	end
end

function AR_Aggro_Boss_List_Type()
	local info = {};
	local selectedIdx = 1;
	for i=1,table.getn(AR_Boss_List) do
		info.text = AR_Boss_List[i];
		info.func = AR_Aggro_Boss_List_Click;
		if (info.text == Boss_Select) then
			selectedIdx = i;			
			info.checked = 1;
		end
		UIDropDownMenu_AddButton(info);
	end
	UIDropDownMenu_SetSelectedID(AR_Aggro_Boss_List, selectedIdx);
end

function AR_Aggro_Boss_List_Click()
	local selectedIdx = this:GetID();
	UIDropDownMenu_SetSelectedID(AR_Aggro_Boss_List, selectedIdx);
	UIDropDownMenu_SetSelectedID(Co_Aggro_Boss_List, 1);	
	UIDropDownMenu_SetSelectedID(Bl_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Fi_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(On_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Zu_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(AT_Aggro_Boss_List, 1);
	SpellAlert_AggroName = nil;	
	if not(this:GetText() == "보스 선택") then			
		Boss_Select = this:GetText();
		DEFAULT_CHAT_FRAME:AddMessage(this:GetText().." 어그로 알리미", 1.0, 0.5, 0.5);
	end
end

function AT_Aggro_Boss_List_Type()
	local info = {};
	local selectedIdx = 1;
	for i=1,table.getn(AT_Boss_List) do
		info.text = AT_Boss_List[i];
		info.func = AT_Aggro_Boss_List_Click;
		if (info.text == Boss_Select) then
			selectedIdx = i;			
			info.checked = 1;
		end
		UIDropDownMenu_AddButton(info);
	end
	UIDropDownMenu_SetSelectedID(AT_Aggro_Boss_List, selectedIdx);
end

function AT_Aggro_Boss_List_Click()
	local selectedIdx = this:GetID();
	UIDropDownMenu_SetSelectedID(AT_Aggro_Boss_List, selectedIdx);
	UIDropDownMenu_SetSelectedID(Co_Aggro_Boss_List, 1);	
	UIDropDownMenu_SetSelectedID(Bl_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Fi_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(On_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(Zu_Aggro_Boss_List, 1);
	UIDropDownMenu_SetSelectedID(AR_Aggro_Boss_List, 1);
	SpellAlert_AggroName = nil;	
	if not(this:GetText() == "보스 선택") then			
		Boss_Select = this:GetText();
		DEFAULT_CHAT_FRAME:AddMessage(this:GetText().." 어그로 알리미", 1.0, 0.5, 0.5);
	end
end

function SpellAlert_Aggro_Update()
	for Aggro_i=1, GetNumRaidMembers() do
		if (UnitName("raid"..Aggro_i.."target") == Boss_Select) then 
			if (UnitCanAttack("raid"..Aggro_i, "raid"..Aggro_i.."target") and (UnitName("raid"..Aggro_i) == UnitName("raid"..Aggro_i.."targettarget"))) then
				if not(SpellAlert_AggroName == UnitName("raid"..Aggro_i)) then
					SendChatMessage("현재 "..Boss_Select.."의 타겟팅은 ["..UnitName("raid"..Aggro_i.."targettarget").."]님 입니다.", "RAID");
					--SendChatMessage("현재 "..Boss_Select.."의 타겟팅은 ["..UnitName("raid"..Aggro_i.."targettarget").."]님 입니다.", "RAID");
					--SendChatMessage("현재 "..Boss_Select.."의 타겟팅은 ["..UnitName("raid"..Aggro_i.."targettarget").."]님 입니다.", "WHISPER", this.language,UnitName("raid"..Aggro_i.."targettarget"));
					SpellAlert_AggroName = UnitName("raid"..Aggro_i);
					if (SpellAlert_AggroName == UnitName("player")) then
						SpellAlert_Sound(4);
						UIErrorsFrame:AddMessage("["..Boss_Select.."] :::  당신을 향해 옵니다.", 1.0, 0.0, 0.0, 1.0, UIERRORS_HOLD_TIME);
					end
				end
			end
		end
	end	
end

function SpellAlert_Aggro_AssiMod()
	for Aggro_i=1, GetNumRaidMembers() do
		if (UnitName("raid"..Aggro_i.."target") == Boss_Select) then 
			if (UnitCanAttack("raid"..Aggro_i, "raid"..Aggro_i.."target") and (UnitName("raid"..Aggro_i) == UnitName("raid"..Aggro_i.."targettarget"))) then
				if not(SpellAlert_AggroName == UnitName("raid"..Aggro_i)) then
					SpellAlert_AggroName = UnitName("raid"..Aggro_i);
					if (SpellAlert_AggroName == UnitName("player")) then
						SpellAlert_Sound(4);
						UIErrorsFrame:AddMessage("["..Boss_Select.."] :::  당신을 향해 옵니다.", 1.0, 0.0, 0.0, 1.0, UIERRORS_HOLD_TIME);
					end
				end
			end
		end
	end	
end

function Assist_Aggro()
	if not(SpellAlert_AggroName == nil) then
		TargetByName(SpellAlert_AggroName);
		UIErrorsFrame:AddMessage(SpellAlert_AggroName.."님 지원함!!!", 1.0, 1.0, 1.0, 1.0, UIERRORS_HOLD_TIME);
	else
		UIErrorsFrame:AddMessage("어그로를 먹은 대상이 없습니다.", 1.0, 0.0, 0.0, 1.0, UIERRORS_HOLD_TIME);
	end	
end
