Quantcast
local addonname, addontbl = ...
local addon = Kungaloosh
local L = addontbl.L

local options = {}
local UpdateIconLists

local function getprofile(info)
	return addon.db.profile[info[2]]
end

local function setprofile(info, val)
	addon.db.profile[info[2]] = val
end

options.general = {
	type = "group",
	name = addonname,

	args = {
		general = {
			name = L["General"],
			type = "group",
			order = 1,
			inline = true,

			args = {
				desc = {
					name = function(info)
						for k,v in pairs(addon.db.profile.enabled_in) do
							if v == true then
								return L["Kungaloosh will be automatically enabled when getting assigned as tank but can also be manually enabled with the slash command |cff00ff00/kl|r."]
							end
						end

						return L["Kungaloosh must be manually enabled with the slash command |cff00ff00/kl|r."]
					end,
					type = "description",
					order = 1
				},

				sep = { order = 2, type = "header", name = "" },

				enabled_in = {
					name = L["Enable when assigned as tank in a ..."],
					desc = L["Enables the addon automatically if you\"re assigned as tank in a dungeon or raid"],
					type = "multiselect",
					order = 3,
					width = "half",

					values = { dungeon = L["Dungeon"], raid = L["Raid"] },
					get = function(info, key) return addon.db.profile.enabled_in[key] end,
					set = function(info, key, val) addon.db.profile.enabled_in[key] = val end
				},
			},
		},

		threat = {
			name = L["Threat"],
			type = "group",
			order = 2,
			inline = true,

			args = {
				desc = {
					name = function(info)
						return L["Kungaloosh will place a raid target icon on the target when you have done |cffff0000%d|r or more threat to it."]:format(addon:GetMinThreat())
					end,
					type = "description",
					order = 1
				},

				sep = { order = 2, type = "header", name = "" },

				dynamic_minthreat = {
					name = L["Dynamic threat value"],
					desc = L["Tries to guess a good threat value based on your level"],
					type = "toggle",
					order = 3,
					set = setprofile,
					get = getprofile
				},

				minthreat = {
					name = L["Manual threat value"],
					desc = L["Threat value to have before placing a raid target icon"],
					type = "input",
					order = 4,

					set = function(info, val) setprofile(info, tonumber(val)) end,
					get = function(info) return tostring(getprofile(info)) end,

					disabled = function(info) return addon.db.profile.dynamic_minthreat end,
					validate = function(info, value) if tonumber(value) then return true else return L["Invalid number"] end end
				}
			}
		},


		icons = {
			name = L["Icons"],
			type = "group",
			order = 3,
			inline = true,

			args = {
				desc = {
					name = L["Here you can customize what icons to use and in what order they will be placed. Click on the icon to either add or remove it."],
					type = "description",
					order = 1
				},

				sep = { order = 2, type = "header", name = "" },

				enabled = {
					name = L["Enabled"],
					type = "group",
					order = 3,

					args = {}
				},
				disabled = {
					name = L["Available"],
					type = "group",
					order = 4,

					args = {}
				},
				icon_recycle = {
					name = L["Recycle icons in combat"],
					desc = L["When this is disabled a icon is not recycled until combat has finished."],
					type = "toggle",
					order = 5,
					set = setprofile,
					get = getprofile
				},

				icon_recycle_time = {
					name = function(info) return L["Recycle icon from dead mob after %d seconds."]:format(getprofile(info)) end,
					desc = L["When a mob dies the icon it had will not be recycled until this time has passed."],
					type = "range",
					order = 6,
					width = "double",
					min = 1,
					max = 1000,
					softMin = 1,
					softMax = 200,
					step = 1,

					set = setprofile,
					get = getprofile,
					disabled = function(info) return not addon.db.profile.icon_recycle end,
				},
			},
		},
	}
}

local GetRaidTargetCoords, CreateIconOptions, IconExecuteFunc, UpdateIconLists
local iconopts = {}

function GetRaidTargetCoords(i)
	local index = i - 1
	local inc = RAID_TARGET_ICON_DIMENSION / RAID_TARGET_TEXTURE_DIMENSION
	local left = mod(index, RAID_TARGET_TEXTURE_COLUMNS) * inc
	local right = left + inc
	local top = floor(index / RAID_TARGET_TEXTURE_ROWS) * inc
	local bottom = top + inc

	return left, right, top, bottom
end

function IconExecuteFunc(info)
	local state, icon = info[2], tonumber(info[3])

	if state == "enabled" then
		for i,v in ipairs(addon.db.profile.raid_icons) do
			if v == icon then
				table.remove(addon.db.profile.raid_icons, i)
			end
		end
	else
		table.insert(addon.db.profile.raid_icons, icon)
	end

	UpdateIconLists()
end

function CreateIconOptions(icon)
	return {
		type = "execute",
		name = _G["RAID_TARGET_"..icon],
		image = [[Interface\TargetingFrame\UI-RaidTargetingIcons]],
		imageCoords = { GetRaidTargetCoords(icon) },
		imageWidth = 16,
		imageHeight = 16,
		width = "half",
		func = IconExecuteFunc
	}
end

function UpdateIconLists()
	local function IsEnabled(icon)
		for i,v in ipairs(addon.db.profile.raid_icons) do
			if v == icon then
				return true
			end
		end
	end

	table.wipe(options.general.args.icons.args.enabled.args)
	table.wipe(options.general.args.icons.args.disabled.args)

	for i, icon in ipairs(addon.db.profile.raid_icons) do
		local icontbl = iconopts[icon]
		icontbl.order = i

		options.general.args.icons.args.enabled.args[tostring(icon)] = icontbl
	end

	for i = 1,8 do
		if not IsEnabled(i) then
			local icontbl = iconopts[i]
			icontbl.order = i

			options.general.args.icons.args.disabled.args[tostring(i)] = icontbl
		end
	end

	LibStub("AceConfigRegistry-3.0"):NotifyChange(addonname)
end

function addon:CreateConfig()
	options.profiles = LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db)

	LibStub("AceConfig-3.0"):RegisterOptionsTable("Kungaloosh-General", options.general)
	LibStub("AceConfig-3.0"):RegisterOptionsTable("Kungaloosh-Profiles", options.profiles)

	LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Kungaloosh-General", addonname)
	LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Kungaloosh-Profiles", "Profiles", addonname)

	LibStub("tekKonfig-AboutPanel").new(addonname, addonname)


	for i = 1,8 do
		iconopts[i] = CreateIconOptions(i)
	end

	self.db.RegisterCallback(self, "OnNewProfile", UpdateIconLists)
	self.db.RegisterCallback(self, "OnProfileChanged", UpdateIconLists)
	self.db.RegisterCallback(self, "OnProfileCopied", UpdateIconLists)
	self.db.RegisterCallback(self, "OnProfileDeleted", UpdateIconLists)
	self.db.RegisterCallback(self, "OnProfileReset", UpdateIconLists)

	UpdateIconLists()
end