local addonName, addon = ...

_G[addonName] = addon

--[[-----------------------------------------------------------------------------
Guild and Friend Invite DropDown Menu Buttons
-------------------------------------------------------------------------------]]
local GUIDropDownAdd = CreateFrame("Frame","GUIDropDownAdd")
GUIDropDownAdd:SetScript("OnEvent", function() hooksecurefunc("UnitPopup_ShowMenu", GDropDownCheck) end)
GUIDropDownAdd:RegisterEvent("PLAYER_LOGIN")

local PopupUnits = {"PARTY", "PLAYER", "RAID_PLAYER", "RAID", "FRIEND", "TEAM", "CHAT_ROSTER", "TARGET", "FOCUS"}

local AddFriendButtonInfo = {
	text = "Add Friend",
	value = "ADD_FRIEND",
	func = function() AddFriend(UIDROPDOWNMENU_OPEN_MENU.name) end,
	notCheckable = 1,
}

local RemoveFriendButtonInfo = {
	text = "Remove Friend",
	value = "REMOVE_FRIEND",
	func = function() RemoveFriend(UIDROPDOWNMENU_OPEN_MENU.name) end,
	notCheckable = 1,
}

local GuildInviteButtonInfo = {
	text = "Guild Invite",
	value = "GUILD_INVITE",
	func = function() GuildInvite(UIDROPDOWNMENU_OPEN_MENU.name) end,
	notCheckable = 1,
}

local CancelButtonInfo = {
	text = "Cancel",
	value = "CANCEL",
	notCheckable = 1
}

function GDropDownCheck()		

	local GoodUnit = false
	for i=1, #PopupUnits do	
		if OPEN_DROPDOWNMENUS[1]["which"] == PopupUnits[i] then	
			GoodUnit = true
		end
	end
	
	if UIDROPDOWNMENU_OPEN_MENU["unit"] == "target" and ((not UnitIsPlayer("target")) or (not UnitIsFriend("player", "target"))) then
		GoodUnit = false
	end
	

	--local SetFocusButton = getglobal("DropDownList1Button2")
		--SetFocusButton:Hide() 
		--DropDownList1.numButtons = DropDownList1.numButtons - 1
	
	if GoodUnit then	
		local CancelButtonFrame = getglobal("DropDownList1Button"..DropDownList1.numButtons)
		CancelButtonFrame:Hide() 
		DropDownList1.numButtons = DropDownList1.numButtons - 1
		
		local PossibleButton1 = getglobal("DropDownList1Button"..(DropDownList1.numButtons))
		local PossibleButton2 = getglobal("DropDownList1Button"..(DropDownList1.numButtons)-1)

		local IsAlreadyFriend = false
		for z=1, GetNumFriends() do
			if GetFriendInfo(z) == UIDROPDOWNMENU_OPEN_MENU["name"] or UIDROPDOWNMENU_OPEN_MENU["name"] == UnitName("player") then
				IsAlreadyFriend = true
			end
		end
		
		if not IsAlreadyFriend then				-- is this person not already your friend?
			if PossibleButton2["value"] ~= "ADD_FRIEND" and PossibleButton1["value"] ~= "ADD_FRIEND" then
				UIDropDownMenu_AddButton(AddFriendButtonInfo)			-- Add the button
			end
		else
			if PossibleButton2["value"] ~= "REMOVE_FRIEND" and PossibleButton1["value"] ~= "REMOVE_FRIEND" then
				UIDropDownMenu_AddButton(RemoveFriendButtonInfo)
			end
		end

		if PossibleButton1["value"] ~= "GUILD_INVITE" and PossibleButton2["value"] ~= "GUILD_INVITE" then
			if IsInGuild() and CanGuildInvite() then
				local IsAlreadyGuilded = false
				for z=1, GetNumGuildMembers() do
					if GetGuildInfo(z) == UIDROPDOWNMENU_OPEN_MENU["name"] or UIDROPDOWNMENU_OPEN_MENU["name"] == UnitName("player") then
							IsAlreadyGuilded = true
					end
				end
				if not IsAlreadyGuilded then				-- is this person not already your friend?
					UIDropDownMenu_AddButton(GuildInviteButtonInfo)			-- Add the button
				end
			end
		end

		UIDropDownMenu_AddButton(CancelButtonInfo)
	
	end
end
