--[[--

*Note that this file is currently in a slow transition
towards becoming a cleaner generic library, so we advice 
against manually manipulating it as it'll change frequently.
-Lars

--]]--

local ADDON, Private = ...
local Colors = Private.Colors

local Core = CogWheel("LibModule"):GetModule(ADDON)
if (not Core) then 
	return 
end

local Module = Core:NewModule("OptionsMenu", "HIGH", "LibMessage", "LibEvent", "LibDB", "LibFrame", "LibSound", "LibTooltip")
local Layout, L, MenuTable

-- Registries
Module.buttons = Module.buttons or {}
Module.menus = Module.menus or {}
Module.toggles = Module.toggles or {}
Module.siblings =  Module.siblings or {}
Module.windows = Module.windows or {}

-- Shortcuts
local Buttons = Module.buttons
local Menus = Module.menus
local Toggles = Module.toggles
local Siblings = Module.siblings
local Windows = Module.windows

-- Lua API
local _G = _G
local math_min = math.min
local table_insert = table.insert

-- Secure script snippets
local secureSnippets = {
	menuToggle = [=[
		local window = self:GetFrameRef("OptionsMenu");
		if window:IsShown() then
			window:Hide();
		else
			local window2 = self:GetFrameRef("MicroMenu"); 
			if (window2 and window2:IsShown()) then 
				window2:Hide(); 
			end 
			window:Show();
			window:RegisterAutoHide(.75);
			window:AddToAutoHide(self);
			local autohideCounter = 1
			local autohideFrame = window:GetFrameRef("autohide"..autohideCounter);
			while autohideFrame do 
				window:AddToAutoHide(autohideFrame);
				autohideCounter = autohideCounter + 1;
				autohideFrame = window:GetFrameRef("autohide"..autohideCounter);
			end 
		end
	]=],
	windowToggle = [=[
		local window = self:GetFrameRef("Window"); 
		if window:IsShown() then 
			window:Hide(); 
			window:CallMethod("OnHide");
		else 
			window:Show(); 
			window:CallMethod("OnShow");
			local counter = 1
			local sibling = window:GetFrameRef("Sibling"..counter);
			while sibling do 
				if sibling:IsShown() then 
					sibling:Hide(); 
					sibling:CallMethod("OnHide");
				end 
				counter = counter + 1;
				sibling = window:GetFrameRef("Sibling"..counter);
			end 
		end 
	]=],
	buttonClick = [=[
		local updateType = self:GetAttribute("updateType"); 
		if (updateType == "SET_VALUE") then 

			-- Figure out the window's attribute name for this button's attached setting
			local optionDB = self:GetAttribute("optionDB"); 
			local optionName = self:GetAttribute("optionName"); 
			local attributeName = "DB_"..optionDB.."_"..optionName; 

			-- retrieve the new value of the setting
			local window = self:GetFrameRef("Window"); 
			local value = self:GetAttribute("optionArg1"); 

			-- store the new setting on the button
			self:SetAttribute("optionValue", value); 

			-- store the new setting on the window
			window:SetAttribute(attributeName, value); 

			-- Feed the new values into the lua db
			self:CallMethod("FeedToDB"); 

			-- Fire a secure settings update on whatever this setting is attached to
			local proxyUpdater = self:GetFrameRef("proxyUpdater"); 
			if proxyUpdater then 
				proxyUpdater:SetAttribute("change-"..optionName, value); 
			end 

			-- Fire lua post updates to menu buttons
			self:CallMethod("Update"); 

			-- Fire lua post updates to siblings, if any, 
			-- as this could be a multi-option.			
			local counter = 1
			local sibling = self:GetFrameRef("Sibling"..counter);
			while sibling do 
				--if sibling:IsShown() then 
					sibling:CallMethod("Update");
				--end 
				counter = counter + 1;
				sibling = self:GetFrameRef("Sibling"..counter);
			end 


		elseif (updateType == "GET_VALUE") then 

		elseif (updateType == "TOGGLE_VALUE") then 
			
			-- Figure out the window's attribute name for this button's attached setting
			local optionDB = self:GetAttribute("optionDB"); 
			local optionName = self:GetAttribute("optionName"); 
			local attributeName = "DB_"..optionDB.."_"..optionName; 

			-- retrieve the old value of the setting
			local window = self:GetFrameRef("Window"); 
			local value = not window:GetAttribute(attributeName); 

			-- store the new setting on the button
			self:SetAttribute("optionValue", not self:GetAttribute("optionValue")); 

			-- store the new setting on the window
			window:SetAttribute(attributeName, value); 

			-- Feed the new values into the lua db
			self:CallMethod("FeedToDB"); 

			-- Fire a secure settings update on whatever this setting is attached to
			local proxyUpdater = self:GetFrameRef("proxyUpdater"); 
			if proxyUpdater then 
				proxyUpdater:SetAttribute("change-"..optionName, self:GetAttribute("optionValue")); 
			end 

			-- Fire lua post updates to menu buttons
			self:CallMethod("Update"); 

			-- Enable/Disable other menu buttons as needed 
			for i=1,select("#", window:GetChildren()) do 

				-- Find the child menu buttons that have a slave setting
				local child = select(i, window:GetChildren()); 
				if (child and child:GetAttribute("isSlave")) then 

					-- figure out the window attribute name for the current menu button's attached setting
					local childAttributeName = "DB_"..child:GetAttribute("slaveDB").."_"..child:GetAttribute("slaveKey"); 

					-- if the menu button is slave to the window's attribute, enable/disable the menu button as needed
					if (childAttributeName == attributeName) then 
						if value then 
							child:Enable(); 
						else 
							child:Disable(); 

							-- Hide any open menu windows belonging to this child button.
							-- This also fires a callback to update the button texture.
							local window = child:GetFrameRef("Window");
							if window and window:IsShown() then
								window:Hide(); 
								window:CallMethod("OnHide");
							end 
						end
					end
				end 
			end 
		end 
	]=]
}

-- Menu Template
local Menu = {}
local Menu_MT = { __index = Menu }

-- Toggle Button template
local Toggle = Module:CreateFrame("CheckButton", nil, "UICenter", "SecureHandlerClickTemplate")
local Toggle_MT = { __index = Toggle }

-- Container template
local Window = Module:CreateFrame("Frame", nil, "UICenter", "SecureHandlerAttributeTemplate")
local Window_MT = { __index = Window }

-- Entry template
local Button = Module:CreateFrame("CheckButton", nil, "UICenter", "SecureHandlerClickTemplate")
local Button_MT = { __index = Button }

local MenuWindow_OnShow = function(self) 
	local tooltip = Module:GetOptionsMenuTooltip()
	local button = Module:GetToggleButton()
	if (tooltip:IsShown() and (tooltip:GetOwner() == button)) then 
		tooltip:Hide()
	end 
end

local MenuWindow_OnHide = function(self) 
	local tooltip = Module:GetOptionsMenuTooltip()
	local toggle = Module:GetToggleButton()
	if (toggle:IsMouseOver(0,0,0,0) and ((not tooltip:IsShown()) or (tooltip:GetOwner() ~= toggle))) then 
		toggle:OnEnter()
	end 
end

Toggle.OnEnter = function(self)
	if (not self.leftButtonTooltip) and (not self.rightButtonTooltip) then 
		return 
	end 
	local tooltip = Module:GetOptionsMenuTooltip()
	local window = Module:GetConfigWindow()
	if window:IsShown() then 
		if (tooltip:IsShown() and (tooltip:GetOwner() == self)) then 
			tooltip:Hide()
		end 
		return 
	end 
	tooltip:SetDefaultAnchor(self)
	tooltip:AddLine(L["Main Menu"], Colors.title[1], Colors.title[2], Colors.title[3])
	tooltip:AddLine(L["Click here to get access to game panels."], Colors.offwhite[1], Colors.offwhite[2], Colors.offwhite[3], true)
	if self.leftButtonTooltip then 
		tooltip:AddLine(self.leftButtonTooltip, Colors.quest.green[1], Colors.quest.green[2], Colors.quest.green[3])
	end 
	if self.rightButtonTooltip then 
		tooltip:AddLine(self.rightButtonTooltip, Colors.quest.green[1], Colors.quest.green[2], Colors.quest.green[3])
	end 
	tooltip:Show()
end

Toggle.OnLeave = function(self)
	local tooltip = Module:GetOptionsMenuTooltip()
	tooltip:Hide() 
end

Window.AddButton = function(self, text, updateType, optionDB, optionName, ...)
	local option = setmetatable(self:CreateFrame("CheckButton", nil, "SecureHandlerClickTemplate"), Button_MT)
	option:SetSize(Layout.MenuButtonSize[1]*Layout.MenuButtonSizeMod, Layout.MenuButtonSize[2]*Layout.MenuButtonSizeMod)
	option:SetPoint("BOTTOMRIGHT", -Layout.MenuButtonSpacing, Layout.MenuButtonSpacing + (Layout.MenuButtonSize[2]*Layout.MenuButtonSizeMod + Layout.MenuButtonSpacing)*(self.numButtons))

	option:HookScript("OnEnable", Button.OnEnable)
	option:HookScript("OnDisable", Button.OnDisable)
	option:HookScript("OnShow", Button.OnShow)
	option:HookScript("OnHide", Button.OnHide)
	option:HookScript("OnMouseDown", Button.OnMouseDown)
	option:HookScript("OnMouseUp", Button.OnMouseUp)
	option:HookScript("OnEnter", Button.Update)
	option:HookScript("OnLeave", Button.Update)

	option:SetAttribute("updateType", updateType)
	option:SetAttribute("optionDB", optionDB)
	option:SetAttribute("optionName", optionName)

	option:SetFrameRef("Window", self)

	for i = 1, select("#", ...) do 
		local value = select(i, ...)
		option:SetAttribute("optionArg"..i, value)
		option["optionArg"..i] = value
	end 

	option.updateType = updateType
	option.optionDB = optionDB
	option.optionName = optionName

	if (updateType == "SET_VALUE") or (updateType == "GET_VALUE") or (updateType == "TOGGLE_VALUE") then 
		option:SetAttribute("_onclick", secureSnippets.buttonClick)
	end

	if (not Module.optionCallbacks) then 
		Module.optionCallbacks = {}
	end 

	Module.optionCallbacks[option] = self

	if Layout.MenuButton_PostCreate then 
		Layout.MenuButton_PostCreate(option, text, updateType, optionDB, optionName, ...)
	end

	self.numButtons = self.numButtons + 1
	self.buttons[self.numButtons] = option

	self:PostUpdateSize()
	self:UpdateSiblings()

	return option
end 

Window.ParseOptionsTable = function(self, tbl, parentLevel)
	local level = (parentLevel or 1) + 1
	for id,data in ipairs(tbl) do
		local button = self:AddButton(data.title, data.type, data.configDB, data.configKey, data.optionArgs and unpack(data.optionArgs))
		button.enabledTitle = data.enabledTitle
		button.disabledTitle = data.disabledTitle
		button.proxyModule = data.proxyModule
		if data.isSlave then 
			button:SetAsSlave(data.slaveDB, data.slaveKey)
		end 
		if data.hasWindow then 
			local window = button:CreateWindow(level)
			if data.buttons then 
				window:ParseOptionsTable(data.buttons)
			end 
		end
	end
end

Window.UpdateSiblings = function(self)
	for id,button in ipairs(self.buttons) do 
		local siblingCount = 0
		for i = 1, self.numButtons do 
			if (i ~= id) then 
				siblingCount = siblingCount + 1
				button:SetFrameRef("Sibling"..siblingCount, self.buttons[i])
			end 
		end 
	end
	if self.windows then 
		for id,button in ipairs(self.windows) do 
			local siblingCount = 0
			for i = 1, self.numWindows do 
				if (i ~= id) then 
					siblingCount = siblingCount + 1
					button:SetFrameRef("Sibling"..siblingCount, self.windows[i])
				end 
			end 
		end
	end 
end

Window.OnHide = function(self)
	if Layout.MenuWindow_OnHide then 
		return Layout.MenuWindow_OnHide(self)
	end 
end

Window.OnShow = function(self)
	if Layout.MenuWindow_OnShow then 
		return Layout.MenuWindow_OnShow(self)
	end 
end

Window.PostUpdateSize = function(self)
	local numButtons = self.numButtons
	self:SetSize(Layout.MenuButtonSize[1]*Layout.MenuButtonSizeMod + Layout.MenuButtonSpacing*2, Layout.MenuButtonSize[2]*Layout.MenuButtonSizeMod*numButtons + Layout.MenuButtonSpacing*(numButtons+1))
end

Button.OnEnable = function(self)
	self:SetAlpha(1)
	self:Update()
end 

Button.OnDisable = function(self)
	self:SetAlpha(.5)
	self:Update()
end 

Button.OnShow = function(self)
	self.isDown = false
	self:Update()
end 

Button.OnHide = function(self)
	self.isDown = false
	self:Update()
end 

Button.OnMouseDown = function(self)
	self.isDown = true
	self:Update()
end 

Button.OnMouseUp = function(self)
	self.isDown = false
	self:Update()
end 

Button.Update = function(self)
	if Layout.MenuButton_PostUpdate then 
		if (self.updateType == "GET_VALUE") then 
			return Layout.MenuButton_PostUpdate(self)
		elseif (self.updateType == "SET_VALUE") then 
			local db = Module:GetConfig(self.optionDB, defaults, "global")
			local option = db[self.optionName]
			return Layout.MenuButton_PostUpdate(self, self.updateType, db, option, option == self.optionArg1)

		elseif (self.updateType == "TOGGLE_VALUE") then 
			local db = Module:GetConfig(self.optionDB, defaults, "global")
			local option = db[self.optionName]
			return Layout.MenuButton_PostUpdate(self, self.updateType, db, option)
		else
			return Layout.MenuButton_PostUpdate(self)
		end 
	end 
end

Button.FeedToDB = function(self)
	if (self.updateType == "SET_VALUE") then 
		Module:GetConfig(self.optionDB, defaults, "global")[self.optionName] = self:GetAttribute("optionValue")

	elseif (self.updateType == "TOGGLE_VALUE") then 
		Module:GetConfig(self.optionDB, defaults, "global")[self.optionName] = self:GetAttribute("optionValue")
	end 
end 

Button.CreateWindow = function(self, level)
	local window = Module:CreateConfigWindowLevel(level, self)
	--window:SetPoint("BOTTOM", Module:GetConfigWindow(), "BOTTOM", 0, 0) -- relative to parent button's window
	window:SetPoint("BOTTOM", self, "BOTTOM", 0, -Layout.MenuButtonSpacing) -- relative to parent button
	window:SetPoint("RIGHT", self, "LEFT", -Layout.MenuButtonSpacing*2, 0)

	if Layout.MenuWindow_CreateBorder then 
		window.Border = Layout.MenuWindow_CreateBorder(window)
	end 

	window.OnHide = Window.OnHide
	window.OnShow = Window.OnShow

	self:SetAttribute("_onclick", secureSnippets.windowToggle)
	self:SetFrameRef("Window", window)

	Module:AddFrameToAutoHide(window)

	local owner = self:GetParent()
	if (not owner.windows) then 
		owner.numWindows = 0
		owner.windows = {}
	end 

	owner.numWindows = owner.numWindows + 1
	owner.windows[owner.numWindows] = window
	owner:UpdateSiblings()
	
	return window
end

Button.SetAsSlave = function(self, slaveDB, slaveKey)
	self.slaveDB = slaveDB
	self.slaveKey = slaveKey
	self.isSlave = true
	self:SetAttribute("slaveDB", slaveDB)
	self:SetAttribute("slaveKey", slaveKey)
	self:SetAttribute("isSlave", true)
end

Module.CreateConfigWindowLevel = function(self, level, parent)
	local frameLevel = 10 + (level-1)*5
	local window = setmetatable(self:CreateFrame("Frame", nil, parent or "UICenter", "SecureHandlerAttributeTemplate"), Window_MT)
	window:Hide()
	window:EnableMouse(true)
	window:SetFrameStrata("DIALOG")
	window:SetFrameLevel(frameLevel)

	window.numButtons = 0
	window.buttons = {}

	if (level > 1) then 
		self:AddFrameToAutoHide(window)
	end 

	return window
end

Module.GetOptionsMenuTooltip = function(self)
	return self:GetTooltip(ADDON.."_OptionsMenuTooltip") or self:CreateTooltip(ADDON.."_OptionsMenuTooltip")
end

Module.GetToggleButton = function(self)
	if (not self.ToggleButton) then 
		local toggleButton = setmetatable(self:CreateFrame("CheckButton", nil, "UICenter", "SecureHandlerClickTemplate"), Toggle_MT)
		toggleButton:SetFrameStrata("DIALOG")
		toggleButton:SetFrameLevel(50)
		toggleButton:SetSize(unpack(Layout.MenuToggleButtonSize))
		toggleButton:Place(unpack(Layout.MenuToggleButtonPlace))
		toggleButton:RegisterForClicks("AnyUp")
		toggleButton:SetScript("OnEnter", Toggle.OnEnter)
		toggleButton:SetScript("OnLeave", Toggle.OnLeave) 
		toggleButton:SetAttribute("_onclick", [[
			if (button == "LeftButton") then
				local leftclick = self:GetAttribute("leftclick");
				if leftclick then
					self:RunAttribute("leftclick", button);
				end
			elseif (button == "RightButton") then 
				local rightclick = self:GetAttribute("rightclick");
				if rightclick then
					self:RunAttribute("rightclick", button);
				end
			end
		]])

		toggleButton.Icon = toggleButton:CreateTexture()
		toggleButton.Icon:SetTexture(Layout.MenuToggleButtonIcon)
		toggleButton.Icon:SetSize(unpack(Layout.MenuToggleButtonIconSize))
		toggleButton.Icon:SetPoint(unpack(Layout.MenuToggleButtonIconPlace))
		toggleButton.Icon:SetVertexColor(unpack(Layout.MenuToggleButtonIconColor))

		self.ToggleButton = toggleButton
	end 
	return self.ToggleButton
end

Module.GetConfigWindow = function(self)
	if (not self.ConfigWindow) then 

		-- create main window 
		local window = self:CreateConfigWindowLevel(1)
		window:Place(unpack(Layout.MenuPlace))
		window:SetSize(unpack(Layout.MenuSize))
		window:EnableMouse(true)
		window:SetScript("OnShow", MenuWindow_OnShow)
		window:SetScript("OnHide", MenuWindow_OnHide)

		if Layout.MenuWindow_CreateBorder then 
			window.Border = Layout.MenuWindow_CreateBorder(window)
		end 

		self.ConfigWindow = window

	end 
	return self.ConfigWindow
end

Module.GetAutoHideReferences = function(self)
	if (not self.AutoHideReferences) then 
		self.AutoHideReferences = {}
	end 
	return self.AutoHideReferences
end

Module.AddFrameToAutoHide = function(self, frame)
	local window = self:GetConfigWindow()
	local hiders = self:GetAutoHideReferences()

	local id = 1 -- targeted id for this autohider
	for frameRef,parent in pairs(hiders) do 
		id = id + 1 -- increase id by 1 for every other frame found
	end 

	-- create a new autohide frame
	local autohideParent = CreateFrame("Frame", nil, window, "SecureHandlerStateTemplate")
	autohideParent:SetPoint("TOPLEFT", frame, "TOPLEFT", -6, 6)
	autohideParent:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 6, -6)

	-- Add it to our registry
	hiders["autohide"..id] = autohideParent
end

Module.AddOptionsToMenuButton = function(self)
	if (not self.addedToMenuButton) then 
		self.addedToMenuButton = true

		local toggleButton = self:GetToggleButton()
		toggleButton:SetFrameRef("OptionsMenu", self:GetConfigWindow())
		toggleButton:SetAttribute("rightclick", secureSnippets.menuToggle)
		for reference,frame in pairs(self:GetAutoHideReferences()) do 
			self:GetConfigWindow():SetFrameRef(reference,frame)
		end 
		toggleButton.rightButtonTooltip = L["%s to toggle Options Menu."]:format(L["<Right-Click>"])
	end
end 

Module.AddOptionsToMenuWindow = function(self)
	if (self.addedToMenuWindow) then 
		return 
	end 
	self:GetConfigWindow():ParseOptionsTable(MenuTable, 1)
	self.addedToMenuWindow = true
end

Module.PostUpdateOptions = function(self, event, ...)
	if (event) then 
		self:UnregisterEvent(event, "PostUpdateOptions")
	end
	if self.optionCallbacks then 
		for option,window in pairs(self.optionCallbacks) do 
			if (option.updateType == "SET_VALUE") then
				local db = self:GetConfig(option.optionDB, defaults, "global")
				local value = db[option.optionName]

				if option.proxyModule then 
					option:SetFrameRef("proxyUpdater", Core:GetModule(option.proxyModule):GetSecureUpdater())
				end 

				option:SetAttribute("optionValue", value)
				option:Update()

			elseif (option.updateType == "TOGGLE_VALUE") then
				local db = self:GetConfig(option.optionDB, defaults, "global")
				local value = db[option.optionName]

				if option.proxyModule then 
					option:SetFrameRef("proxyUpdater", Core:GetModule(option.proxyModule):GetSecureUpdater())
				end 

				option:SetAttribute("optionValue", value)
				option:Update()
			end 
			if (option.isSlave) then 
				local attributeName = "DB_"..option.slaveDB.."_"..option.slaveKey
				local db = self:GetConfig(option.slaveDB, defaults, "global")
				local value = db[option.slaveKey]

				window:SetAttribute(attributeName, value)

				if (value) then 
					option:Enable()
				else
					option:Disable()
				end 

				option:Update()
			end 
		end 
	end 
end 

Module.CreateMenuTable = function(self)
	MenuTable = {}

	-- Actionbars 
	local ActionBarMain = Core:GetModule("ActionBarMain")
	if ActionBarMain and not (ActionBarMain:IsIncompatible() or ActionBarMain:DependencyFailed()) then 
		table_insert(MenuTable, {
			title = L["ActionBars"], type = nil, hasWindow = true, 
			buttons = {
				{
					title = L["More Buttons"], type = nil, hasWindow = true, 
					buttons = {
						{
							title = L["No Extra Buttons"], type = "SET_VALUE", 
							configDB = "ActionBars", configKey = "extraButtonsCount", optionArgs = { 0 }, 
							proxyModule = "ActionBarMain"
						},
						{
							title = L["+%d Buttons"]:format(5), type = "SET_VALUE", 
							configDB = "ActionBars", configKey = "extraButtonsCount", optionArgs = { 5 }, 
							proxyModule = "ActionBarMain"
						},
						{
							title = L["+%d Buttons"]:format(11), type = "SET_VALUE", 
							configDB = "ActionBars", configKey = "extraButtonsCount", optionArgs = { 11 }, 
							proxyModule = "ActionBarMain"
						},
						{
							title = L["+%d Buttons"]:format(17), type = "SET_VALUE", 
							configDB = "ActionBars", configKey = "extraButtonsCount", optionArgs = { 17 }, 
							proxyModule = "ActionBarMain"
						}
					}
				}, 
				{
					title = L["Extra Buttons Visibility"], type = nil, hasWindow = true, 
					buttons = {
						{
							title = L["MouseOver"], 
							type = "SET_VALUE", 
							configDB = "ActionBars", configKey = "extraButtonsVisibility", optionArgs = { "hover" }, 
							proxyModule = "ActionBarMain"
						},
						{
							title = L["MouseOver + Combat"], 
							type = "SET_VALUE", 
							configDB = "ActionBars", configKey = "extraButtonsVisibility", optionArgs = { "combat" }, 
							proxyModule = "ActionBarMain"
						},
						{
							title = L["Always Visible"], 
							type = "SET_VALUE", 
							configDB = "ActionBars", configKey = "extraButtonsVisibility", optionArgs = { "always" }, 
							proxyModule = "ActionBarMain"
						}
					}
				},
				{
					enabledTitle = L["Cast on Down: %s"]:format(L["Enabled"]),
					disabledTitle = L["Cast on Down: %s"]:format(L["Disabled"]),
					type = "TOGGLE_VALUE", hasWindow = false, 
					configDB = "ActionBars", configKey = "castOnDown", 
					proxyModule = "ActionBarMain", 
				}

				--[[
				-- Primary bar options
				{
					title = L["Primary Bar"], type = nil, hasWindow = true, 
					buttons = {
						{
							title = L["Button Count"], type = nil, hasWindow = true, 
							buttons = {
								{
									title = L["%d Buttons"]:format(7), type = "SET_VALUE", 
									configDB = "ActionBars", configKey = "buttonsPrimary", optionArgs = { 1 }, 
									proxyModule = "ActionBarMain"
								},
								{
									title = L["%d Buttons"]:format(10),	type = "SET_VALUE", 
									configDB = "ActionBars", configKey = "buttonsPrimary", optionArgs = { 2 }, 
									proxyModule = "ActionBarMain"
								},
								{
									title = L["%d Buttons"]:format(12), type = "SET_VALUE", 
									configDB = "ActionBars", configKey = "buttonsPrimary", optionArgs = { 3 }, 
									proxyModule = "ActionBarMain"
								}
							}
						},
						{
							title = L["Button Visibility"],	type = "GET_VALUE", hasWindow = true, 
							configDB = "ActionBars", configKey = "buttonsPrimary", optionArgs = { 2, 3 }, 
							buttons = {
								{
									title = L["MouseOver"], 
									type = "SET_VALUE", 
									configDB = "ActionBars", configKey = "visibilityPrimary", optionArgs = { 1 }, 
									proxyModule = "ActionBarMain"
								},
								{
									title = L["MouseOver + Combat"], 
									type = "SET_VALUE", 
									configDB = "ActionBars", configKey = "visibilityPrimary", optionArgs = { 2 }, 
									proxyModule = "ActionBarMain"
								},
								{
									title = L["Always Visible"], 
									type = "SET_VALUE", 
									configDB = "ActionBars", configKey = "visibilityPrimary", optionArgs = { 3 }, 
									proxyModule = "ActionBarMain"
								}
							}
						}
					}
				},
				-- Complimentary bar options
				{
					title = L["Complimentary Bar"], type = nil, hasWindow = true, 
					buttons = {
						{
							enabledTitle = L["Enabled"],
							disabledTitle = L["Disabled"],
							type = "TOGGLE_VALUE", hasWindow = false, 
							configDB = "ActionBars", configKey = "enableComplimentary", 
							proxyModule = "ActionBarMain", 
						},
						{
							title = L["Button Count"], isSlave = true, hasWindow = true, 
							slaveDB = "ActionBars", slaveKey = "enableComplimentary",
							proxyModule = "ActionBarMain",
							buttons = {
								{
									title = L["%d Buttons"]:format(6), 
									type = "SET_VALUE", 
									configDB = "ActionBars", configKey = "buttonsComplimentary", optionArgs = { 1 }, 
									proxyModule = "ActionBarMain"
								},
								{
									title = L["%d Buttons"]:format(12), 
									type = "SET_VALUE", 
									configDB = "ActionBars", configKey = "buttonsComplimentary", optionArgs = { 2 }, 
									proxyModule = "ActionBarMain"
								}
							}
						},
						{
							title = L["Button Visibility"], isSlave = true, hasWindow = true, 
							slaveDB = "ActionBars", slaveKey = "enableComplimentary", 
							buttons = {
								{
									title = L["MouseOver"], 
									type = "SET_VALUE", 
									configDB = "ActionBars", configKey = "visibilityComplimentary", optionArgs = { 1 }, 
									proxyModule = "ActionBarMain"
								},
								{
									title = L["MouseOver + Combat"], 
									type = "SET_VALUE", 
									configDB = "ActionBars", configKey = "visibilityComplimentary", optionArgs = { 2 }, 
									proxyModule = "ActionBarMain"
								},
								{
									title = L["Always Visible"], 
									type = "SET_VALUE", 
									configDB = "ActionBars", configKey = "visibilityComplimentary", optionArgs = { 3 }, 
									proxyModule = "ActionBarMain"
								}
							}
						}
					}
				}
				]]

			}
		})
	end

	-- Unitframes
	local UnitFrameMenu = {
		title = L["UnitFrames"], type = nil, hasWindow = true, 
		buttons = {
			-- Player options
		}
	}

	local UnitFrameParty = Core:GetModule("UnitFrameParty")
	if UnitFrameParty and not (UnitFrameParty:IsIncompatible() or UnitFrameParty:DependencyFailed()) then 
		table_insert(UnitFrameMenu.buttons, {
			enabledTitle = L["Party Frames: %s"]:format(L["Enabled"]),
			disabledTitle = L["Party Frames: %s"]:format(L["Disabled"]),
			type = "TOGGLE_VALUE", 
			configDB = "UnitFrameParty", configKey = "enablePartyFrames", 
			proxyModule = "UnitFrameParty"
		})
	end

	local UnitFrameRaid = Core:GetModule("UnitFrameRaid")
	if UnitFrameRaid and not (UnitFrameRaid:IsIncompatible() or UnitFrameRaid:DependencyFailed()) then 
		table_insert(UnitFrameMenu.buttons, {
			enabledTitle = L["Raid Frames: %s"]:format(L["Enabled"]),
			disabledTitle = L["Raid Frames: %s"]:format(L["Disabled"]),
			type = "TOGGLE_VALUE", 
			configDB = "UnitFrameRaid", configKey = "enableRaidFrames", 
			proxyModule = "UnitFrameRaid"
		})
	end

	local UnitFrameArena = Core:GetModule("UnitFrameArena")
	if UnitFrameArena and not (UnitFrameArena:IsIncompatible() or UnitFrameArena:DependencyFailed()) then 
		table_insert(UnitFrameMenu.buttons, {
			enabledTitle = L["PvP Frames: %s"]:format(L["Enabled"]),
			disabledTitle = L["PvP Frames: %s"]:format(L["Disabled"]),
			type = "TOGGLE_VALUE", 
			configDB = "UnitFrameArena", configKey = "enableArenaFrames", 
			proxyModule = "UnitFrameArena"
		})
	end
	table_insert(MenuTable, UnitFrameMenu)
		

	-- Nameplates
	local NamePlates = Core:GetModule("NamePlates")
	if NamePlates and not (NamePlates:IsIncompatible() or NamePlates:DependencyFailed()) then 
		table_insert(MenuTable, {
			title = L["NamePlates"], type = nil, hasWindow = true, 
			buttons = {
				-- Disable player auras
				{
					enabledTitle = L["Auras: %s"]:format(L["Enabled"]),
					disabledTitle = L["Auras: %s"]:format(L["Disabled"]),
					type = "TOGGLE_VALUE", 
					configDB = "NamePlates", configKey = "enableAuras", 
					proxyModule = "NamePlates"
				}		
			}
		})
	end 

	-- HUD elements
	table_insert(MenuTable,	{
		title = L["HUD"], type = nil, hasWindow = true, 
		buttons = {
			-- Talking Head
			{
				enabledTitle = L["TalkingHead: %s"]:format(L["Enabled"]),
				disabledTitle = L["TalkingHead: %s"]:format(L["Disabled"]),
				type = "TOGGLE_VALUE", 
				configDB = "FloaterHUD", configKey = "enableTalkingHead", 
				proxyModule = "FloaterHUD"
			},

			-- Alerts 
			{
				enabledTitle = L["Alerts: %s"]:format(L["Enabled"]),
				disabledTitle = L["Alerts: %s"]:format(L["Disabled"]),
				type = "TOGGLE_VALUE", 
				configDB = "FloaterHUD", configKey = "enableAlerts", 
				proxyModule = "FloaterHUD"
			}

			
		}
	})
	
	-- Explorer Mode
	local ExplorerMode = Core:GetModule("ExplorerMode")
	if ExplorerMode and not (ExplorerMode:IsIncompatible() or ExplorerMode:DependencyFailed()) then 
		table_insert(MenuTable, {
			title = L["Explorer Mode"], type = nil, hasWindow = true, 
			buttons = {
				-- Enable explorer mode
				{
					enabledTitle = L["Player Fading: %s"]:format(L["Enabled"]),
					disabledTitle = L["Player Fading: %s"]:format(L["Disabled"]),
					type = "TOGGLE_VALUE", 
					configDB = "ExplorerMode", configKey = "enableExplorer", 
					proxyModule = "ExplorerMode"
				},
				{
					enabledTitle = L["Tracker Fading: %s"]:format(L["Enabled"]),
					disabledTitle = L["Tracker Fading: %s"]:format(L["Disabled"]),
					type = "TOGGLE_VALUE", 
					configDB = "ExplorerMode", configKey = "enableTrackerFading", 
					proxyModule = "ExplorerMode"
				}		
			}
		})
	end 
	
end

Module.PreInit = function(self)
	local PREFIX = Core:GetPrefix()
	Layout = CogWheel("LibDB"):GetDatabase(PREFIX..": Layout [Core]")
	L = CogWheel("LibLocale"):GetLocale(PREFIX)
	self:CreateMenuTable()
end

Module.OnInit = function(self)
	self:AddOptionsToMenuWindow()
end 

Module.OnEnable = function(self)
	self:AddOptionsToMenuButton()
	self:RegisterEvent("PLAYER_ENTERING_WORLD", "PostUpdateOptions")
end 
