view Modules/Config.lua @ 214:a7c02f65b072

Removed the outdated manual from the config. Fixed general help to prepare for a release.
author Zerotorescue
date Sun, 06 Feb 2011 13:43:06 +0100
parents 1959e2b3dfe1
children 70daa765e275
line wrap: on
line source
local addon = select(2, ...);
local mod = addon:NewModule("Config");

local options, groupIdToName, groupIsVirtual, temp, count, includeTradeSkillItems, currentGroupType = {}, {}, {}, {}, 0, 500, "Normal";
local AceConfigDialog, AceConfigRegistry, AceSerializer;

local unknownItemName = "Unknown (#%d)";

-- Private functions and tables

local function SetOption(info, value, multiSelectEnabled)
	local groupName = groupIdToName[info[2]];
	local optionName = info[#info];
	
	-- Special treatment for override toggle boxes
	if optionName:find("override") then
		if not value and info.arg then
			-- If this override was disabled and a saved variable name was provided, set it to nil rather than false
			
			value = nil;
			
			-- If this is an override toggler then also set the related field to nil
			addon.db.profile.groups[groupName][info.arg] = nil;
		elseif value and info.arg then
			-- If this override is now enabled, we need to copy the default into this field (unless it is not nil (which is supposed to be impossible), in which case we'll use the already selected value)
			
			local inheritedValue = addon:GetOptionByKey(groupName, info.arg);
			
			addon.db.profile.groups[groupName][info.arg] = (type(inheritedValue) == "table" and CopyTable(inheritedValue)) or inheritedValue; -- copying defaults by reference would let one (unintendedly) change the defaults
		end
	end
	
	if multiSelectEnabled ~= nil then
		-- The saved vars for a multiselect will always be an array, it may not yet exist in which case it must be created.
		if not addon.db.profile.groups[groupName][optionName] then
			addon.db.profile.groups[groupName][optionName] = {};
		end
		
		addon.db.profile.groups[groupName][optionName][value] = multiSelectEnabled or nil;
	else
		addon.db.profile.groups[groupName][optionName] = value;
	end
end

local function GetOption(info)
	local groupName = groupIdToName[info[2]];
	local optionName = info[#info];
	
	local noDefault;
	
	if optionName:find("override") then
		noDefault = true;
	end
	
	return addon:GetOptionByKey(groupName, optionName, noDefault);
end

local function GetMultiOption(info, value)
	local groupName = groupIdToName[info[2]];
	local optionName = info[#info];
	
	local multiSelectValue = addon:GetOptionByKey(groupName, optionName);
	
	return multiSelectValue and multiSelectValue[value];
end

local function GetDisabled(info)
	local groupName = groupIdToName[info[2]];
	local optionName = info[#info];
	
	if optionName:find("override") or not info.arg then
		return false;
	end
	
	return (addon:GetOptionByKey(groupName, info.arg, true) == nil);
end

local function ValidateGroupName(_, value)
	value = string.lower(string.trim(value or ""));
	
	for name, _ in pairs(addon.db.profile.groups) do
		if string.lower(name) == value then
			return ("A group named \"%s\" already exists."):format(name);
		end
	end
	
	return true;
end

local tblAddItemTemplate = {
	order = 0,
	type = "input",
	name = function(info)
		local itemName, _, itemRarity = GetItemInfo(info[#info]);
		return tostring( 7 - (itemRarity or 0) ) .. (itemName or "");
	end,
	get = function(info)
		return tostring(info[#info]); -- Ace is going to be bitching about this if it's a numeric value, so we transmute it into a string here then back to a number on the other side
	end,
	set = function(groupId, itemData)
    	-- This is NOT a real "set", we pass the widget reference to this function which contains similar, but not the same, info.
    	
    	if itemData then
			local groupName = groupIdToName[groupId];
			
			if not itemData:AddToGroup(groupName) then
				addon:Print("Couldn't add the item with itemId (" .. itemData.id .. ") because it is already in a group.", addon.Colors.Red);
			end
			
			if AceConfigRegistry then
				-- Now rebuild the list
				AceConfigRegistry:NotifyChange("Inventorium");
			end
    	end
	end,
	width = "double",
	dialogControl = "ConfigItemLinkButton",
};

local tblRemoveItemTemplate = {
	order = 0,
	type = "input",
	name = function(info)
		local itemName, _, itemRarity = GetItemInfo(info[#info]);
		return tostring( 7 - (itemRarity or 0) ) .. (itemName or "");
	end,
	get = function(info)
		return tostring(info[#info]); -- Ace is going to be anal about this if it's a numeric value, so we transmute it into a string here then back to a number on the other side
	end,
	set = function(groupId, itemData)
    	-- This is NOT a real "set", we pass the widget reference to this function which contains similar, but not the same, info.
    	
    	if itemData then
	    	local groupName = groupIdToName[groupId];
	    	
	    	itemData:RemoveFromGroup(groupName);
			
			if AceConfigRegistry then
				-- Now rebuild the list
				AceConfigRegistry:NotifyChange("Inventorium");
			end
    	end
	end,
	width = "double",
	dialogControl = "ConfigItemLinkButton",
};

local function UpdateAddItemList(info)
	local groupName = groupIdToName[info[2]];
	
	if not addon.db.profile.groups[groupName].items then
		addon.db.profile.groups[groupName].items = {};
	end
	
	-- Merge all items from all groups together (we only use this to check if an item is already in a group)
	local items = {};
	for groupName, values in pairs(addon.db.profile.groups) do
		if values.items then
			for itemId, count in pairs(values.items) do
				items[itemId] = tonumber(count) or 0;
			end
		end
	end
	
	local ref = options.args.groups.args[info[2]].args.add.args.list.args;
	
	-- Remaking the list, so out with the old, in with the new
	table.wipe(ref);
	
	-- Parse bags and show these
	for bagID = 4, 0, -1 do
		for slot = 1, GetContainerNumSlots(bagID) do
			local itemId = addon:GetItemId(GetContainerItemLink(bagID, slot));
			
			if itemId then
				if not items[itemId] then
					-- If this item isn't used in any group yet
					ref[itemId] = tblAddItemTemplate;
				else
					-- It's already used in a group, don't show it
					ref[itemId] = nil;
				end
			end
		end
	end
	
	if includeTradeSkillItems ~= 500 then
		-- Include tradeskill items
		
		-- Go through all trade skills for the profession
		for i = 1, GetNumTradeSkills() do
			-- Try to retrieve the itemlink, this will be nil if current item is a group instead
			local itemLink = GetTradeSkillItemLink(i);
			
			if itemLink then
				local itemId = addon:GetItemId(itemLink);
				if not itemId then
					-- If this isn't an item, it can only be an enchant instead
					itemId = tonumber(itemLink:match("|Henchant:([-0-9]+)|h"));
					
					itemId = addon.scrollIds[itemId]; -- change enchantIds into scrollIds
				end
				
				if itemId then
					local itemLevel = select(4, GetItemInfo(itemId)) or 0;
					
					if includeTradeSkillItems == 0 or itemLevel >= includeTradeSkillItems then
						if not items[itemId] then
							-- If this item isn't used in any group yet
							ref[itemId] = tblAddItemTemplate;
						else
							-- It's already used in a group, don't show it
							ref[itemId] = nil;
						end
					end
				else
					addon:Debug("|cffff0000ERROR|r: Couldn't find proper item id for %s", itemLink);
				end
			end
		end
	end
end

local function UpdateRemoveItemList(info)
	local groupName = groupIdToName[info[2]];
	
	if not addon.db.profile.groups[groupName].items then
		addon.db.profile.groups[groupName].items = {};
	end
	
	local ref = options.args.groups.args[info[2]].args.remove.args.list.args;
	
	-- Unset all
	for itemId, _ in pairs(ref) do
		ref[itemId] = nil;
	end
	
	-- Parse items in group and show these
	for itemId, _ in pairs(addon.db.profile.groups[groupName].items) do
		ref[itemId] = tblRemoveItemTemplate;
	end
end

local function ImportPremadeItemsGroup(groupName, name)
	local premadeItemGroup = addon.defaultGroups[name];
	
	addon:Print(("Importing items from the premade group \"|cfffed000%s|r\"."):format(name));

	-- Remember we imported this group and it's version so if it is ever changed, people can be notified
	if not addon.db.profile.groups[groupName].premadeGroups then
		addon.db.profile.groups[groupName].premadeGroups = {};
	end
	addon.db.profile.groups[groupName].premadeGroups[name] = premadeItemGroup.version;

	for itemId, version in pairs(premadeItemGroup.items) do
		if version > 0 then
			-- Sanity check
			itemId = itemId and tonumber(itemId);
			
			local itemData = addon.ItemData:New(itemId);
			
			if not itemId then
				addon:Print(("\"|cfffed000%s|r\" is not a number."):format(name), addon.Colors.Red);
			elseif itemData:InGroup() then
				addon:Print(("Skipping |cfffed000%s|r as it is already in the group |cfffed000%s|r."):format( (itemData.link or unknownItemName:format(itemId)), itemData:InGroup() ), addon.Colors.Red);
			elseif itemData:AddToGroup(groupName) then
				addon:Print(("Added %s to the group |cfffed000%s|r."):format( (itemData.link or unknownItemName:format(itemId)), groupName ), addon.Colors.Green);
			end
		end
	end
end

-- Default group
local defaultGroup = {
	order = 0,
	type = "group",
	childGroups = "tab",
	name = function(info)
		local groupId = info[#info];
		if groupIsVirtual[groupId] then
			return ("%s  |cfffed000Virtual|r"):format(groupIdToName[groupId]);
		else
			return groupIdToName[groupId];
		end
	end,
	desc = function(info)
		local groupId = info[#info];
		if groupIsVirtual[groupId] then
			return "This is a virtual group, you can use it to override the defaults for other groups.";
		end
	end,
	args = {
		general = {
			order = 10,
			type = "group",
			name = "General",
			desc = "Change the general settings for just this group.",
			args = {
				general = {
					order = 0,
					type = "group",
					inline = true,
					name = "General",
					set = SetOption,
					get = GetOption,
					disabled = GetDisabled,
					args = {
						description = {
							order = 0,
							type = "description",
							name = function(info)
								local groupName = groupIdToName[info[2]];
								
								local t = "";
								if not addon.db.profile.defaults.hideHelp then
									t = "Here you can set general settings for the currently selected group. If you do not wish to override a setting, the default setting specified in the general group will be used.";
								end
								
								return t;
							end,
						},
						header = {
							order = 5,
							type = "header",
							name = "",
							hidden = function() return addon.db.profile.defaults.hideHelp; end,
						},
						overrideTrackAtCharacters = {
							order = 19,
							type = "toggle",
							name = "Override track at",
							desc = "Allows you to override at which characters groups should be tracked, appear in the summary or generate alerts.",
							arg = "trackAtCharacters",
						},
						trackAtCharacters = {
							order = 20,
							type = "multiselect",
							name = "Track at",
							desc = "Select at which characters groups should be tracked, appear in the summary or generate alerts.",
							values = function()
								local temp = {};
								for charName in pairs(addon.db.factionrealm.characters) do
									temp[charName] = charName;
								end
								
								return temp;
							end,
							get = GetMultiOption,
							dialogControl = "Dropdown", -- this is not standard, normal multiselect control gives us a list of all chars with toggle-boxes. UGLY! We want a multiselect-box instead.
							arg = "overrideTrackAtCharacters",
						},
						overrideDontAlertAtCharacters = {
							order = 29,
							type = "toggle",
							name = "Override not alerting at",
							desc = "Allows you to override at which characters items in groups should |cffff0000not|r generate alerts when they are below the required stock.",
							arg = "dontAlertAtCharacters",
						},
						dontAlertAtCharacters = {
							order = 30,
							type = "multiselect",
							name = "Do |cffff0000not|r alert at",
							desc = "Select at which characters items in groups should |cffff0000not|r generate alerts when they are below the required stock.",
							values = function()
								local temp = {};
								for charName in pairs(addon.db.factionrealm.characters) do
									temp[charName] = charName;
								end
								
								return temp;
							end,
							get = GetMultiOption,
							dialogControl = "Dropdown", -- this is not standard, normal multiselect control gives us a list of all chars with toggle-boxes. UGLY! We want a multiselect-box instead.
							arg = "overrideDontAlertAtCharacters",
						},
						overrideLocalItemData = {
							order = 39,
							type = "toggle",
							name = "Override local item data",
							desc = "Allows you to override the local item data setting for this group.",
							arg = "localItemData",
						},
						localItemData = {
							order = 40,
							type = "multiselect",
							name = "Include in local item data",
							desc = "Select which data should be included in the local item data.",
							values = {
								["Bag"] = "Bag",
								["Bank"] = "Bank",
								["Auction House"] = "Auction House",
								["Mailbox"] = "Mailbox",
							},
							get = GetMultiOption,
							dialogControl = "Dropdown", -- this is not standard, normal multiselect control gives us a list of all chars with toggle-boxes. UGLY! We want a multiselect-box instead.
							arg = "overrideLocalItemData",
						},
						virtualGroup = {
							order = 50,
							type = "select",
							name = "Use virtual group settings", 
							desc = "Use the settings from a virtual group before using the general defaults.\n\n|cffff9933This is an advanced option, you will probably not need it unless you manage a lot of groups.|r\n\n|cfffed000Off|r: Use the overridden options in this group and then the defaults.\n\n|cfffed000On|r: Use the overridden options in this group, then the ones in the selected virtual group and then the defaults.",
							values = function(info)
								local groupName = groupIdToName[info[2]];
								
								local temp = {};
								
								temp[""] = "";
								for name, values in pairs(addon.db.profile.groups) do
									if values.isVirtual and name ~= groupName then
										temp[name] = name;
									end
								end
								
								return temp;
							end,
							set = function(info, value)
								local groupName = groupIdToName[info[2]];
								local optionName = info[#info];
								
								addon.db.profile.groups[groupName][optionName] = value ~= "" and value;
							end,
							disabled = false,
						},
					},
				},
				minimumStock = {
					order = 10,
					type = "group",
					inline = true,
					name = "Minimum stock",
					set = SetOption,
					get = GetOption,
					disabled = GetDisabled,
					args = {
						description = {
							order = 0,
							type = "description",
							name = "Here you can specify the minimum amount of items you wish to keep in stock and related settings for the currently selected group. Please note the values entered here do not affect the queued quantities, you must set settings for that in the area below.",
							hidden = function() return addon.db.profile.defaults.hideHelp; end,
						},
						header = {
							order = 5,
							type = "header",
							name = "",
							hidden = function() return addon.db.profile.defaults.hideHelp; end,
						},
						
						overrideMinLocalStock = {
							order = 10,
							type = "toggle",
							name = "Override min local stock",
							desc = "Allows you to override the minimum local stock setting for this group.",
							arg = "minLocalStock",
						},
						minLocalStock = {
							order = 11,
							type = "range",
							min = 0,
							max = 100000,
							softMax = 100,
							step = 1,
							name = "Minimum local stock",
							desc = "You can manually enter a value between 100 and 100.000 in the text box below if the provided range is insufficient.",
							arg = "overrideMinLocalStock",
						},
						overrideAlertBelowLocalMinimum = {
							order = 15,
							type = "toggle",
							name = "Override local minimum alert",
							desc = "Allows you to override wether an alert should be shown when an item in this group gets below the local minimum stock threshold.",
							arg = "alertBelowLocalMinimum",
						},
						alertBelowLocalMinimum = {
							order = 16,
							type = "toggle",
							name = "Alert when below local minimum",
							desc = "Show an alert when an item in this group gets below the local minimum stock threshold.",
							arg = "overrideAlertBelowLocalMinimum",
						},
						overrideAutoRefill = {
							order = 17,
							type = "toggle",
							name = "Override auto refill",
							desc = "Allows you to override wether you want to automatically refill items when below the minimum local stock.",
							arg = "autoRefill",
						},
						autoRefill = {
							order = 18,
							type = "toggle",
							name = "Auto refill from storage",
							desc = "Automatically refill items from your storage (bank/mailbox - unless this is included in the local count - or the guild bank) when below the minimum local stock.",
							arg = "overrideAutoRefill",
						},
						spacer = {
							order = 19,
							type = "description",
							name = "",
						},
						
						overrideMinGlobalStock = {
							order = 20,
							type = "toggle",
							name = "Override min global stock",
							desc = "Allows you to override the minimum global stock setting for this group.",
							arg = "minGlobalStock",
						},
						minGlobalStock = {
							order = 21,
							type = "range",
							min = 0,
							max = 100000,
							softMax = 100,
							step = 1,
							name = "Minimum global stock",
							desc = "You can manually enter a value between 100 and 100.000 in the text box below if the provided range is insufficient.",
							arg = "overrideMinGlobalStock",
						},
						overrideAlertBelowGlobalMinimum = {
							order = 25,
							type = "toggle",
							name = "Override global minimum alert",
							desc = "Allows you to override wether an alert should be shown when an item in this group gets below the global minimum stock threshold.",
							arg = "alertBelowGlobalMinimum",
						},
						alertBelowGlobalMinimum = {
							order = 26,
							type = "toggle",
							name = "Alert when below global minimum",
							desc = "Show an alert when an item in this group gets below the global minimum stock threshold.",
							arg = "overrideAlertBelowGlobalMinimum",
						},
						
						overrideSummaryThresholdShow = {
							order = 34,
							type = "toggle",
							name = "Override summary showing",
							desc = "Allows you to override when this group should appear in the summary.",
							arg = "summaryThresholdShow",
						},
						summaryThresholdShow = {
							order = 35,
							type = "range",
							min = 0,
							max = 10,
							softMax = 100,
							step = 0.05,
							isPercent = true,
							name = "Show in summary when below",
							desc = "Show items in the summary when below the specified percentage of the minimum stock.\n\nYou can manually enter a value between 1.000% and 10.000% in the edit box if the provided range is insufficient.",
							arg = "overrideSummaryThresholdShow",
						},
					},
				},
				refill = {
					order = 20,
					type = "group",
					inline = true,
					name = "Replenishing stock",
					set = SetOption,
					get = GetOption,
					disabled = GetDisabled,
					args = {
						description = {
							order = 0,
							type = "description",
							name = function(info)
								local groupName = groupIdToName[info[2]];
								local r = "Here you can specify the amount of items to which you wish to restock when you are collecting new items for the currently selected group. This may be higher than the minimum stock.\n\n";
								
								r = r .. "When restocking the target amount is |cfffed000" .. addon:GetOptionByKey(groupName, "restockTarget") .. "|r of every item. Not queueing craftable items when only missing |cfffed000" .. floor( addon:GetOptionByKey(groupName, "minCraftingQueue") * addon:GetOptionByKey(groupName, "restockTarget") ) .. "|r (|cfffed000" .. ( addon:GetOptionByKey(groupName, "minCraftingQueue") * 100 ) .. "%|r) of the restock target and making |cfffed000" .. floor( ( addon:GetOptionByKey(groupName, "bonusQueue") * addon:GetOptionByKey(groupName, "restockTarget") ) + .5 ) .. "|r (|cfffed000" .. ( addon:GetOptionByKey(groupName, "bonusQueue") * 100 ) .. "%|r) extra items when you completely ran out. ";
								
								if addon:GetOptionByKey(groupName, "priceThreshold") == 0 then
									r = r .. "Queueing items at |cfffed000any|r auction value.";
								else
									r = r .. "Queueing items worth |cfffed000" .. addon:ReadableMoney(addon:GetOptionByKey(groupName, "priceThreshold")) .. "|r or more.";
								end
								
								return r;
							end,
							hidden = function() return addon.db.profile.defaults.hideHelp; end,
						},
						header = {
							order = 5,
							type = "header",
							name = "",
							hidden = function() return addon.db.profile.defaults.hideHelp; end,
						},
						overrideRestockTarget = {
							order = 9,
							type = "toggle",
							name = "Override restock target",
							desc = "Allows you to override the restock target setting for this group.",
							arg = "restockTarget",
						},
						restockTarget = {
							order = 10,
							type = "range",
							min = 0,
							max = 100000,
							softMax = 100,
							step = 1,
							name = "Restock target",
							desc = "You can manually enter a value between 100 and 100.000 in the edit box if the provided range is insufficient.",
							arg = "overrideRestockTarget",
						},
						overrideMinCraftingQueue = {
							order = 19,
							type = "toggle",
							name = "Override min queue",
							desc = "Allows you to override the minimum craftable items queue setting for this group.",
							arg = "minCraftingQueue",
						},
						minCraftingQueue = {
							order = 20,
							type = "range",
							min = 0,
							max = 1,
							step = 0.01,
							isPercent = true,
							name = "Don't queue when only missing...",
							desc = "Don't add a craftable item to the queue when only missing this much or less of the restock target.\n\nExample: if your restock target is set to 60 and this is set to 5%, an item won't be queued unless you are missing more than 3 of it.",
							arg = "overrideMinCraftingQueue",
						},
						overrideBonusQueue = {
							order = 29,
							type = "toggle",
							name = "Override bonus queue",
							desc = "Allows you to override the bonus craftable items queue setting for this group.",
							arg = "bonusQueue",
						},
						bonusQueue = {
							order = 30,
							type = "range",
							min = 0,
							max = 10, -- 1000%
							step = 0.01, -- 1%
							isPercent = true,
							name = "Bonus queue",
							desc = "Get additional items when there are none left.\n\nExample: if your restock target is set to 60 and this is set to 10%, you will get 66 items instead of just 60 if you end up with none left while queueing.",
							arg = "overrideBonusQueue",
						},
						overridePriceThreshold = {
							order = 39,
							type = "toggle",
							name = "Override price threshold",
							desc = "Allows you to override the price threshold setting for this group.",
							arg = "priceThreshold",
						},
						priceThreshold = {
							order = 40,
							type = "input",
							name = "Price threshold",
							desc = "Only queue craftable items when they are worth at least this much according to your auction house addon.\n\nSet to 0 to ignore auction prices.",
							validate = function(info, value) return addon:ValidateReadableMoney(info, value); end,
							get = function(i) return addon:ReadableMoney(GetOption(i)); end,
							set = function(i, v) SetOption(i, addon:ReadableMoneyToCopper(v)); end,
							arg = "overridePriceThreshold",
						},
						overrideSummaryHidePriceThreshold = {
							order = 49,
							type = "toggle",
							name = "Override summary showing",
							desc = "Allows you to override if items in this group should be hidden from the summary while their value is below the price threshold.",
							arg = "summaryHidePriceThreshold",
						},
						summaryHidePriceThreshold = {
							order = 50,
							type = "toggle",
							name = "Hide when below threshold",
							desc = "Hide items from the summary when their value is below the set price threshold.",
							arg = "overrideSummaryHidePriceThreshold",
						},
						overrideAlwaysGetAuctionValue = {
							order = 59,
							type = "toggle",
							name = "Override auction value showing",
							desc = "Allows you to override if the auction value of items in this group should be cached and displayed even when the price threshold is set to 0|cffeda55fc|r.",
							arg = "alwaysGetAuctionValue",
						},
						alwaysGetAuctionValue = {
							order = 60,
							type = "toggle",
							name = "Always show auction value",
							desc = "Always cache and show the auction value of items in this group, even if the price threshold is set to 0|cffeda55fc|r.",
							arg = "overrideAlwaysGetAuctionValue",
						},
					},
				},
				addons = {
					order = 30,
					type = "group",
					inline = true,
					name = "Prefered addons",
					set = SetOption,
					get = GetOption,
					disabled = GetDisabled,
					args = {
						description = {
							order = 0,
							type = "description",
							name = function(info)
								local groupName = groupIdToName[info[2]];
								
								local t = "";
								if not addon.db.profile.defaults.hideHelp then
									t = "Selecting your prefered addons is optional, the first working addon will be used if the selected addon doesn't exist. You only need to set this when you are running multiple addons of a kind and wish to specify which addons to use for what groups.\n\n";
								end
								
								local currentAddon, selectedAddonName = addon:GetItemCountAddon(groupName);
								local preferedAddon = addon:GetOptionByKey(groupName, "itemCountAddon");
								
								if currentAddon then
									--GetCharacterCount
									--addon.supportedAddons.itemCount[selectedExternalAddon]
									t = t .. "Currently using |cfffed000" .. selectedAddonName .. "|r as your item count addon. This addon is " .. ((currentAddon.IsEnabled() and "|cff00ff00enabled|r") or "|cffff0000disabled|r") .. ".";
									
									if currentAddon.GetTotalCount and currentAddon.GetCharacterCount then
										t = t .. " This addon supports |cfffed000both total as local|r item counts.";
									elseif currentAddon.GetTotalCount then
										t = t .. " This addon supports |cfffed000only total|r item counts.";
									elseif currentAddon.GetCharacterCount then
										t = t .. " This addon supports |cfffed000only local|r item counts.";
									end
									
									if preferedAddon ~= selectedAddonName then
										t = t .. "\n\n|cffff0000You have selected |cfffed000" .. preferedAddon .. "|r|cffff0000 as your item count addon, but this appears to be disabled and thus a random alternative was selected.|r";
									end
								end
								
								return t;
							end,
						},
						header = {
							order = 5,
							type = "header",
							name = "",
							hidden = function() return addon.db.profile.defaults.hideHelp; end,
						},
						overrideAuctionPricingAddon = {
							order = 9,
							type = "toggle",
							name = "Override pricing addon",
							desc = "Allows you to override the pricing addon setting for this group.",
							arg = "auctionPricingAddon",
						},
						auctionPricingAddon = {
							order = 10,
							type = "select",
							name = "Prefered pricing addon",
							desc = "Select the addon you prefer data for this group to be retrieved from. A random supported addon will be used if the selected addon can not be found.",
							values = function()
								local temp = {};
								for name, value in pairs(addon.supportedAddons.auctionPricing) do
									temp[name] = name;
								end
								
								return temp;
							end,
							set = function(info, value)
								local groupName = groupIdToName[info[2]];
								local optionName = info[#info];
								
								addon.db.profile.groups[groupName][optionName] = value ~= "" and value;
								
								if addon.supportedAddons.auctionPricing[value].OnSelect then
									addon.supportedAddons.auctionPricing[value].OnSelect();
								end
							end,
							arg = "overrideAuctionPricingAddon",
						},
						overrideItemCountAddon = {
							order = 19,
							type = "toggle",
							name = "Override item count addon",
							desc = "Allows you to override the item count addon setting for this group.",
							arg = "itemCountAddon",
						},
						itemCountAddon = {
							order = 20,
							type = "select",
							name = "Prefered item count addon",
							desc = "Select the addon you prefer data for this group to be retrieved from. A random supported addon will be used if the selected addon can not be found.",
							values = function()
								local temp = {};
								for name, value in pairs(addon.supportedAddons.itemCount) do
									temp[name] = name;
								end
								
								return temp;
							end,
							set = function(info, value)
								local groupName = groupIdToName[info[2]];
								local optionName = info[#info];
								
								addon.db.profile.groups[groupName][optionName] = value ~= "" and value;
								
								if addon.supportedAddons.itemCount[value].OnSelect then
									addon.supportedAddons.itemCount[value].OnSelect();
								end
							end,
							arg = "overrideItemCountAddon",
						},
						overrideCraftingAddon = {
							order = 29,
							type = "toggle",
							name = "Override crafting addon",
							desc = "Allows you to override the crafting addon setting for this group.",
							arg = "craftingAddon",
						},
						craftingAddon = {
							order = 30,
							type = "select",
							name = "Prefered crafting addon",
							desc = "Select the addon you prefer data from this group to be queued into. A random supported addon will be used if the selected addon can not be found.",
							values = function()
								local temp = {};
								for name, value in pairs(addon.supportedAddons.crafting) do
									temp[name] = name;
								end
								
								return temp;
							end,
							set = function(info, value)
								local groupName = groupIdToName[info[2]];
								local optionName = info[#info];
								
								addon.db.profile.groups[groupName][optionName] = value ~= "" and value;
								
								if addon.supportedAddons.crafting[value].OnSelect then
									addon.supportedAddons.crafting[value].OnSelect();
								end
							end,
							arg = "overrideCraftingAddon",
						},
					},
				},
			},
		},
		group = {
			order = 20,
			type = "group",
			name = "Management",
			desc = "Rename, delete, duplicate or export this group.",
			args = {
				actions = {
					order = 10,
					type = "group",
					name = "Actions",
					inline = true,
					args = {
						rename = {
							order = 10,
							type = "input",
							name = "Rename group - New name",
							desc = "Change the name of this group to something else. You can also use item links here as you wish.",
							validate = ValidateGroupName,
							set = function(info, value)
								local oldGroupName = groupIdToName[info[2]];
								
								addon.db.profile.groups[value] = CopyTable(addon.db.profile.groups[oldGroupName]);
								addon.db.profile.groups[oldGroupName] = nil;
								
								groupIdToName[info[2]] = value;
								groupIdToName[value] = true;
								groupIdToName[oldGroupName] = nil;
								
								mod:FillGroupOptions();
							end,
							get = function(info)
								return groupIdToName[info[2]];
							end,
						},
						duplicate = {
							order = 20,
							type = "input",
							name = "Duplicate group - New name",
							desc = "Duplicate this group. You can also use item links here as you wish.\n\nAll item data will be erased.",
							validate = ValidateGroupName,
							set = function(info, value)
								local oldGroupName = groupIdToName[info[2]];
								
								addon.db.profile.groups[value] = CopyTable(addon.db.profile.groups[oldGroupName]);
								
								-- Reset item data (duplicate items me no want)
								addon.db.profile.groups[value].items = nil;
								
								mod:FillGroupOptions();
							end,
							get = false,
						},
						delete = {
							order = 30,
							type = "execute",
							name = "Delete group",
							desc = "Delete the currently selected group.",
							confirm = true,
							confirmText = "Are you sure you wish to |cffff0000DELETE|r this group? This action is not reversable!",
							func = function(info)
								local groupName = groupIdToName[info[2]];
								
								addon.db.profile.groups[groupName] = nil;
								
								mod:FillGroupOptions();
							end,
						},
					},
				},
				export = {
					order = 40,
					type = "group",
					name = "Export",
					inline = true,
					args = {
						input = {
							order = 10,
							type = "input",
							multiline = true,
							name = "Group data",
							width = "full",
							desc = "Export the group data for the currently selected group. Press CTRL-A to select all and CTRL-C to copy the text.",
							set = false,
							get = function(info)
								local groupName = groupIdToName[info[2]];
								
								return mod:ExportGroup(groupName);
							end,
						},
					},
				},
			},
		},
		add = {
			order = 30,
			type = "group",
			name = "Add items",
			desc = "Add new items to this group.",
			hidden = function(info) return groupIsVirtual[info[2]]; end,
			args = {
				singleAdd = {
					order = 10,
					type = "group",
					inline = true,
					name = "Add items",
					args = {
						help = {
							order = 10,
							type = "description",
							name = "You can add a single item to this group at a time by pasting the item-id or an item-link in the field to the left or you can also import multiple items at once by pasting exported item data in the field to the right. Scroll further down to add items based on your inventory contents.",
							hidden = function() return addon.db.profile.defaults.hideHelp; end,
						},
						itemLink = {
							order = 20,
							type = "input",
							name = "Single item add (item-link or item-id)",
							desc = "Shift-click an item-link or enter an item-id to add the related item to this group. You can only add one item link or item id at a time.",
							validate = function(info, value)
								-- If the value is empty we'll allow passing to clear the carret
								if value == "" then return true; end
								
								local groupName = groupIdToName[info[2]];
								
								local itemId = (addon:GetItemId(string.trim(value)) or tonumber(string.trim(value)));
								
								local itemData = addon.ItemData:New(itemId);
								
								if not itemId then
									return "This is not a valid item link or id.";
								elseif itemData:InGroup() then
									return ("This item is already in the group |cfffed000%s|r."):format(itemData:InGroup());
								end
								
								return true;
							end,
							set = function(info, value)
								if value and value ~= "" then
									local groupName = groupIdToName[info[2]];
									
									local itemId = (addon:GetItemId(string.trim(value)) or tonumber(string.trim(value)));
									
									local itemData = addon.ItemData:New(itemId);
									
									if itemData:AddToGroup(groupName) then
										addon:Print(("Added %s to the selected group."):format( (itemData.link or unknownItemName:format(itemId)) ), addon.Colors.Green);
										
										if AceConfigRegistry then
											-- Now rebuild the list
											AceConfigRegistry:NotifyChange("Inventorium");
										end
									else
										addon:Print(("%s is already in the group |cfffed000%s|r."):format( (itemData.link or unknownItemName:format(itemId)), itemData:InGroup() ), addon.Colors.Red);
									end
								end
							end,
							get = false,
						},
						importItemData = {
							order = 30,
							type = "input",
							name = "Import item data",
							desc = "Import item data from an exported item data-string. Any items already grouped will be skipped.",
							set = function(info, value)
								local groupName = groupIdToName[info[2]];
								
								local allItemIds = { string.split(";", value or "") };
								
								for _, value in pairs(allItemIds) do
									local itemId = tonumber(value);
									
									if itemId then
										local itemData = addon.ItemData:New(itemId);
										
										if itemData:InGroup() then
											addon:Print(("Skipping %s (#%d) as it is already in the group |cfffed000%s|r."):format(itemData.link or "Unknown", itemId, itemData:InGroup()), addon.Colors.Red);
											addon:Print(("Skipping %s (#%d) as it is already in the group |cfffed000%s|r."):format(itemData.link or "Unknown", itemId, itemData:InGroup()), addon.Colors.Red);
										elseif itemData:AddToGroup(groupName) then
											addon:Print(("Added %s to the group |cfffed000%s|r."):format(itemData.link or unknownItemName:format(itemId), groupName), addon.Colors.Green);
										end
									else
										addon:Print(("\"%s\" is not a number."):format(value), addon.Colors.Red);
									end
								end
								
								if AceConfigRegistry then
									-- Now rebuild the list
									AceConfigRegistry:NotifyChange("Inventorium");
								end
							end,
							get = false,
						},
						importPremadeData = {
							order = 40,
							type = "select",
							width = "double",
							name = "Import premade data",
							desc = "Import item data from a premade item-group. Any items already grouped will be skipped.",
							values = function()
								local temp = {};
								for key, group in pairs(addon.defaultGroups) do
									temp[key] = key;
								end
								
								return temp;
							end,
							set = function(info, value)
								local groupName = groupIdToName[info[2]];
								
								local premadeItemGroup = addon.defaultGroups[value];
								
								if premadeItemGroup.isParent and premadeItemGroup.childs then
									for _, premadeItemGroupName in pairs(premadeItemGroup.childs) do
										ImportPremadeItemsGroup(groupName, premadeItemGroupName);
									end
								else
									ImportPremadeItemsGroup(groupName, value);
								end
								
								if AceConfigRegistry then
									-- Now rebuild the list
									AceConfigRegistry:NotifyChange("Inventorium");
								end
							end,
							get = false,
						},
					},
				},
				massAdd = {
					order = 20,
					type = "group",
					inline = true,
					name = "Mass add",
					args = {
						help = {
							order = 10,
							type = "description",
							name = "Click the items you wish to add to this group or add multiple of these items at once by providing a name filter in the field below.",
							hidden = function() return addon.db.profile.defaults.hideHelp; end,
						},
						massAdd = {
							order = 20,
							type = "input",
							name = "Add all items matching...",
							desc = "Add every item in your inventory matching the name entered in this field. If you enter \"Glyph\" as a filter, any items in your inventory containing this in their name will be added to this group.",
							set = function(info, value)
						    	local groupName = groupIdToName[info[2]];
						    	
								if not value then return; end
								
								value = value:lower();
								
								local ref = options.args.groups.args[info[2]].args.add.args.list.args;
								
								for itemId, test in pairs(ref) do
									if test then
										local itemData = addon.ItemData:New(itemId);
										
										if itemData.name:lower():find(value) then
									    	if itemData:AddToGroup(groupName) then
												addon:Print(("Added %s to the selected group."):format( (itemData.link or unknownItemName:format(itemId)) ), addon.Colors.Green);
									    	else
									    		addon:Print(("Couldn't add %s because it is already in a group."):format(itemData.link or unknownItemName:format(itemId)), addon.Colors.Red);
									    	end
										end
									end
								end
								
								if AceConfigRegistry then
									-- Now rebuild the list
									AceConfigRegistry:NotifyChange("Inventorium");
								end
							end,
							get = false,
						},
						minItemLevel = {
							order = 40,
							type = "select",
							values = function()
								local temp = {};
								
								temp[0] = "Include everything";
								
								local itemLevelTemplate = "Itemlevel >= %d";
								
								for i = 1, 49 do
									temp[( i * 10 )] = itemLevelTemplate:format(( i * 10 ));
								end
								
								temp[500] = "Include nothing";
								
								return temp;
							end,
							name = "Include tradeskill items",
							desc = "Include all items above this item level from the currently opened tradeskill window in the below item list.\n\nSetting this very low this might considerably slow down this config window.\n\nSet to 500 to disable showing of items completely.",
							set = function(i, v) includeTradeSkillItems = v; end,
							get = function() return includeTradeSkillItems; end,
							disabled = function()
								if GetTradeSkillLine() == "UNKNOWN" then
									includeTradeSkillItems = 500;
									return true; -- disabled
								else
									return false;
								end
							end,
						},
					},
				},
				list = {
					order = 30,
					type = "group",
					inline = true,
					name = "Item list",
					hidden = UpdateAddItemList,
					args = {
					
					},
				},
			},
		},
		remove = {
			order = 40,
			type = "group",
			name = "Current items",
			desc = "View, export or remove items from this group.",
			hidden = function(info) return groupIsVirtual[info[2]]; end,
			args = {
				help = {
					order = 10,
					type = "group",
					inline = true,
					name = "Remove items",
					hidden = false,
					args = {
						help = {
							order = 10,
							type = "description",
							name = "Click the items you wish to remove from this group.",
							hidden = function() return addon.db.profile.defaults.hideHelp; end,
						},
						massRemove = {
							order = 20,
							type = "input",
							name = "Remove all items matching...",
							desc = "Remove every item in this group matching the name entered in this field. If you enter \"Glyph\" as a filter, any items in this group containing this in their name will be removed from this group.",
							set = function(info, value)
						    	local groupName = groupIdToName[info[2]];
						    	
								if not value then return; end
								
								value = value:lower();
								
								local ref = options.args.groups.args[info[2]].args.remove.args.list.args;
								
								for itemId, test in pairs(ref) do
									if test then
										local itemData = addon.ItemData:New(itemId);
										
										if itemData.name:lower():find(value) then
											itemData:RemoveFromGroup(groupName);
											
											addon:Print(("Removed %s from the selected group."):format( (itemData.link or unknownItemName:format(itemId)) ), addon.Colors.Red);
										end
									end
								end
								
								if AceConfigRegistry then
									-- Now rebuild the list
									AceConfigRegistry:NotifyChange("Inventorium");
								end
							end,
							get = false,
						},
						premadeGroups = {
							order = 30,
							type = "select",
							width = "double",
							name = "Imported premade groups",
							desc = "This is a list of all premade groups that were imported into this group. You will be notified when any of these premade groups have changed and you will be able to import these changes.\n\nSelect a group to stop reminding you of changes to the premade group (the item list will be unaffected). Doing so will require you to manually update this when new items are added to the game.",
							values = function(info)
						    	local groupName = groupIdToName[info[2]];
						    	
						    	local temp = {};
								temp[""] = "";
						    	if addon.db.profile.groups[groupName].premadeGroups then
							    	for name, version in pairs(addon.db.profile.groups[groupName].premadeGroups) do
							    		temp[name] = name;
							    	end
						    	end
						    	
						    	return temp;
							end,
							set = function(info, value)
								if value and value ~= "" then
									-- Remove premade group from this group
							    	local groupName = groupIdToName[info[2]];
							    	
							    	addon.db.profile.groups[groupName].premadeGroups[value] = nil;
							    	
							    	addon:Print(("No longer notifying you about changes made to the premade group named \"|cfffed000%s|r\"."):format(value));
						    	end
							end,
							get = false,
							disabled = function(info)
						    	local groupName = groupIdToName[info[2]];
						    	
								return (not addon.db.profile.groups[groupName].premadeGroups);
							end,
						},
					},
				},
				list = {
					order = 20,
					type = "group",
					inline = true,
					name = "Item list",
					hidden = UpdateRemoveItemList,
					args = {
					
					},
				},
				export = {
					order = 30,
					type = "group",
					name = "Export",
					inline = true,
					args = {
						input = {
							order = 10,
							type = "input",
							name = "Item data",
							width = "full",
							desc = "Export the item data for the currently selected group. Press CTRL-A to select all and CTRL-C to copy the text.",
							set = false,
							get = function(info)
								local groupName = groupIdToName[info[2]];
								
								local combinedItemIds;
								-- Parse items in group and show these
								for itemId, _ in pairs(addon.db.profile.groups[groupName].items) do
									if not combinedItemIds then
										combinedItemIds = tostring(itemId);
									else
										combinedItemIds = combinedItemIds .. (";%d"):format(itemId);
									end
								end
								
								return combinedItemIds; -- We don't serialize this because we actually DO want people to be able to manually modify it - besides, parsing it isn't going to be hard
							end,
						},
					},
				},
			},
		},
	},
};









-- Object functions

function mod:OnEnable()
	-- Register our config slash command
	-- /im config
	addon:RegisterSlash(function()
		if mod:IsFrameOpen() then
			mod:CloseFrame();
		else
			mod:Show();
		end
	end, { "c", "config", "conf", "option", "options", "opt", "setting", "settings" }, "|Hfunction:InventoriumCommandHandler:config|h|cff00fff7/im config|r|h (or /im c) - Open the config window to change the settings and manage groups.");
	
	self:Load(false);
	
	-- Whenever the profile is changed, update the groups | args: (object for the functionName, eventName, functionName)
	addon.db.RegisterCallback(self, "OnProfileChanged", "RefreshConfig");
	addon.db.RegisterCallback(self, "OnProfileCopied", "RefreshConfig");
	addon.db.RegisterCallback(self, "OnProfileReset", "RefreshConfig");
	
	-- Register our custom widgets
	local Widgets = addon:GetModule("Widgets");
	Widgets:ItemLinkButton();
	Widgets:ConfigItemLinkButton();
end

function mod:Show()
	-- We don't want any other windows open at this time.
	for name, module in addon:IterateModules() do
		if module.CloseFrame then
			module:CloseFrame();
		end
	end
	
	self:Load(true);
	
	AceConfigDialog:Open("Inventorium");
end

function mod:IsFrameOpen()
	return (AceConfigDialog.OpenFrames["Inventorium"] ~= nil);
end

function mod:CloseFrame()
	LibStub("AceConfigDialog-3.0"):Close("Inventorium");
end

function mod:ExportGroup(groupName)
	-- We want to include the group name, so we copy the table then set another value
	local temp = CopyTable(addon.db.profile.groups[groupName]);
	temp.name = groupName;
	temp.trackAtCharacters = nil;
	temp.overrideTrackAtCharacters = nil;
	temp.dontAlertAtCharacters = nil;
	temp.overrideDontAlertAtCharacters = nil;

	if not AceSerializer then
		AceSerializer = LibStub("AceSerializer-3.0");
	end

	return AceSerializer:Serialize(temp);
end

function mod:RefreshConfig()
	self:PremadeGroupsCheck();
	
	self:FillGroupOptions();
end

function mod:Load(premadeGroupsCheck)
	if premadeGroupsCheck then
		self:PremadeGroupsCheck();
	end
	
	if not AceConfigDialog and not AceConfigRegistry then
		self:FillOptions();
	
		-- Build options dialog
		AceConfigDialog = LibStub("AceConfigDialog-3.0");
		AceConfigRegistry = LibStub("AceConfigRegistry-3.0");
		-- Register options table
		LibStub("AceConfig-3.0"):RegisterOptionsTable("Inventorium", options);
		-- Set a nice default size (so that 4 normal sized elements fit next to eachother)
		AceConfigDialog:SetDefaultSize("Inventorium", 975, 600);
		
		 -- In case the addon is loaded from another condition, always call the remove interface options
		if AddonLoader and AddonLoader.RemoveInterfaceOptions then
			AddonLoader:RemoveInterfaceOptions("Inventorium");
		end
		
		-- Add to the blizzard addons options thing
		AceConfigDialog:AddToBlizOptions("Inventorium", "Inventorium");
	end
end

function mod:FillOptions()
	options = {
		type = "group",	
		name = "Inventorium Config",
		childGroups = "tree",
		args = {
		},
	};
	
	-- General
	self:FillGeneralOptions();
	
	-- Help
	self:FillHelpOptions();
	
	-- Profile
	options.args.profiles = LibStub("AceDBOptions-3.0"):GetOptionsTable(addon.db, true);
	options.args.profiles.order = 200;
	
	-- Extra
	self:FillExtraOptions();
	
	-- Groups
	self:MakeGroupOptions();
	
	-- Groups-contents
	self:FillGroupOptions();
end

function mod:FillGeneralOptions()
	options.args.general = {
		order = 100,
		type = "group",
		name = "General",
		desc = "Change general Inventorium settings.",
		args = {
			general = {
				order = 1,
				type = "group",
				inline = true,
				name = "General",
				args = {
					description = {
						order = 0,
						type = "description",
						name = function()
							local t = "";
							if not addon.db.profile.defaults.hideHelp then
								t = "Here you can set general settings. The settings entered here will be used when you choose not to override the settings within an individual group.";
							end
							
							return t;
						end,
					},
					header = {
						order = 5,
						type = "header",
						name = "",
					},
					trackAtCharacters = {
						order = 10,
						type = "multiselect",
						name = "Track at",
						desc = "Select at which characters groups should be tracked, appear in the summary or generate alerts.",
						values = function()
							local temp = {};
							for charName in pairs(addon.db.factionrealm.characters) do
								temp[charName] = charName;
							end
							
							return temp;
						end,
						get = function(i, v)
							return addon.db.profile.defaults.trackAtCharacters[v];
						end,
						set = function(i, v, e)
							addon.db.profile.defaults.trackAtCharacters[v] = e or nil;
						end,
						dialogControl = "Dropdown", -- this is not standard, normal multiselect control gives us a list of all chars with toggle-boxes. UGLY! We want a multiselect-box instead.
					},
					dontAlertAtCharacters = {
						order = 15,
						type = "multiselect",
						name = "Do |cffff0000not|r alert at:",
						desc = "Select at which characters items in groups should |cffff0000not|r generate alerts when they are below the required stock.",
						values = function()
							local temp = {};
							for charName in pairs(addon.db.factionrealm.characters) do
								temp[charName] = charName;
							end
							
							return temp;
						end,
						get = function(i, v)
							return addon.db.profile.defaults.dontAlertAtCharacters[v];
						end,
						set = function(i, v, e)
							addon.db.profile.defaults.dontAlertAtCharacters[v] = e or nil;
						end,
						dialogControl = "Dropdown", -- this is not standard, normal multiselect control gives us a list of all chars with toggle-boxes. UGLY! We want a multiselect-box instead.
					},
					--[[stockLevelAlertScanRepeatInterval = {
						order = 17,
						type = "multiselect",
						name = "Stock scan repeat interval",
						desc = "Select when or how often your stock levels should be checked and show alerts.",
						values = {
							["00Login"] = "At login",
							["01Repeat5"] = "Every 5 minutes",
							["02Repeat10"] = "Every 10 minutes",
							["03Repeat15"] = "Every 15 minutes",
							["04Repeat30"] = "Every 30 minutes",
							["05Repeat60"] = "Every 1 hour",
							["06Repeat120"] = "Every 2 hours",
						},
						get = function(i, v) return addon.db.profile.defaults.scanRepeatInterval and addon.db.profile.defaults.scanRepeatInterval[v]; end,
						set = function(i, v, e) addon.db.profile.defaults.scanRepeatInterval[v] = e; end, -- can't be nil or the defaults will be used
						dialogControl = "Dropdown", -- this is not standard, normal multiselect control gives us a list of all chars with toggle-boxes. UGLY! We want a multiselect-box instead.
					},]]
					localItemData = {
						order = 20,
						type = "multiselect",
						name = "Include in local item data",
						desc = "Select which data should be included in the local item data.",
						values = {
							["Bag"] = "Bag",
							["Bank"] = "Bank",
							["Auction House"] = "Auction House",
							["Mailbox"] = "Mailbox",
						},
						get = function(i, v) return addon.db.profile.defaults.localItemData and addon.db.profile.defaults.localItemData[v]; end,
						set = function(i, v, e) addon.db.profile.defaults.localItemData[v] = e; end, -- can't be nil or the defaults will be used
						dialogControl = "Dropdown", -- this is not standard, normal multiselect control gives us a list of all chars with toggle-boxes. UGLY! We want a multiselect-box instead.
					},
				},
			},
			minimumStock = {
				order = 10,
				type = "group",
				inline = true,
				name = "Minimum stock",
				args = {
					description = {
						order = 0,
						type = "description",
						name = "Here you can specify the default minimum amount of items you wish to keep in stock and related settings. The settings entered here will be used when you choose not to override the settings within an individual group.",
						hidden = function() return addon.db.profile.defaults.hideHelp; end,
					},
					header = {
						order = 5,
						type = "header",
						name = "",
						hidden = function() return addon.db.profile.defaults.hideHelp; end,
					},
					
					minLocalStock = {
						order = 10,
						type = "range",
						min = 0,
						max = 100000,
						softMax = 100,
						step = 1,
						name = "Minimum local stock",
						desc = "You can manually enter a value between 100 and 100.000 in the text box below if the provided range is insufficient.",
						get = function() return addon.db.profile.defaults.minLocalStock; end,
						set = function(i, v) addon.db.profile.defaults.minLocalStock = v; end,
					},
					alertBelowLocalMinimum = {
						order = 11,
						type = "toggle",
						name = "Alert when below local minimum",
						desc = "Show an alert when this item gets below this threshold.",
						get = function() return addon.db.profile.defaults.alertBelowLocalMinimum; end,
						set = function(i, v) addon.db.profile.defaults.alertBelowLocalMinimum = v; end,
					},
					autoRefill = {
						order = 12,
						type = "toggle",
						name = "Auto refill from storage",
						desc = "Automatically refill items from your storage (bank/mailbox - unless this is included in the local count - or the guild bank) when below the minimum local stock.",
						get = function() return addon.db.profile.defaults.autoRefill; end,
						set = function(i, v) addon.db.profile.defaults.autoRefill = v; end,
					},
					spacer = {
						order = 19,
						type = "description",
						name = "",
					},
					
					minGlobalStock = {
						order = 20,
						type = "range",
						min = 0,
						max = 100000,
						softMax = 100,
						step = 1,
						name = "Minimum global stock",
						desc = "You can manually enter a value between 100 and 100.000 in the text box below if the provided range is insufficient.",
						get = function() return addon.db.profile.defaults.minGlobalStock; end,
						set = function(i, v) addon.db.profile.defaults.minGlobalStock = v; end,
					},
					alertBelowGlobalMinimum = {
						order = 21,
						type = "toggle",
						name = "Alert when below global minimum",
						desc = "Show an alert when this item gets below this threshold.",
						get = function() return addon.db.profile.defaults.alertBelowGlobalMinimum; end,
						set = function(i, v) addon.db.profile.defaults.alertBelowGlobalMinimum = v; end,
					},
					
					summaryThresholdShow = {
						order = 30,
						type = "range",
						min = 0,
						max = 100,
						softMax = 100,
						step = 0.05,
						isPercent = true,
						name = "Show in summary when below",
						desc = "Show items in the summary when below this percentage of the minimum stock. This can be either below the minimum or the global stock.",
						get = function() return addon.db.profile.defaults.summaryThresholdShow; end,
						set = function(i, v) addon.db.profile.defaults.summaryThresholdShow = v; end,
					},
				},
			},
			refill = {
				order = 20,
				type = "group",
				inline = true,
				name = "Replenishing stock",
				args = {
					description = {
						order = 0,
						type = "description",
						name = function()
							local r = "Here you can specify the default amount of items to which you wish to restock when you are collecting new items. This may be higher than the minimum stock. The settings entered here will be used when you choose not to override the settings within an individual group.\n\n";
							
							r = r .. "When restocking the target amount is |cfffed000" .. addon.db.profile.defaults.restockTarget .. "|r of every item. Not queueing craftable items when only missing |cfffed000" .. floor( addon.db.profile.defaults.minCraftingQueue * addon.db.profile.defaults.restockTarget ) .. "|r (|cfffed000" .. ( addon.db.profile.defaults.minCraftingQueue * 100 ) .. "%|r) of the restock target.";
							
							return r;
						end,
						hidden = function() return addon.db.profile.defaults.hideHelp; end,
					},
					header = {
						order = 5,
						type = "header",
						name = "",
						hidden = function() return addon.db.profile.defaults.hideHelp; end,
					},
					restockTarget = {
						order = 10,
						type = "range",
						min = 0,
						max = 100000,
						softMax = 100,
						step = 1,
						name = "Restock target",
						desc = "You can manually enter a value between 100 and 100.000 in the edit box if the provided range is insufficient.",
						get = function() return addon.db.profile.defaults.restockTarget; end,
						set = function(i, v) addon.db.profile.defaults.restockTarget = v; end,
					},
					minCraftingQueue = {
						order = 20,
						type = "range",
						min = 0,
						max = 1,
						step = 0.01, -- 1%
						isPercent = true,
						name = "Don't queue if I only miss",
						desc = "Don't add a craftable item to the queue if I only miss this much or less of the restock target.\n\nExample: if your restock target is set to 60 and this is set to 5%, an item won't be queued unless you are missing more than 3 of it.",
						get = function() return addon.db.profile.defaults.minCraftingQueue; end,
						set = function(i, v) addon.db.profile.defaults.minCraftingQueue = v; end,
					},
					bonusQueue = {
						order = 30,
						type = "range",
						min = 0,
						max = 10, -- 1000%
						step = 0.01, -- 1%
						isPercent = true,
						name = "Bonus queue",
						desc = "Get additional items when there are none left.\n\nExample: if your restock target is set to 60 and this is set to 10%, you will get 66 items instead of just 60 if you end up with none left while queueing.",
						get = function() return addon.db.profile.defaults.bonusQueue; end,
						set = function(i, v) addon.db.profile.defaults.bonusQueue = v; end,
					},
					priceThreshold = {
						order = 40,
						type = "input",
						name = "Price threshold",
						desc = "Only queue craftable items when they are worth at least this much according to your auction house addon.\n\nSet to 0 to ignore auction prices.",
						validate = function(info, value) return addon:ValidateReadableMoney(info, value); end,
						get = function() return addon:ReadableMoney(addon.db.profile.defaults.priceThreshold); end,
						set = function(i, v) addon.db.profile.defaults.priceThreshold = addon:ReadableMoneyToCopper(v); end,
					},
					summaryHidePriceThreshold = {
						order = 50,
						type = "toggle",
						name = "Hide when below threshold",
						desc = "Hide items from the summary when their value is below the set price threshold.",
						get = function() return addon.db.profile.defaults.summaryHidePriceThreshold; end,
						set = function(i, v) addon.db.profile.defaults.summaryHidePriceThreshold = v; end,
					},
					alwaysGetAuctionValue = {
						order = 60,
						type = "toggle",
						name = "Always show auction value",
						desc = "Always cache and show the auction value of items, even if the price threshold is set to 0|cffeda55fc|r.",
						get = function() return addon.db.profile.defaults.alwaysGetAuctionValue; end,
						set = function(i, v) addon.db.profile.defaults.alwaysGetAuctionValue = v; end,
					},
				},
			},
			addon = {
				order = 30,
				type = "group",
				inline = true,
				name = "Prefered addons",
				args = {
					description = {
						order = 0,
						type = "description",
						name = function()
							local t = "";
							if not addon.db.profile.defaults.hideHelp then
								t = "Selecting your prefered addons is optional, the first working addon will be used if the selected addon doesn't exist. You only need to set this when you are running multiple addons of a kind and wish to specify which addons to use for what groups.\n\n";
							end
							
							local currentAddon, selectedAddonName = addon:GetItemCountAddon();
							local preferedAddon = addon.db.profile.defaults.itemCountAddon;
							
							if currentAddon then
								t = t .. "Currently using |cfffed000" .. selectedAddonName .. "|r as your item count addon. This addon is " .. ((currentAddon.IsEnabled() and "|cff00ff00enabled|r") or "|cffff0000disabled|r") .. ".";
								
								if currentAddon.GetTotalCount and currentAddon.GetCharacterCount then
									t = t .. " This addon supports |cfffed000both total as local|r item counts.";
								elseif currentAddon.GetTotalCount then
									t = t .. " This addon supports |cfffed000only total|r item counts.";
								elseif currentAddon.GetCharacterCount then
									t = t .. " This addon supports |cfffed000only local|r item counts.";
								end
								
								if preferedAddon ~= selectedAddonName then
									t = t .. "\n\n|cffff0000You have selected |cfffed000" .. preferedAddon .. "|r|cffff0000 as your item count addon, but this appears to be disabled and thus a random alternative was selected.|r";
								end
							end
							
							return t;
						end,
					},
					header = {
						order = 5,
						type = "header",
						name = "",
					},
					auctionPricingAddon = {
						order = 10,
						type = "select",
						name = "Prefered pricing addon",
						desc = "Select the addon you prefer data to be retrieved from. A random supported addon will be used if the selected addon can not be found.",
						values = function()
							local temp = {};
							for name, value in pairs(addon.supportedAddons.auctionPricing) do
								temp[name] = name;
							end
							
							return temp;
						end,
						get = function() return addon.db.profile.defaults.auctionPricingAddon; end,
						set = function(i, v)
							addon.db.profile.defaults.auctionPricingAddon = v;
							
							if addon.supportedAddons.auctionPricing[v].OnSelect then
								addon.supportedAddons.auctionPricing[v].OnSelect();
							end
						end,
					},
					itemCountAddon = {
						order = 20,
						type = "select",
						name = "Prefered item count addon",
						desc = "Select the addon you prefer data to be retrieved from. A random supported addon will be used if the selected addon can not be found.",
						values = function()
							local temp = {};
							for name, value in pairs(addon.supportedAddons.itemCount) do
								temp[name] = name;
							end
							
							return temp;
						end,
						get = function() return addon.db.profile.defaults.itemCountAddon; end,
						set = function(i, v)
							addon.db.profile.defaults.itemCountAddon = v;
							
							if addon.supportedAddons.itemCount[v].OnSelect then
								addon.supportedAddons.itemCount[v].OnSelect();
							end
						end,
					},
					craftingAddon = {
						order = 30,
						type = "select",
						name = "Prefered crafting addon",
						desc = "Select the addon you prefer data to be queued into. A random supported addon will be used if the selected addon can not be found.",
						values = function()
							local temp = {};
							for name, value in pairs(addon.supportedAddons.crafting) do
								temp[name] = name;
							end
							
							return temp;
						end,
						get = function() return addon.db.profile.defaults.craftingAddon; end,
						set = function(i, v)
							addon.db.profile.defaults.craftingAddon = v;
							
							if addon.supportedAddons.crafting[v].OnSelect then
								addon.supportedAddons.crafting[v].OnSelect();
							end
						end,
					},
					guildSelection = {
						order = 40,
						type = "multiselect",
						name = "Include guild bank data",
						desc = "Select which guild data should be included in the item counts.",
						values = function()
							local temp = {};
							
							local currentAddon, selectedAddonName = addon:GetItemCountAddon(groupName);
							
							if currentAddon and currentAddon.GetGuildNames then
								local guilds = currentAddon.GetGuildNames();
								
								if guilds and type(guilds) == "table" then
									for guildName, state in pairs(guilds) do
										temp[guildName] = guildName;
										
										if addon.db.profile.defaults.itemCountGuildsExcluded[guildName] then
											currentAddon.SetGuildState(guildName, false);
										else
											currentAddon.SetGuildState(guildName, true);
										end
									end
								end
							end
							
							return temp;
						end,
						get = function(i, v)
							local currentAddon, selectedAddonName = addon:GetItemCountAddon(groupName);
							
							return currentAddon and currentAddon.GetGuildNames and currentAddon.GetGuildNames()[v];
						end,
						set = function(i, v, e)
							local currentAddon, selectedAddonName = addon:GetItemCountAddon(groupName);
							
							if e then
								-- Guild is enabled, so not excluded
								addon.db.profile.defaults.itemCountGuildsExcluded[v] = nil;
							else
								addon.db.profile.defaults.itemCountGuildsExcluded[v] = true; -- this is excluded, excluded is indicated by true
							end
							
							if currentAddon and currentAddon.SetGuildState then
								currentAddon.SetGuildState(v, e);
							end
						end, -- can't be nil or the defaults will be used
						dialogControl = "Dropdown", -- this is not standard, normal multiselect control gives us a list of all chars with toggle-boxes. UGLY! We want a multiselect-box instead.
					},
				},
			},
		},
	};
end

function mod:FillExtraOptions()
	local selectedExportGroups, enableBackupGeneration;
	options.args.extra = {
		order = 300,
		type = "group",
		name = "Extra",
		desc = "Change additional (but completely optional) settings.",
		args = {
			misc = {
				order = 10,
				type = "group",
				inline = true,
				name = "Miscellaneous",
				args = {
					minimapIcon = {
						order = 0,
						type = "toggle",
						width = "full",
						name = "Display the minimap icon",
						desc = "Display the minimap icon for Inventorium allowing functionality to be used without typing slash commands.",
						get = function() return addon.db.profile.defaults.minimapIcon; end,
						set = function(i, v)
							addon.db.profile.defaults.minimapIcon = v;
							
							if v then
								addon:GetModule("MinimapIcon"):ShowIcon();
							else
								addon:GetModule("MinimapIcon"):HideIcon();
							end
						end,
					},
					hideHelp = {
						order = 10,
						type = "toggle",
						width = "full",
						name = "Hide any help tooltips, descriptions and the help config category",
						desc = "Hide any optional help tooltips, descriptions and the help config category.\n\nPlease note some tooltips may not disappear until next login.",
						get = function() return addon.db.profile.defaults.hideHelp; end,
						set = function(i, v) addon.db.profile.defaults.hideHelp = v; end,
					},
					autoRefillSkipConfirm = {
						order = 20,
						type = "toggle",
						width = "full",
						name = "Skip the confirmation window for storage refilling",
						desc = "Automatically start moving items from the storage (bank, guild bank or mailbox) without showing the confirmation window.",
						get = function() return addon.db.profile.defaults.autoRefillSkipConfirm; end,
						set = function(i, v) addon.db.profile.defaults.autoRefillSkipConfirm = v; end,
					},
					stockAlertScanInterval = {
						order = 25,
						type = "select",
						width = "double",
						name = "Stock scan speed",
						desc = "Select the speed at which items should be scanned for stock alerts. Faster requires more resources and may drastically reduce your frame rate during a scan.",
						values = {
							["0"] = "(Near) instant", -- scans in steps of 100
							["0.01"] = "Very fast", -- scans in steps of 2
							["0.05"] = "Fast",
							["0.1"] = "Default",
							["0.2"] = "Medium",
							["0.3"] = "Slow",
							["0.5"] = "Very slow",
						},
						get = function() return addon.db.profile.defaults.scanInterval; end,
						set = function(i, v) addon.db.profile.defaults.scanInterval = v; end,
					},
					spacer = {
						order = 26,
						type = "description",
						name = "",
					},
					removeCharacter = {
						order = 30,
						type = "select",
						width = "double",
						name = "Remove a character from Inventorium's memory",
						desc = "Select a character to remove all traces of it from Inventorium's memory.\n\nYour current character can not be removed, you must login to a different character to do so.",
						values = function()
							local temp = {};
							
							temp[""] = "";
							
							local playerName = UnitName("player");
							for charName in pairs(addon.db.factionrealm.characters) do
								if playerName ~= charName then
									temp[charName] = charName;
								end
							end
							
							return temp;
						end,
						set = function(i, value)
							if value and value ~= "" then
								addon.db.factionrealm.characters[value] = nil;
								addon.db.profile.defaults.trackAtCharacters[value] = nil;
								addon.db.profile.defaults.dontAlertAtCharacters[value] = nil;
								for name, values in pairs(addon.db.profile.groups) do
									if values.trackAtCharacters then
										values.trackAtCharacters[name] = nil;
									end
									
									if values.dontAlertAtCharacters then
										values.dontAlertAtCharacters[name] = nil;
									end
								end
							end
						end,
					},
				},
			},
			colorCodes = {
				order = 30,
				type = "group",
				inline = true,
				name = "Color codes",
				args = {
					description = {
						order = 0,
						type = "description",
						name = "Change the color code thresholds based on the current stock remaining of the required minimum stock.",
						hidden = function() return addon.db.profile.defaults.hideHelp; end,
					},
					header = {
						order = 5,
						type = "header",
						name = "",
						hidden = function() return addon.db.profile.defaults.hideHelp; end,
					},
					green = {
						order = 10,
						type = "range",
						min = 0,
						max = 1,
						step = 0.01,
						isPercent = true,
						name = "|cff00ff00Green|r",
						desc = "Show quantity in green when at least this much of the minimum stock is available.",
						get = function() return addon.db.profile.defaults.colors.green; end,
						set = function(i, v) addon.db.profile.defaults.colors.green = v; end,
					},
					yellow = {
						order = 20,
						type = "range",
						min = 0,
						max = 1,
						step = 0.01,
						isPercent = true,
						name = "|cffffff00Yellow|r",
						desc = "Show quantity in yellow when at least this much of the minimum stock is available.",
						get = function() return addon.db.profile.defaults.colors.yellow; end,
						set = function(i, v) addon.db.profile.defaults.colors.yellow = v; end,
					},
					orange = {
						order = 30,
						type = "range",
						min = 0,
						max = 1,
						step = 0.01,
						isPercent = true,
						name = "|cffff9933Orange|r",
						desc = "Show quantity in orange when at least this much of the minimum stock is available.",
						get = function() return addon.db.profile.defaults.colors.orange; end,
						set = function(i, v) addon.db.profile.defaults.colors.orange = v; end,
					},
					red = {
						order = 40,
						type = "range",
						min = 0,
						max = 1,
						step = 0.01,
						isPercent = true,
						name = "|cffff0000Red|r",
						desc = "Show quantity in red when at least this much of the minimum stock is available.",
						get = function() return addon.db.profile.defaults.colors.red; end,
						set = function(i, v) addon.db.profile.defaults.colors.red = v; end,
					},
				},
			},
			export = {
				order = 80,
				type = "group",
				inline = true,
				name = "Export groups",
				args = {
					description = {
						order = 0,
						type = "description",
						name = "Select the groups you wish to export below. Each group will have any account specific data removed and can safely and freely be shared with other users. All exported groups can be imported at once at the bottom of the \"Groups\" category of the config.",
						hidden = function() return addon.db.profile.defaults.hideHelp; end,
					},
					header = {
						order = 5,
						type = "header",
						name = "",
						hidden = function() return addon.db.profile.defaults.hideHelp; end,
					},
					localItemData = {
						order = 9,
						type = "multiselect",
						name = "Select groups",
						desc = "Select which groups should be included in the export.",
						values = function()
							local temp = {};
							
							if addon.db.profile.groups then
								temp["*InverseAll"] = "Inverse";
								
								for groupName, _ in pairs(addon.db.profile.groups) do
									temp[groupName] = groupName;
								end
							end
							
							return temp;
						end,
						get = function(info, value)
							--local groupName = groupIdToName[info[2]];
							--local optionName = info[#info];
							
							return selectedExportGroups and selectedExportGroups[value];
						end,
						set = function(info, name, value)
							--local groupName = groupIdToName[info[2]];
							--local optionName = info[#info];
							
							if not selectedExportGroups then
								selectedExportGroups = {};
							end
							
							if name == "*InverseAll" then
								for groupName, _ in pairs(addon.db.profile.groups) do
									if selectedExportGroups and selectedExportGroups[groupName] then
										selectedExportGroups[groupName] = nil;
									else
										selectedExportGroups[groupName] = true;
									end
								end
							else
								if selectedExportGroups and selectedExportGroups[name] then
									selectedExportGroups[name] = nil;
								else
									selectedExportGroups[name] = true;
								end
							end
						end,
					},
					input = {
						order = 10,
						type = "input",
						multiline = true,
						width = "full",
						name = "Exported data",
						desc = "Exported group data for the currently selected group(s). Press CTRL-A to select all and CTRL-C to copy the text.",
						set = false,
						get = function()
							local result = "";
							if selectedExportGroups then
								for groupName, v in pairs(selectedExportGroups) do
									if v then
										result = result .. mod:ExportGroup(groupName) .. "\n";
									end
								end
							end
							
							return result;
						end,
					},
				},
			},
			backup = {
				order = 100,
				type = "group",
				inline = true,
				name = "Generate a full backup",
				args = {
					description = {
						order = 0,
						type = "description",
						name = "With this you can generate a full backup of all your Inventorium settings. You can store this in a text file on your computer so you can import it at a later time if anything breaks to prevent losing a time consuming setup. You can also use it to share with other users but keep in mind this will include account specific data which you might not want in the wrong hands.",
						hidden = function() return addon.db.profile.defaults.hideHelp; end,
					},
					header = {
						order = 5,
						type = "header",
						name = "",
						hidden = function() return addon.db.profile.defaults.hideHelp; end,
					},
					generate = {
						order = 9,
						type = "toggle",
						width = "full",
						name = "Generate a full backup (might take a second)",
						desc = "Generate a full backup. This process might take 1 to 2 seconds.",
						set = function(_, v)
							enableBackupGeneration = v;
						end,
						get = function()
							return enableBackupGeneration;
						end,
					},
					input = {
						order = 10,
						type = "input",
						multiline = true,
						width = "full",
						name = "Exported data",
						desc = "Exported backup data. Press CTRL-A to select all and CTRL-C to copy the text.\n\nPlease note this includes character data.",
						set = false,
						get = function()
							if not enableBackupGeneration then
								return;
							end
							
							-- We want to include the group name, so we copy the table then set another value
							local temp = {
								["type"] = "backup",
								["global"] = addon.db.global or {},
								["profiles"] = addon.db.profiles or {},
								["factionrealm"] = addon.db.factionrealm or {},
							};

							if not AceSerializer then
								AceSerializer = LibStub("AceSerializer-3.0");
							end

							return AceSerializer:Serialize(temp);
						end,
					},
				},
			},
			importBackup = {
				order = 101,
				type = "group",
				inline = true,
				name = "Import a full backup",
				args = {
					description = {
						order = 0,
						type = "description",
						name = "With this you can import a full backup of all your Inventorium settings. |cffff0000Warning! Importing a backup will TRASH all your current settings and profiles. You are advised to store your own backup prior to importing one.|r",
						hidden = function() return addon.db.profile.defaults.hideHelp; end,
					},
					header = {
						order = 5,
						type = "header",
						name = "",
						hidden = function() return addon.db.profile.defaults.hideHelp; end,
					},
					input = {
						order = 10,
						type = "input",
						multiline = true,
						width = "full",
						name = "Paste the backup data here",
						desc = "Exported backup data. To copy the backup data, press CTRL-A to select all and CTRL-C to copy it. Then click this textblock and hit CTRL-V to paste it.",
						set = function(_, v)
							if v and string.trim(v) ~= "" then
								if not AceSerializer then
									AceSerializer = LibStub("AceSerializer-3.0");
								end
								
								local result, temp = AceSerializer:Deserialize(v);
								
								if result and temp and type(temp) == "table" and temp.type and temp.type == "backup" then
									addon.db:ResetDB("TEMPPROFILENAME" .. GetTime());
									
									local tempProfileName = addon.db:GetCurrentProfile();
									
									if temp.global then
										print("Importing |cfffed000global|r data...");
										
										-- Update by reference, rather than changing the reference
										for k, v in pairs(temp.global) do
											addon.db.global[k] = v;
										end
									end
									
									if temp.profiles then
										print("Importing |cfffed000profiles|r data...");
										
										-- Update by reference, rather than changing the reference
										for profileName, profile in pairs(temp.profiles) do
											print("Importing profile |cfffed000" .. profileName .. "|r...");
											
											addon.db.profiles[profileName] = profile;
											
											-- If the current profile is the temp profile, select the first profile in the backup
											if addon.db:GetCurrentProfile() == tempProfileName or profileName == "Default" then
												addon.db:SetProfile(profileName);
											end
											
											-- If our backup contains a profile with the same name as the temp profile, then don't delete the temp profile
											if profileName == tempProfileName then
												tempProfileName = nil;
											end
										end
									end
									
									-- Delete the temp profile
									if tempProfileName then
										addon.db:DeleteProfile(tempProfileName);
									end
									
									if temp.factionrealm then
										print("Importing |cfffed000character|r data...");
										
										-- Update by reference, rather than changing the reference
										for k, v in pairs(temp.factionrealm) do
											addon.db.factionrealm[k] = v;
										end
									end
									
									mod:FillGroupOptions();
									
									addon:Print("Import finished.", addon.Colors.Green);
								else
									addon:Print("The data provided is not supported.", addon.Colors.Red);
								end
							else
								addon:Print("The data provided is not supported.", addon.Colors.Red);
							end
						end,
						get = false,
						confirm = function() return "Are you sure you wish to override all your current settings with this data?"; end,
					},
				},
			},
		},
	};
end

function mod:FillHelpOptions()
	options.args.help = {
		order = 150,
		type = "group",
		hidden = function() return addon.db.profile.defaults.hideHelp; end,
		childGroups = "tab",
		name = "Help",
		desc = "Useful information for if you're unfamiliar with a part of the addon.",
		args = {
			general = {
				order = 1,
				type = "group",
				name = "General",
				args = {
					description = {
						order = 0,
						type = "description",
						name = "You can find a |cfffed000User Manual|r inside your addon folder or online at Google Docs (see the addon download page for a link). If you are new to this addon it may be a wealth of information.\n\n" .. 
								"Dropdown boxes that allow you to select more than 1 option should be closed using the \"|cfffed000Close|r\" option at the bottom before switching groups or closing the config. If this dropdown is still opened while switching groups or closing the config, it may cause an error and break the config requiring you to do /reloadui to continue working with the config.\n\n" .. 
								"Please request things you want and report anything that's clunky, weird, vague or otherwise buggy at |cfffed000the Inventorium ticket tracker|r. You can find this by going to the addon page.\n\n" .. 
								"You might notice the summary window currently gets a little slow when refreshed once you get over 200ish items in the list. This is a known issue and the summary window will be rewritten to resolve this somewhere in a future patch.",
					},
				},
			},
			FAQ = {
				order = 3,
				type = "group",
				name = "FAQ",
				args = {
					description = {
						order = 0,
						type = "description",
						name = "|cfffed000My groups don't appear in the summary window.|r\n" .. 
									"Please ensure your current character is toggled on at the \"track at\" option beneath the \"minimum stock\" category within a group or the defaults.\n\n" .. 
									
								"|cfffed000The auction value column always shows a \"-\".|r\n" .. 
									"The auction value will not be cached when you set the \"price threshold\" beneath the \"replenishing stock\" category to |cfffed0000c|r. You can change this behavior by adjusting this value or toggling the \"Always show auction value\" option on.\n\n" .. 
									
								"|cfffed000Some items appear in the \"unqueueable\" frame while I can find them in the profession.|r\n" .. 
									"Old items from before Cataclysm (such as any old gems) may have been renamed and their crafting skill removed. This might have resulted in certain items having the same name as others but with different ids, for example for Smooth King's Amber (search that at Wowhead for more info). Remove both items with the same name and add the one in your profession window again and this should no longer occur.\n\n" .. 
								
								"|cfffed000What use do profiles have?|r\n" .. 
									"Because there is already the \"track at\" option, profiles may not be useful to anyone. Nevertheless someone might find a use for it in some way and thus it is left available. You can use it to test certain things for example without the risk of your main groups being destroyed (although this should never be an excuse not to back up your settings from time to time).\n\n" .. 
								
								"|cfffed000Can you provide me with an example scenario for virtual groups?|r\n" .. 
									"The simplest (and possibly most popular) setup to imagine for using virtual groups are glyphs. There are over 300 glyphs available distributed over 10 classes (or 8 inks). Glyphs for certain classes (such as the tribrids; Druids & Paladins) might sell a lot more often than those for others (such as pure DPS; Hunters, etc.).\n\n" .. 
									"To get an easily adjustable setup you can make one virtual group, called \"Glyphs\" and override all your prefered settings in there. After you are done, make a glyph-group for each class (such as \"Glyphs (Death Knight)\" etc.) and select \"Glyphs\" as virtual group for every one of them (you can easily insert item data to these class specific groups by selecting premade data).\n\n" .. 
									"Now, to change the settings for all glyph groups you can just change the settings within the virtual \"Glyph\" group. To change the settings for one class in particular, such as Paladins because they sell more often than others, you can click this group and override the appropriate settings for just that group. There are many more possibilities for you to find out.\n\n" .. 
								"",
					},
				},
			},
		},
	};
end

function mod:MakeGroupOptions()
	options.args.groups = {
		order = 1100,
		type = "group",
		name = "Groups",
		desc = "Change a group.",
		args = {
			create = {
				order = 10,
				type = "group",
				inline = true,
				name = "Create a brand new group",
				args = {
					name = {
						order = 10,
						type = "input",
						name = "Group name",
						desc = "The name of the group. You can also use item links as you wish.",
						validate = ValidateGroupName,
						set = function(_, value)
							addon.db.profile.groups[value] = {};
							if currentGroupType == "Virtual" then
								addon.db.profile.groups[value].isVirtual = true;
							end
							
							mod:FillGroupOptions();
						end,
						get = false,
						width = "double",
					},
					type = {
						order = 20,
						type = "select",
						name = "Type (advanced)",
						desc = "The type of the new group. This can not be changed at a later time.\n\n|cffff9933This is an advanced option, you will probably not need it unless you manage a lot of groups.|r\n\n|cfffed000Normal|r: A normal group with complete functionality.\n\n|cfffed000Virtual|r: A virtual group which you can use to override the defaults for a set of groups. You can not add items to virtual groups.",
						values = {
							["Normal"] = "Normal",
							["Virtual"] = "Virtual",
						},
						set = function(_, value) currentGroupType = value; end,
						get = function() return currentGroupType; end,
					},
				},
			},
			import = {
				order = 20,
				type = "group",
				inline = true,
				name = "Import a group",
				args = {
					input = {
						order = 10,
						type = "input",
						multiline = true,
						name = "Group data",
						desc = "Paste the group data as provided by a group export. If you are trying to import multiple groups at the same time, make sure to use newlines to seperate them.",
						set = function(info, value)
							local data = { string.split("\n", value or "") };
							
							for _, current in pairs(data) do
								if current and string.trim(current) ~= "" then
									if not AceSerializer then
										AceSerializer = LibStub("AceSerializer-3.0");
									end
									
									local result, temp = AceSerializer:Deserialize(current);
									
									if not temp.name then
										addon:Print("The provided data is not supported.", addon.Colors.Red);
									elseif ValidateGroupName(nil, temp.name) ~= true then
										addon:Print(("Aborting: A group named \"%s\" already exists."):format(temp.name), addon.Colors.Red);
									else
										local name = temp.name;
										temp.name = nil;
										addon:Print(("Importing %s..."):format(name));
										
										if temp.items then
											-- Remove items that are already in another group
											for value, count in pairs(temp.items) do
												local itemId = tonumber(value);
												
												local itemData = addon.ItemData:New(itemId);
												
												if not itemId then
													addon:Print(("\"%s\" is not a number."):format(value), addon.Colors.Red);
													temp.items[value] = nil;
												elseif itemData:InGroup() then
													addon:Print(("Skipping %s as it is already in the group |cfffed000%s|r."):format( (itemData.link or unknownItemName:format(itemId)), itemData:InGroup() ), addon.Colors.Red);
													temp.items[value] = nil;
												else
													-- Ensure the keys are numeric
													temp.items[value] = nil;
													temp.items[itemId] = tonumber(count) or 0;
												end
											end
										end
										
										-- Ensure this data isn't received (this would be silly/buggy as exports from other accounts - with different characters - won't know what to do with this)
										temp.trackAtCharacters = nil;
										temp.overrideTrackAtCharacters = nil;
										temp.dontAlertAtCharacters = nil;
										temp.overrideDontAlertAtCharacters = nil;
										
										addon.db.profile.groups[name] = temp;
									end
								end
							end
							
							self:FillGroupOptions();
						end,
						get = false,
						width = "full",
					},
				},
			},
		},
	};
end

function mod:FillGroupOptions()
	for id, name in pairs(groupIdToName) do
		if type(name) == "string" and not addon.db.profile.groups[name] then
			options.args.groups.args[id] = nil;
			groupIdToName[id] = nil;
			groupIdToName[name] = nil;
			groupIsVirtual[id] = nil;
		end
	end
	
	for name, values in pairs(addon.db.profile.groups) do
		if not groupIdToName[name] then
			options.args.groups.args[tostring(count)] = defaultGroup;
			
			groupIdToName[tostring(count)] = name;
			groupIdToName[name] = true;
			if values.isVirtual then
				groupIsVirtual[tostring(count)] = true;
			end
			
			count = ( count + 1 );
		end
	end
end






-- Verify premade groups

function mod:PremadeGroupsCheck(updateGroupName, updateKey, accept)
	-- Compare the current premade groups with those used, notify about changes
	if addon.defaultGroups then
		for premadeGroupName, groupInfo in pairs(addon.defaultGroups) do
			-- Go through all default groups
			
			for groupName, values in pairs(addon.db.profile.groups) do
				-- Go through all groups to find those with this premade group
				
				if values.premadeGroups and values.premadeGroups[premadeGroupName] and values.premadeGroups[premadeGroupName] < groupInfo.version then
					-- Outdated group
					
					if updateGroupName and updateKey then
						-- This function was called after pressing yes or no in a confirm box
						
						if accept then
							-- Yes was clicked
							
							for itemId, version in pairs(groupInfo.items) do
								-- Go through all items in this premade group
								
								local itemData = addon.ItemData:New(itemId);
								
								if version > values.premadeGroups[premadeGroupName] then
									-- This item was added in a more recent version than this group: Add item
									
									if itemData:InGroup() then
										addon:Print(("Skipping %s as it is already in the group |cfffed000%s|r."):format( (itemData.link or unknownItemName:format(itemId)), itemData:InGroup() ));
									elseif itemData:AddToGroup(groupName) then
										addon:Print(("Added %s found in the premade group |cfffed000%s|r to the group |cfffed000%s|r."):format( (itemData.link or unknownItemName:format(itemId)), premadeGroupName, itemData:InGroup() ), addon.Colors.Green);
									end
								elseif ( version * -1 ) > values.premadeGroups[premadeGroupName] then
									if itemData:InGroup() == groupName then
										itemData:RemoveFromGroup(groupName);
										
										addon:Print(("Removed %s from the group |cfffed000%s|r as it was removed from the premade group |cfffed000%s|r."):format( (itemData.link or unknownItemName:format(itemId)), itemData:InGroup(), premadeGroupName ), addon.Colors.Red);
									else
										addon:Print(("Skipping the removal of %s as it isn't in the group |cfffed000%s|r."):format( (itemData.link or unknownItemName:format(itemId)), itemData:InGroup() ));
									end
								end
							end
							
							if AceConfigRegistry then
								-- Now rebuild the list
								AceConfigRegistry:NotifyChange("Inventorium");
							end
							
							-- Remember the new version
							values.premadeGroups[premadeGroupName] = groupInfo.version;
						else
							-- No was clicked
							
							-- Let user know what was not added
							for itemId, version in pairs(groupInfo.items) do
								-- Go through all items in this premade group
								
								local itemData = addon.ItemData:New(itemId);
								
								if version > values.premadeGroups[premadeGroupName] then
									-- This item was added in a more recent version than this group: don't add (since we clicked no), but announce it
									
									addon:Print(("Skipping %s found in the premade group |cfffed000%s|r."):format( (itemData.link or unknownItemName:format(itemId)), itemData:InGroup() ));
								end
							end
							
							-- Remember the new version
							values.premadeGroups[premadeGroupName] = groupInfo.version;
						end
					else
						StaticPopupDialogs["InventoriumConfirmUpdatePremadeGroup"] = {
							text = "The premade group |cfffed000%s|r used in the group |cfffed000%s|r has been changed. Do you wish to copy these changes?",
							button1 = YES,
							button2 = NO,
							OnAccept = function()
								mod:PremadeGroupsCheck(groupName, premadeGroupName, true);
							end,
							OnCancel = function(_, _, reason)
								if reason == "clicked" then
									mod:PremadeGroupsCheck(groupName, premadeGroupName, false);
								end
							end,
							timeout = 0,
							whileDead = 1,
							hideOnEscape = 1,
						};
						StaticPopup_Show("InventoriumConfirmUpdatePremadeGroup", premadeGroupName, groupName);
						
						return;
					end
				end
			end
		end
	end
end