--[[
Copyright (c) 2006, Hendrik Leppkes < mail@nevcairiel.net >
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are 
met:

    * Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above 
       copyright notice, this list of conditions and the following 
       disclaimer in the documentation and/or other materials provided 
       with the distribution.
    * Neither the name of the Bartender3 Development Team nor the names of 
       its contributors may be used to endorse or promote products derived 
       from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]]
--[[ $Id: Options.lua 29831 2007-03-09 18:56:12Z nevcairiel $ ]]
local dewdrop = AceLibrary("Dewdrop-2.0")

local L = AceLibrary("AceLocale-2.2"):new("Bartender3")

function Bartender3:PopulateOptions()
	self.options = {
		type = "group",
		args = {
			lock = {
				order = 1,
				name = L["Lock"], 
				type = "toggle",
				desc = L["Toggle the lock of the action bars."],
				get = function() return not Bartender3.unlock end,
				set = function(v)
					if Bartender3.unlock then
						Bartender3:LockBars()
					else
						Bartender3:UnlockBars()
					end
				end,
				disabled = function() return InCombatLockdown() end,
			},
			buttonlock = {
				order = 2,
				name = L["Lock Buttons"], 
				type = "toggle",
				desc = L["Toggle the lock of the buttons."],
				get = function() return Bartender3.db.profile.ButtonLock end,
				set = function() Bartender3:LockButtons(true) end,
			},
			head1 = {
				order = 3,
				type = "header",
			},
			tooltip = {
				order = 4,
				name = L["Button Tooltip"],
				type = "toggle",
				desc = L["Toggle the Button Tooltip"],
				get = function() return not Bartender3.db.profile.HideTooltip end,
				set = function(h)
					Bartender3.db.profile.HideTooltip = not h
				end,
			},
			selfcast = {
				order = 5,
				name = L["Self Casting"],
				desc = L["Configute Self Casting options"],
				type = "group",
				args = {
					modifier = {
						order = 1,
						name = L["Modifier SelfCast"],
						desc = L["SelfCast using the Interface SelfCast Modifier"],
						type = "toggle",
						get = function() return Bartender3.db.profile.SelfCastModifier end,
						set = function(scm)
							Bartender3.db.profile.SelfCastModifier = scm
							for i,v in pairs(Bartender3.actionbars) do
								if v then v:UpdateSelfCast() end
							end
						end,
					},
					rightclick = {
						order = 2,
						name = L["RightClick SelfCast"],
						desc = L["SelfCast using Right click"],
						type = "toggle",
						get = function() return Bartender3.db.profile.SelfCastRightClick end,
						set = function(v)
							Bartender3.db.profile.SelfCastRightClick = v
							for i,v in pairs(Bartender3.actionbars) do
								if v then v:UpdateSelfCast() end
							end
						end,
					},
				},
				disabled = function() return InCombatLockdown() end,
			},
			range = {
				order = 7,
				name = L["Out of Range Indicator"],
				desc = L["Toggle the button recoloring based on range"],
				type = "toggle",
				get = function() return Bartender3.db.profile.OutOfRange end,
				set = function(sc) 
					Bartender3.db.profile.OutOfRange = sc
					for k, v in pairs(Bartender3.actionbuttons) do
						if v.object then v.object:UpdateUsable() end
					end
				end,
			},
			head2 = {
				order = 10,
				type = "header",
			},
			head3 = {
				order = 45,
				type = "header",
			},
		},
	}
	
	self:RegisterChatCommand({ "/bar", "/bartender3", "/bt3" }, self.options )
	self.options.args.standby.hidden = true
end

function Bartender3:CreateBarOptions(id, name, special)
	local optionname = ( special ) and (string.lower(id).."bar") or ("bar"..id)
	
	local order = ( special ) and 50 or (30+id)
	-- Options for all bartypes
	self.options.args[optionname] = {
		order = order, 
		name = name,
		type = "group",
		desc = string.format(L["Configuration for %s"], name),
		args = {
			hide = {
				order = 2,
				name = L["Hide"],
				type = "toggle",
				desc = string.format(L["Hide %s"], name),
				get = function() return self:GetConfig(id, special).Hide end,
				set = function() self:GetBarObject(id, special):ToggleVisibilty() end,
				disabled = function() return InCombatLockdown() or not self:GetConfig(id, special).Enabled end,
			},
			rows = {
				order = 4,
				name = L["Rows"],
				type = "range",
				desc = L["Change the rows of the Bar"],
				max = 12, min = 1, step = 1, -- maxbuttons will be adjusted by the bar itself.
				get = function() return self:GetConfig(id, special).Rows end,
				set = function(r) 
					self:GetBarObject(id, special):SetRows(r)
				end,
				disabled = function() return InCombatLockdown() or not self:GetConfig(id, special).Enabled end,
				hidden = function() return not (#self:GetBarObject(id, special).buttons > 1) end,
			},
			padding = {
				order = 5,
				name = L["Padding"],
				type = "range",
				desc = L["Change the padding of the bar."],
				min = -20, max = 30, step = 1,
				get = function() return self:GetConfig(id, special).Padding end,
				set = function(p)
					self:GetBarObject(id, special):SetPadding(p)
				end,
				disabled = function() return InCombatLockdown() or not self:GetConfig(id, special).Enabled end,
			},
			scale = {
				order = 6,
				name = L["Scale"],
				type = "range",
				desc = L["Change the scale of the bar."],
				min = .1, max = 2, step = 0.05,
				isPercent = true,
				get = function() return self:GetConfig(id, special).Scale end,
				set = function(s)
					self:GetBarObject(id, special):SetScale(s)
				end,
				disabled = function() return InCombatLockdown() or not self:GetConfig(id, special).Enabled end,
			},
			alpha = {
				order = 7,
				name = L["Alpha"],
				type = "range",
				desc = L["Change the alpha of the bar."],
				min = .1, max = 1, step = 0.1,
				get = function() return self:GetConfig(id, special).Alpha end,
				set = function(a)
					self:GetBarObject(id, special):SetAlpha(a)
					self:GetBarObject(id, special).faded = nil
				end,
				disabled = function() return not self:GetConfig(id, special).Enabled end,
			},
			style = {
				order = 8,
				name = L["Style"],
				type = "text",
				desc = L["Change the style of the bar."],
				get = function() return self:GetConfig(id, special).Style end,
				set = function(s)
					self:GetBarObject(id, special):SetStyle(s)
				end,
				validate = {},
				disabled = function() return InCombatLockdown() or not self:GetConfig(id, special).Enabled end,
			},
			fadeout = {
				order = 9,
				name = L["FadeOut"],
				type = "toggle",
				desc = L["Fade out the Bar when not hovering it."],
				get = function() return self:GetConfig(id, special).FadeOut end,
				set = function(s)
					self:GetBarObject(id, special):SetFadeOut(s)
				end,
				validate = {},
				disabled = function() return not self:GetConfig(id, special).Enabled end,
			},
		}
	}
	
	local options = self.options.args[optionname]
	
	for k,v in pairs(Bartender3.styles) do
		options.args.style.validate[k] = v.name
	end
	
	-- Options for ActionBars
	if not special then
		options.args.enabled = {
			order = 1,
			name = L["Enabled"],
			type = "toggle",
			desc = string.format(L["Enable %s."], name),
			get = function() return self:GetConfig(id).Enabled end,
			set = function() self:ToggleBarEnabled(id) end,
			disabled = function() return InCombatLockdown() end,
		}
		options.args.macrotext = {
			order = 9,
			name = L["Macro Text"],
			type = "toggle",
			desc = L["Show the button Macro Text"],
			get = function() return not self:GetConfig(id, special).HideMacrotext end,
			set = function(s)
				self:GetConfig(id).HideMacrotext = not s
				local object = self:GetBarObject(id)
				for i,v in pairs(object.buttonobjects) do
					v:UpdateButton()
				end
			end,
			disabled = function() return InCombatLockdown()  or not self:GetConfig(id, special).Enabled end,
		}
		options.args.hotkey = {
			order = 9,
			name = L["Hotkey"],
			type = "toggle",
			desc = L["Show the button Hotkey"],
			get = function() return not self:GetConfig(id, special).HideHotkey end,
			set = function(s)
				self:GetConfig(id).HideHotkey = not s
				local object = self:GetBarObject(id)
				for i,v in pairs(object.buttonobjects) do
					v:UpdateHotkeys()
				end
			end,
			disabled = function() return InCombatLockdown()  or not self:GetConfig(id, special).Enabled end,
		}
		options.args.showgrid = {
			order = 9,
			name = L["Show Grid"],
			type = "toggle",
			desc = L["Show the grid of the bar even while locked."],
			get = function() return self:GetConfig(id).ShowGrid end,
			set = function(s) self:GetBarObject(id):SetShowGrid(s) end,
			disabled = function() return not self:GetConfig(id).Enabled end,
		}
		options.args.paging = {
			order = 10,
			name = L["Button Paging"],
			type = "group",
			desc = L["Configure the State-Based Button Swapping."],
			args = {
				enabled = {
					order = 1,
					name = L["Enabled"],
					type = "toggle",
					desc = L["Toggle the Paging support."],
					get = function() return self:GetConfig(id).StatesEnabled end,
					set = function(v) 
						self:GetConfig(id).StatesEnabled = v 
						self:GetBarObject(id):UpdateStates()
					end,
				},
				modifier = {
					order = 2,
					name = L["Modifier"],
					type = "group",
					desc = L["Swapping based on the modifiers."],
					args = {},
					disabled = function() return not self:GetConfig(id).StatesEnabled end,
				},
				stance = {
					order = 3,
					name = L["Stances"],
					type = "group",
					desc = L["Swapping based on your Stance."],
					args = {},
					hidden = function() return self.stancemap == nil end,
					disabled = function() return not self:GetConfig(id).StatesEnabled end,
				},
			},
			disabled = function() return InCombatLockdown() or not self:GetConfig(id).Enabled end,
		}
		for _,m in pairs(self.statemodifiers) do
			options.args.paging.args.modifier.args[m] = {
				name = L[m],
				type = "range",
				desc = string.format(L["Configure which bar to switch to when %s is down."], L[m]),
				min = 0, max = 10, step = 1,
				get = function() return self:GetConfig(id).States[m] or 0 end,
				set = function(b) self:GetBarObject(id):SetModifierState(m, b) end,
			}
		end
		options.args.buttons = {
			order = 11,
			name = L["Buttons"],
			type = "range",
			desc = L["Configure the number of Buttons."],
			min = 1, max = 12, step = 1,
			get = function() return self:GetConfig(id).Buttons end,
			set = function(a)
				self:GetBarObject(id):SetButtons(a)
			end,
			disabled = function() return InCombatLockdown() or not self:GetConfig(id).Enabled end,
		}
	else
		if id == "BAGS" then
			options.args.keyring = {
				order = 10,
				name = L["Keyring"],
				type = "toggle",
				desc = L["Toggle the Keyring"],
				get = function() return self:GetConfig(id, special).Keyring end,
				set = function()
					self:GetBarObject(id, special):ToggleKeyring()
				end,
				disabled = function() return InCombatLockdown() end,
			}
			options.args.onebag = {
				order = 11,
				name = L["OneBag"],
				type = "toggle",
				desc = L["Toggle the display of only one Bag Icon"],
				get = function() return self:GetConfig(id, special).OneBag end,
				set = function()
					self:GetBarObject(id, special):ToggleOneBag()
				end,
				disabled = function() return InCombatLockdown() end,
			}
		elseif id == "MICROMENU" then
			options.args.style = nil
		elseif id == "XP" then
			options.args.style = nil
			options.args.rows = nil
			options.args.padding = nil
		end
	end
	
	return options
end

function Bartender3:CreateDisabledBarOptions(id)
	local name = string.format(L["Bar %s"], id)
	local optionname = "bar"..id
	
	self.options.args[optionname] = {
		order = 30+id,
		name = name,
		type = "group",
		desc = string.format(L["Configuration for %s"], name),
		args = {
			enabled = {
				order = 1,
				name = L["Enabled"],
				type = "toggle",
				desc = string.format(L["Enable %s."], name),
				get = function() return self:GetConfig(id).Enabled end,
				set = function() self:ToggleBarEnabled(id) end,
				disabled = function() return InCombatLockdown() end,
			},
		},
	}
end
