local addonName, addon = ...
local L = LibStub("AceLocale-3.0"):GetLocale(addonName)

local menu = {}

local dropdown = LibStub("Libra"):CreateDropdown("Menu")
dropdown.initialize = function(self, level)
	for index = 1, #menu do
		local value = menu[index]
		if value.text then
			value.index = index
			UIDropDownMenu_AddButton(value, level)
		end
	end
end

local function getMenuText(text, command)
	if not addon.db.profile.hideKeyBindings and command and GetBindingKey(command) then
		-- We cannot inline this because GetBindingKey may return more than one binding for a single command,
		--	and so when it returns more than one it messes up GetBindingText and we only need the first one.
		local binding = GetBindingKey(command)
		return text .. " (|cff82c5ff" .. GetBindingText(binding) .. FONT_COLOR_CODE_CLOSE .. ")"
	else
		return text
	end
end

local function insertItem(text, func)
	table.insert(menu, {
		text = text,
		notCheckable = 1,
		func = func,
	})
end

local function insertSeparator()
	if #menu > 0 then
		table.insert(menu, {
			text = "",
			notCheckable = 1,
			disabled = true,
		})
	end
end

local function insertInterface(name, order)
	local interface = addon.display:GetInterface(name)
	if interface and order > 0 then
		local inCombat = UnitAffectingCombat("player")
		
		local entry = {
			order = order,
			name = name,
			text = getMenuText(interface.title, interface.command),
			icon = interface.icon,
			notCheckable = 1,
			disabled = inCombat and interface.secure
		}
		
		if not inCombat and interface.button and interface.secure then
			entry.attributes = {
				["type"] = "click",
				["clickbutton"] = interface.button,
			}
		elseif interface.special then
			entry.func = function() addon.display:DisplayInterface(name) end
		else
			entry.func = interface.func
		end
		
		table.insert(menu, entry)
	end
end

local function comp(a, b)
	return a.order < b.order
end

function addon:BuildMenu()
	if not self.updateMenu then
		return
	end
	
	wipe(menu)
	for name, order in pairs(self.db.profile.menu) do
		insertInterface(name, order)
	end
	table.sort(menu, comp)
	
	if not self.db.profile.hideLogoutButtons then
		insertSeparator()
		insertItem(LOGOUT, function() 
			Logout() 
			HideUIPanel(GameMenuFrame) 
		end)
	end
	
	if not self.db.profile.hideLogoutButtons then
		insertItem(EXIT_GAME, function() 
			Quit()
			HideUIPanel(GameMenuFrame)
		end)
	end
	
	if not self.db.profile.hideReloadUIButton then
		insertSeparator()
		insertItem(L["Reload UI"], function() 
			ReloadUI()
		end)
	end
	
	if not self.db.profile.hideOptionsButton then
		insertSeparator()
		insertItem(L["Options"], function() 
			if not InterfaceOptionsFrame:IsVisible() then
				HideUIPanel(GameMenuFrame)
				InterfaceOptionsFrame_OpenToCategory(addon.name)
				InterfaceOptionsFrame_OpenToCategory(addon.name)
			else
				HideUIPanel(InterfaceOptionsFrame)
			end
		end)
	end
	
	self.updateMenu = false
end

function addon:UpdateMenu()
	dropdown:Close()
	addon.updateMenu = true
end

function addon:OpenMenu(frame, button)
	local name
	local isLeftButton = self.db.profile.mouseButton == 1 and button == "LeftButton"
	local isRightButton = self.db.profile.mouseButton == 2 and button == "RightButton"
	
	for _, v in pairs(self.db.profile.accessibility) do
		if not IsModifierKeyDown() and v.modifier == 1
		or IsAltKeyDown() and v.modifier == 4 
		or IsControlKeyDown() and v.modifier == 3 
		or IsShiftKeyDown() and v.modifier == 2 then
			name = v.name
			break
		end
	end
	
	if isLeftButton or isRightButton then
		addon.display:DisplayInterface(name)
	else
		dropdown:Toggle(nil, frame)
	end
end

function addon:IsMenuVisible()
	return dropdown:IsShown()
end