--[[
HoloFriends addon created by Holo, continued by Zappster, followed by Andymon

Get the latest version at wow.curse.com

See HoloFriends_change.log for more informations  
]]

--[[

This file defines pop-up-menus for *_friends and *_ignore

]]

HoloFriends_MenuModDone = false;


-- functions to manage drop down menu background
function HoloFriends_DropDownBG_Init()
	-- Add a background texture to the default pull down menus
	getglobal("DropDownList1"):CreateTexture("DropDownList1_HoloFriendsBG", "BACKGROUND");
end


function HoloFriends_DropDownBG_Show()
	if ( not HOLOFRIENDS_OPTIONS.ShowDropdownBG and not HOLOFRIENDS_OPTIONS.ShowDropdownAllBG ) then
		return;
	end

	-- Add a black backgraund to the default pull down menu
	local listFrame = getglobal("DropDownList1");
	local tex = getglobal("DropDownList1_HoloFriendsBG");
	tex:SetTexture([[Interface\ChatFrame\ChatFrameBackground]]);
	tex:SetPoint("TOPLEFT",listFrame,"TOPLEFT",4,-5);
	tex:SetPoint("BOTTOMRIGHT",listFrame,"BOTTOMRIGHT",-4,5);
	tex:SetVertexColor(0.0, 0.0, 0.0);
end


function HoloFriends_DropDownBG_Hide()
	if ( HOLOFRIENDS_OPTIONS.ShowDropdownAllBG ) then
		return;
	end

	-- remove background texture from default drop down menu
	getglobal("DropDownList1_HoloFriendsBG"):SetTexture(nil);
	getglobal("DropDownList1_HoloFriendsBG"):ClearAllPoints();
end


-- BEGIN OF LIST DROP DOWN MENU

function HoloFriends_InsertDropDown()
	-- list drop down
	UnitPopupButtons["HOLOFRIENDS_ADDCOMMENT"]  = { text = TEXT(HOLOFRIENDS_WINDOWMAINADDCOMMENT), dist = 0 };
	UnitPopupButtons["HOLOFRIENDS_RENAMEGROUP"] = { text = TEXT(HOLOFRIENDS_WINDOWMAINRENAMEGROUP), dist = 0 };
	UnitPopupButtons["HOLOFRIENDS_REMOVEGROUP"] = { text = TEXT(HOLOFRIENDS_WINDOWMAINREMOVEGROUP), dist = 0 };
	UnitPopupButtons["HOLOFRIENDS_ADDFRIEND"]   = { text = TEXT(ADD_FRIEND), dist = 0 };
	UnitPopupButtons["HOLOFRIENDS_ADDIGNORE"]   = { text = TEXT(IGNORE_PLAYER), dist = 0 };
	UnitPopupButtons["HOLOFRIENDS_WHOREQUEST"]  = { text = TEXT(HOLOFRIENDS_WINDOWMAINWHOREQUEST), dist = 0 };

	UnitPopupMenus["HOLOFRIENDS_LIST"] = {	"WHISPER",
						"INVITE",
						"HOLOFRIENDS_ADDCOMMENT",
						"HOLOFRIENDS_RENAMEGROUP",
						"HOLOFRIENDS_REMOVEGROUP",
						"HOLOFRIENDS_WHOREQUEST",
						"CANCEL" };

	--extend the existing drop down menus
	HoloFriends_ExtendDropdown(UnitPopupMenus["FRIEND"]);
	if ( HOLOFRIENDS_OPTIONS.MenuMod ) then
		HoloFriends_MenuModDone = true;
		HoloFriends_ExtendDropdown(UnitPopupMenus["PLAYER"]);
		HoloFriends_ExtendDropdown(UnitPopupMenus["PARTY"]);
		HoloFriends_ExtendDropdown(UnitPopupMenus["RAID"]);
	end
end


function HoloFriends_ExtendDropdown(dropdown)
	if ( not (dropdown and type(dropdown) == "table") ) then
		return;
	end

	table.insert(dropdown, table.getn(dropdown), "HOLOFRIENDS_ADDFRIEND");
	table.insert(dropdown, table.getn(dropdown), "HOLOFRIENDS_ADDIGNORE");
	table.insert(dropdown, table.getn(dropdown), "HOLOFRIENDS_WHOREQUEST");
end


function HoloIgnore_InsertDropDown()
	UnitPopupMenus["HOLOIGNORE_LIST"] = {	"HOLOFRIENDS_ADDCOMMENT",
						"HOLOFRIENDS_RENAMEGROUP",
						"HOLOFRIENDS_REMOVEGROUP",
						"CANCEL" };
end

-- END OF LIST DROP DOWN MENU


-- HoloFriends specific functions

function HoloFriends_ListDropDown_Initialize()
	UnitPopup_ShowMenu(UIDROPDOWNMENU_OPEN_MENU,
			   "HOLOFRIENDS_LIST",
			   nil,
			   HoloFriendsDropDown.name,
			   HoloFriendsDropDown.userData);
end


function HoloFriends_ShowListDropdown(id)
	local list = HoloFriends_GetList();

	HideDropDownMenu(1);

	HoloFriends_DropDownBG_Show();

	HoloFriendsDropDown.initialize = HoloFriends_ListDropDown_Initialize;
	HoloFriendsDropDown.displayMode = "MENU";
	HoloFriendsDropDown.name = HoloFriendsLists_GetName(list, id);
	HoloFriendsDropDown.userData = id;
	ToggleDropDownMenu(1, nil, HoloFriendsDropDown, "cursor");

	-- hide the invite, whisper, and who buttons
	if ( not HoloFriendsLists_IsConnected(list, id) ) then
		UIDropDownMenu_DisableButton(1, 2);
		UIDropDownMenu_DisableButton(1, 3);
		UIDropDownMenu_DisableButton(1, 7);
	end

	-- hide the remove, and rename group button, if no group
	if ( not HoloFriendsFuncs_IsGroup(list, id) or 
	     HoloFriendsLists_GetGroup(list, id) == FRIENDS )
	then
		UIDropDownMenu_DisableButton(1, 5);
		UIDropDownMenu_DisableButton(1, 6);
	end
end

-- end of HoloFriends specific functions


-- HoloIgnore specific functions

function HoloIgnore_ListDropDown_Initialize()
	UnitPopup_ShowMenu(UIDROPDOWNMENU_OPEN_MENU,
			   "HOLOIGNORE_LIST",
			   nil,
			   HoloIgnoreDropDown.name,
			   HoloIgnoreDropDown.userData);
end


function HoloIgnore_ShowListDropdown(id)
	HideDropDownMenu(1);

	HoloFriends_DropDownBG_Show();

	HoloIgnoreDropDown.initialize = HoloIgnore_ListDropDown_Initialize;
	HoloIgnoreDropDown.displayMode = "MENU";
	HoloIgnoreDropDown.name = HoloFriendsLists_GetName(HoloIgnore_GetList(), id);
	HoloIgnoreDropDown.userData = id;
	ToggleDropDownMenu(1, nil, HoloIgnoreDropDown, "cursor");

	-- hide the remove, and rename group button, if no group
	if( not HoloFriendsFuncs_IsGroup(HoloIgnore_GetList(), id) or 
	    HoloFriendsLists_GetGroup(HoloIgnore_GetList(), id) == IGNORE )
	then
		UIDropDownMenu_DisableButton(1, 3);
		UIDropDownMenu_DisableButton(1, 4);
	end
end

-- end of HoloFriends specific functions
