if not PriceTracker then
	return
end

local PriceTracker = PriceTracker
local PriceTrackerMenu = {}
PriceTracker.menu = PriceTrackerMenu

PriceTrackerMenu.algorithmTable = {
	"Weighted Average",
	"Median",
	"Most Frequently Used"
}

PriceTrackerMenu.keyTable = {
	"None",
	"Shift",
	"Control",
	"Alt",
	"Command"
}

function PriceTrackerMenu:InitAddonMenu(addOnName)
	local panelData = {
		type = "panel",
		name = addOnName,
		displayName = PriceTracker.colors.title .. addOnName,
		author = "Barvazon",
		slashCommand = "/ptsetup",
		registerForRefresh = true
	}

	local optionsData = {
		[1] = {
			type = "dropdown",
			name = "Select price algorithm",
			choices = self.algorithmTable,
			getFunc = function() return PriceTracker.settings.algorithm or self.algorithmTable[1] end,
			setFunc = function(algorithm) PriceTracker.settings.algorithm = algorithm end,
			default = self.algorithmTable[1]
		},
		[2] = {
			type = "description",
			title = PriceTracker.colors.instructional .. "Weighted Average" .. PriceTracker.colors.default,
			text = "The average price of all items, with stack sizes taken into account.  For example, a stack of 10 items will be counted 10 times when calculating the average price."
		},
		[3] = {
			type = "description",
			title = PriceTracker.colors.instructional .. "Median" .. PriceTracker.colors.default,
			text = "The price value for which half of the items cost more and half cost less."
		},
		[4] = {
			type = "description",
			title = PriceTracker.colors.instructional .. "Most Frequently Used (also known as Mode)" .. PriceTracker.colors.default,
			text = "The most common price value."
		},
		[5] = {
			type = "checkbox",
			name = "Show Min / Max Prices",
			tooltip = "Show minimum and maximum sell values",
			getFunc = function() return PriceTracker.settings.showMinMax end,
			setFunc = function(check) PriceTracker.settings.showMinMax = check end,
			default = true
		},
		[6] = {
			type = "checkbox",
			name = "Show 'Seen'",
			tooltip = "Show how many times an item was seen in the guild stores",
			getFunc = function() return PriceTracker.settings.showSeen end,
			setFunc = function(check) PriceTracker.settings.showSeen = check end,
			default = true
		},
		[7] = {
			type = "dropdown",
			name = "Show only if key is pressed",
			tooltip = "Show pricing on tooltip only if one of the following keys is pressed.  This is useful if you have too many addons modifying your tooltips.",
			choices = self.keyTable,
			getFunc = function() return PriceTracker.settings.keyPress or self.keyTable[1] end,
			setFunc = function(key) PriceTracker.settings.keyPress = key end,
			default = self.keyTable[1]
		},
		[8] = {
			type = "dropdown",
			name = "Limit results to a specific guild",
			tooltip = "Check pricing data from all guild, or a specific one",
			choices = self:GetGuildList(),
			getFunc = function() return self:GetGuildList()[PriceTracker.settings.limitToGuild or 1] end,
			setFunc = function(...) self:setLimitToGuild(...) end,
			default = self:GetGuildList()[1]
		},
	}

	local LAM2 = LibStub:GetLibrary("LibAddonMenu-2.0")
	LAM2:RegisterAddonPanel("PriceTrackerOptions", panelData)
	LAM2:RegisterOptionControls("PriceTrackerOptions", optionsData)
end

function PriceTrackerMenu:IsKeyPressed()
	return PriceTracker.settings.keyPress == self.keyTable[1] or
		(PriceTracker.settings.keyPress == self.keyTable[2] and IsShiftKeyDown()) or
		(PriceTracker.settings.keyPress == self.keyTable[3] and IsControlKeyDown()) or
		(PriceTracker.settings.keyPress == self.keyTable[4] and IsAltKeyDown()) or
		(PriceTracker.settings.keyPress == self.keyTable[5] and IsCommandKeyDown())
end

function PriceTrackerMenu:GetGuildList()
	local guildList = {}
	guildList[1] = "All Guilds"
	for i = 1, GetNumGuilds() do
		guildList[i + 1] = GetGuildName(GetGuildId(i))
	end
	return guildList
end

function PriceTrackerMenu:setLimitToGuild(guildName)
	local guildList = self:GetGuildList()
	for i = 1, #guildList do
		if guildList[i] == guildName then
			PriceTracker.settings.limitToGuild = i
			return
		end
	end
	-- Guild not found.  Default to 'All Guilds'
	PriceTracker.settings.limitToGuild = 1
end