local L = LibStub("AceLocale-3.0"):GetLocale("Bartender4")

local Bar = Bartender4.Bar.prototype
local ButtonBar = Bartender4.ButtonBar.prototype

--[[===================================================================================
	Bar Options
===================================================================================]]--

-- option utilty functions
local optGetter, optSetter
do
	local getBar, optionMap, callFunc
	local barregistry = Bartender4.Bar.barregistry
	-- maps option keys to function names
	optionMap = {
		rows = "Rows",
		padding = "Padding",
		zoom = "Zoom",
	}
	
	-- retrieves a valid bar object from the barregistry table
	function getBar(id)
		local bar = barregistry[tostring(id)]
		assert(bar, ("Invalid bar id in options table. (%s)"):format(id))
		return bar
	end
	
	-- calls a function on the bar
	function callFunc(bar, type, option, ...)
		local func = type .. (optionMap[option] or option)
		assert(bar[func], ("Invalid get/set function %s in bar %s."):format(func, bar.id))
		return bar[func](bar, ...)
	end
	
	-- universal function to get a option
	function optGetter(info)
		local bar = getBar(info[2])
		local option = info[#info]
		return callFunc(bar, "Get", option)
	end
	
	-- universal function to set a option
	function optSetter(info, ...)
		local bar = getBar(info[2])
		local option = info[#info]
		return callFunc(bar, "Set", option, ...)
	end
end

function ButtonBar:GetOptionObject()
	local obj = Bar.GetOptionObject()
	local otbl_general = {
		padding = {
			order = 40,
			type = "range",
			name = L["Padding"],
			desc = L["Configure the padding of the buttons."],
			min = -10, max = 20, step = 1,
			set = optSetter,
			get = optGetter,
		},
		zoom = {
			order = 59,
			name = L["Zoom"],
			type = "toggle",
			desc = L["Toggle Button Zoom\nFor more style options you need to install ButtonFacade"],
			get = optGetter,
			set = optSetter,
			hidden = function() return LibStub("LibButtonFacade", true) and true or false end,
		},
		rows = {
			order = 70,
			name = L["Rows"],
			desc = L["Number of rows."],
			type = "range",
			min = 1, max = 12, step = 1,
			set = optSetter,
			get = optGetter,
		},
	}
	obj:AddElementGroup("general", otbl_general)
	return obj
end
