Quantcast
--[[
##############################################################################
_____/\\\\\\\\\\\____/\\\________/\\\__/\\\________/\\\__/\\\\\\\\\\\_       #
 ___/\\\/////////\\\_\/\\\_______\/\\\_\/\\\_______\/\\\_\/////\\\///__      #
  __\//\\\______\///__\//\\\______/\\\__\/\\\_______\/\\\_____\/\\\_____     #
   ___\////\\\__________\//\\\____/\\\___\/\\\_______\/\\\_____\/\\\_____    #
    ______\////\\\________\//\\\__/\\\____\/\\\_______\/\\\_____\/\\\_____   #
     _________\////\\\______\//\\\/\\\_____\/\\\_______\/\\\_____\/\\\_____  #
      __/\\\______\//\\\______\//\\\\\______\//\\\______/\\\______\/\\\_____ #
       _\///\\\\\\\\\\\/________\//\\\________\///\\\\\\\\\/____/\\\\\\\\\\\_#
        ___\///////////___________\///___________\/////////_____\///////////_#
##############################################################################
S U P E R - V I L L A I N - U I   By: Munglunch                              #
##############################################################################
##########################################################
LOCALIZED LUA FUNCTIONS
##########################################################
]]--
--[[ GLOBALS ]]--
local _G = _G;
local unpack    = _G.unpack;
local select    = _G.select;
local pairs     = _G.pairs;
local next 		= _G.next;
local type      = _G.type;
local error 	= _G.error;
local rawset    = _G.rawset;
local rawget    = _G.rawget;
local table     = _G.table;
--[[ TABLE METHODS ]]--
local tremove, tcopy, twipe, tsort, tconcat, tdump = table.remove, table.copy, table.wipe, table.sort, table.concat, table.dump;
--[[
##########################################################
GET ADDON DATA
##########################################################
]]--
local SV, L, Registry = unpack(select(2, ...));
local realm = GetRealmName()
local name = UnitName("player")
local pkey = ("%s - %s"):format(name, realm)
local databaseListener = CreateFrame("Frame", nil)
local CONFIGS = SV.Configs
--[[
##########################################################
DB BUILD HELPERS
##########################################################
]]--
local function SpellName(id)
	local name, _, _, _, _, _, _, _, _ = GetSpellInfo(id)
	if not name then
		SV:Debugger('|cffFF9900SVUI:|r Spell not found: (#ID) '..id)
		name = "Voodoo Doll";
	end
	return name
end
--[[
##########################################################
DB PROFILE
##########################################################
]]--
CONFIGS["copyKey"] = pkey
CONFIGS["profileKey"] = pkey
CONFIGS["framelocations"] = {}
CONFIGS["system"] = {
	["cooldown"] = true,
	["autoScale"] = true,
	["multiMonitor"] = false,
	["taintLog"] = false,
	["stickyFrames"] = true,
	["loginmessage"] = true,
	["hideErrorFrame"] = true,
	["threatbar"] = false,
	["bubbles"] = true,
	["comix"] = true,
	["bigComix"] = true,
	["questWatch"] = true,
	["woot"] = true,
	["pvpinterrupt"] = true,
	["lookwhaticando"] = false,
	["reactionChat"] = false,
	["sharingiscaring"] = false,
	["arenadrink"] = true,
	["stupidhat"] = true,
	["totems"] = {
		["enable"] = true,
		["showBy"] = "VERTICAL",
		["sortDirection"] = "ASCENDING",
		["size"] = 40,
		["spacing"] = 4
	},
}
CONFIGS["media"] = {
	["fonts"] = {
		["default"] = "SVUI System Font",
		["name"] = "SVUI Name Font",
		["number"] = "SVUI Number Font",
		["combat"] = "SVUI Combat Font",
		["giant"] = "SVUI Action Font",
		["size"] = 10,
		["unicodeSize"] = 12,
	},
	["textures"] = {
		["pattern"] 	 = "SVUI Backdrop 1",
		["comic"] 		 = "SVUI Comic 1",
		["unitlarge"] 	 = "SVUI Unit BG 3",
		["unitsmall"] 	 = "SVUI Small BG 3"
	},
	["colors"] = {
		["default"] 	 = {0.2, 0.2, 0.2, 1},
		["special"] 	 = {0.37, 0.32, 0.29, 1},
	},
	["unitframes"] = {
		["health"] 		 = {0.3, 0.5, 0.3},
		["power"] 		 = {
			["MANA"] 		 = {0.41, 0.85, 1},
			["RAGE"] 		 = {1, 0.31, 0.31},
			["FOCUS"] 		 = {1, 0.63, 0.27},
			["ENERGY"] 		 = {0.85, 0.83, 0.25},
			["RUNES"] 		 = {0.55, 0.57, 0.61},
			["RUNIC_POWER"] = {0, 0.82, 1},
			["FUEL"] 		 = {0, 0.75, 0.75}
		},
		["reaction"] 	 = {
			[1] = {0.92, 0.15, 0.15},
			[2] = {0.92, 0.15, 0.15},
			[3] = {0.92, 0.15, 0.15},
			[4] = {0.85, 0.85, 0.13},
			[5] = {0.19, 0.85, 0.13},
			[6] = {0.19, 0.85, 0.13},
			[7] = {0.19, 0.85, 0.13},
			[8] = {0.19, 0.85, 0.13},
		},
		["tapped"] 			 = {0.55, 0.57, 0.61},
		["disconnected"] 	 = {0.84, 0.75, 0.65},
		["casting"] 		 = {0.8, 0.8, 0},
		["spark"] 			 = {1, 0.72, 0},
		["interrupt"] 		 = {0.78, 0.25, 0.25},
		["shield_bars"] 	 = {0.56, 0.4, 0.62},
		["buff_bars"] 		 = {0.31, 0.31, 0.31},
		["debuff_bars"] 	 = {0.8, 0.1, 0.1},
		["predict"] 		 = {
			["personal"] 		 = {0, 1, 0.5, 0.25},
			["others"] 			 = {0, 1, 0, 0.25},
			["absorbs"] 		 = {1, 1, 0, 0.25}
		},
		["spellcolor"] = {
			[SpellName(2825)] = {0.98, 0.57, 0.11}, 	--Bloodlust
			[SpellName(32182)] = {0.98, 0.57, 0.11}, --Heroism
			[SpellName(80353)] = {0.98, 0.57, 0.11}, --Time Warp
			[SpellName(90355)] = {0.98, 0.57, 0.11}, --Ancient Hysteria
			[SpellName(84963)] = {0.98, 0.57, 0.11}, --Inquisition
			[SpellName(86659)] = {0.98, 0.57, 0.11}, --Guardian of Ancient Kings
		}
	}
}
CONFIGS["filter"] = {
	["CC"] = {},
	["Shield"] = {},
	["Player"] = {},
	["Blocked"] = {},
	["Allowed"] = {},
	["Strict"] = {},
	["Raid"] = {},
}

--[[
##########################################################
CONSTRUCT FILTER DEFAULTS
##########################################################
]]--
local CLASS_WATCH_INDEX = {
	PRIEST = {
		{-- Weakened Soul
			["enabled"] = true,
			["id"] = 6788,
			["point"] = "TOPRIGHT",
			["color"] = {["r"] = 1, ["g"] = 0, ["b"] = 0},
			["anyUnit"] = true,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Prayer of Mending
			["enabled"] = true,
			["id"] = 41635,
			["point"] = "BOTTOMRIGHT",
			["color"] = {["r"] = 0.2, ["g"] = 0.7, ["b"] = 0.2},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Renew
			["enabled"] = true,
			["id"] = 139,
			["point"] = "BOTTOMLEFT",
			["color"] = {["r"] = 0.4, ["g"] = 0.7, ["b"] = 0.2},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Power Word: Shield
			["enabled"] = true,
			["id"] = 17,
			["point"] = "TOPLEFT",
			["color"] = {["r"] = 0.81, ["g"] = 0.85, ["b"] = 0.1},
			["anyUnit"] = true,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Power Word: Shield Power Insight
			["enabled"] = true,
			["id"] = 123258,
			["point"] = "TOPLEFT",
			["color"] = {["r"] = 0.81, ["g"] = 0.85, ["b"] = 0.1},
			["anyUnit"] = true,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Power Infusion
			["enabled"] = true,
			["id"] = 10060,
			["point"] = "RIGHT",
			["color"] = {["r"] = 0.89, ["g"] = 0.09, ["b"] = 0.05},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Guardian Spirit
			["enabled"] = true,
			["id"] = 47788,
			["point"] = "LEFT",
			["color"] = {["r"] = 0.86, ["g"] = 0.44, ["b"] = 0},
			["anyUnit"] = true,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Pain Suppression
			["enabled"] = true,
			["id"] = 33206,
			["point"] = "LEFT",
			["color"] = {["r"] = 0.89, ["g"] = 0.09, ["b"] = 0.05},
			["anyUnit"] = true,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
	},
	DRUID = {
		{-- Rejuvenation
			["enabled"] = true,
			["id"] = 774,
			["point"] = "TOPRIGHT",
			["color"] = {["r"] = 0.8, ["g"] = 0.4, ["b"] = 0.8},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Regrowth
			["enabled"] = true,
			["id"] = 8936,
			["point"] = "BOTTOMLEFT",
			["color"] = {["r"] = 0.2, ["g"] = 0.8, ["b"] = 0.2},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Lifebloom
			["enabled"] = true,
			["id"] = 33763,
			["point"] = "TOPLEFT",
			["color"] = {["r"] = 0.4, ["g"] = 0.8, ["b"] = 0.2},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Wild Growth
			["enabled"] = true,
			["id"] = 48438,
			["point"] = "BOTTOMRIGHT",
			["color"] = {["r"] = 0.8, ["g"] = 0.4, ["b"] = 0},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
	},
	PALADIN = {
		{-- Beacon of Light
			["enabled"] = true,
			["id"] = 53563,
			["point"] = "TOPRIGHT",
			["color"] = {["r"] = 0.7, ["g"] = 0.3, ["b"] = 0.7},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Hand of Protection
			["enabled"] = true,
			["id"] = 1022,
			["point"] = "BOTTOMRIGHT",
			["color"] = {["r"] = 0.2, ["g"] = 0.2, ["b"] = 1},
			["anyUnit"] = true,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Hand of Freedom
			["enabled"] = true,
			["id"] = 1044,
			["point"] = "BOTTOMRIGHT",
			["color"] = {["r"] = 0.89, ["g"] = 0.45, ["b"] = 0},
			["anyUnit"] = true,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Hand of Salvation
			["enabled"] = true,
			["id"] = 1038,
			["point"] = "BOTTOMRIGHT",
			["color"] = {["r"] = 0.93, ["g"] = 0.75, ["b"] = 0},
			["anyUnit"] = true,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Hand of Sacrifice
			["enabled"] = true,
			["id"] = 6940,
			["point"] = "BOTTOMRIGHT",
			["color"] = {["r"] = 0.89, ["g"] = 0.1, ["b"] = 0.1},
			["anyUnit"] = true,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Hand of Purity
			["enabled"] = true,
			["id"] = 114039,
			["point"] = "BOTTOMRIGHT",
			["color"] = {["r"] = 0.64, ["g"] = 0.41, ["b"] = 0.72},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Sacred Shield
			["enabled"] = true,
			["id"] = 20925,
			["point"] = "TOPLEFT",
			["color"] = {["r"] = 0.93, ["g"] = 0.75, ["b"] = 0},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Eternal Flame
			["enabled"] = true,
			["id"] = 114163,
			["point"] = "BOTTOMLEFT",
			["color"] = {["r"] = 0.87, ["g"] = 0.7, ["b"] = 0.03},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
	},
	SHAMAN = {
		{-- Riptide
			["enabled"] = true,
			["id"] = 61295,
			["point"] = "TOPRIGHT",
			["color"] = {["r"] = 0.7, ["g"] = 0.3, ["b"] = 0.7},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Earth Shield
			["enabled"] = true,
			["id"] = 974,
			["point"] = "BOTTOMLEFT",
			["color"] = {["r"] = 0.2, ["g"] = 0.7, ["b"] = 0.2},
			["anyUnit"] = true,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Earthliving
			["enabled"] = true,
			["id"] = 51945,
			["point"] = "BOTTOMRIGHT",
			["color"] = {["r"] = 0.7, ["g"] = 0.4, ["b"] = 0.4},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
	},
	MONK = {
		{--Renewing Mist
			["enabled"] = true,
			["id"] = 119611,
			["point"] = "TOPLEFT",
			["color"] = {["r"] = 0.8, ["g"] = 0.4, ["b"] = 0.8},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Life Cocoon
			["enabled"] = true,
			["id"] = 116849,
			["point"] = "TOPRIGHT",
			["color"] = {["r"] = 0.2, ["g"] = 0.8, ["b"] = 0.2},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Enveloping Mist
			["enabled"] = true,
			["id"] = 132120,
			["point"] = "BOTTOMLEFT",
			["color"] = {["r"] = 0.4, ["g"] = 0.8, ["b"] = 0.2},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Zen Sphere
			["enabled"] = true,
			["id"] = 124081,
			["point"] = "BOTTOMRIGHT",
			["color"] = {["r"] = 0.7, ["g"] = 0.4, ["b"] = 0},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
	},
	ROGUE = {
		{-- Tricks of the Trade
			["enabled"] = true,
			["id"] = 57934,
			["point"] = "TOPRIGHT",
			["color"] = {["r"] = 0.89, ["g"] = 0.09, ["b"] = 0.05},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
	},
	MAGE = {
		{-- Ice Ward
			["enabled"] = true,
			["id"] = 111264,
			["point"] = "TOPLEFT",
			["color"] = {["r"] = 0.2, ["g"] = 0.2, ["b"] = 1},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
	},
	WARRIOR = {
		{-- Vigilance
			["enabled"] = true,
			["id"] = 114030,
			["point"] = "TOPLEFT",
			["color"] = {["r"] = 0.2, ["g"] = 0.2, ["b"] = 1},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Intervene
			["enabled"] = true,
			["id"] = 3411,
			["point"] = "TOPRIGHT",
			["color"] = {["r"] = 0.89, ["g"] = 0.09, ["b"] = 0.05},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
		{-- Safe Guard
			["enabled"] = true,
			["id"] = 114029,
			["point"] = "TOPRIGHT",
			["color"] = {["r"] = 0.89, ["g"] = 0.09, ["b"] = 0.05},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
	},
	DEATHKNIGHT = {
		{-- Unholy Frenzy
			["enabled"] = true,
			["id"] = 49016,
			["point"] = "TOPRIGHT",
			["color"] = {["r"] = 0.89, ["g"] = 0.09, ["b"] = 0.05},
			["anyUnit"] = false,
			["onlyShowMissing"] = false,
			['style'] = 'coloredIcon',
			['displayText'] = false,
			['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
			['textThreshold'] = -1,
			['xOffset'] = 0,
			['yOffset'] = 0
		},
	},
}

local PET_WATCH = {
	{-- Frenzy
		["enabled"] = true,
		["id"] = 19615,
		["point"] = "TOPLEFT",
		["color"] = {["r"] = 0.89, ["g"] = 0.09, ["b"] = 0.05},
		["anyUnit"] = true,
		["onlyShowMissing"] = false,
		['style'] = 'coloredIcon',
		['displayText'] = false,
		['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
		['textThreshold'] = -1,
		['xOffset'] = 0,
		['yOffset'] = 0
	},
	{-- Mend Pet
		["enabled"] = true,
		["id"] = 136,
		["point"] = "TOPRIGHT",
		["color"] = {["r"] = 0.2, ["g"] = 0.8, ["b"] = 0.2},
		["anyUnit"] = true,
		["onlyShowMissing"] = false,
		['style'] = 'coloredIcon',
		['displayText'] = false,
		['textColor'] = {["r"] = 1, ["g"] = 1, ["b"] = 1},
		['textThreshold'] = -1,
		['xOffset'] = 0,
		['yOffset'] = 0
	},
}

local FilterIDs = {
	["Blocked"] = [[36900,36901,36893,114216,97821,36032,132365,8733,57724,25771,57723,36032,58539,26013,6788,71041,41425,55711,8326,23445,24755,25163,80354,95809,95223,124275,124274,124273,117870,123981,15007,113942,89140]],

	["Allowed"] = [[31821,2825,32182,80353,90355,47788,33206,116849,22812,1490,116202,123059,136431,137332,137375,144351,142863,142864,142865,143198]],

	["Strict"] = [[123059,136431,137332,137375,144351,142863,142864,142865,143198]],

	["CC"] = [[47476,91800,91807,91797,108194,115001,33786,2637,339,78675,22570,5211,9005,102359,99,127797,45334,102795,114238,113004,3355,1513,19503,34490,24394,64803,19386,117405,128405,50519,91644,90337,54706,4167,90327,56626,50245,50541,96201,96201,31661,118,55021,122,82691,118271,44572,33395,102051,20066,10326,853,105593,31935,105421,605,64044,8122,9484,15487,114404,88625,113792,87194,2094,1776,6770,1833,51722,1330,408,88611,115197,113953,51514,64695,63685,76780,118905,118345,710,6789,118699,5484,6358,30283,24259,115782,115268,118093,89766,137143,20511,7922,676,105771,107566,132168,107570,118895,18498,116706,117368,115078,122242,119392,119381,120086,116709,123407,140023,25046,20549,107079]],

	["Shield"] = [[17,47515,45243,45438,115610,48797,48792,49039,87256,55233,50461,33206,47788,62618,47585,104773,110913,108359,22812,102342,106922,61336,19263,53480,1966,31224,74001,5277,45182,98007,30823,108271,1022,6940,114039,31821,498,642,86659,31850,118038,55694,97463,12975,114029,871,114030,120954,131523,122783,122278,115213,116849,20594]],

	["Player"] = [[17,47515,45243,45438,45438,115610,110909,12051,12472,80353,12042,32612,110960,108839,111264,108843,48797,48792,49039,87256,49222,55233,50461,49016,51271,96268,33206,47788,62618,47585,6346,10060,114239,119032,27827,104773,110913,108359,113860,113861,113858,88448,22812,102342,106922,61336,117679,102543,102558,102560,16689,132158,106898,1850,106951,29166,52610,69369,112071,124974,19263,53480,51755,54216,34471,3045,3584,131894,90355,90361,31224,74001,5277,45182,51713,114018,2983,121471,11327,108212,57933,79140,13750,98007,30823,108271,16188,2825,79206,16191,8178,58875,108281,108271,16166,114896,1044,1022,1038,6940,114039,31821,498,642,86659,20925,31850,31884,53563,31842,54428,105809,85499,118038,55694,97463,12975,114029,871,114030,18499,1719,23920,114028,46924,3411,107574,120954,131523,122783,122278,115213,116849,125174,116841,20594,59545,20572,26297,68992]],

	["Raid"] = [[116281,116784,116417,116942,116161,117708,118303,118048,118135,117878,117949,116835,116778,116525,122761,122760,122740,123812,123180,123474,122835,123081,122125,121885,121949,117436,118091,117519,122752,123011,116161,123121,119985,119086,119775,122151,138349,137371,136767,137641,137359,137972,136903,136753,137633,137731,133767,133768,136050,138569,134691,137440,137408,137360,135000,143436,143579,147383,146124,144851,144358,144774,147207,144215,143990,144330,143494,142990,143919,143766,143773,146589,143777,143385,143974,145183]]
}

local FilterOverrides = {
	["45438"] = 5, ["48797"] = 5, ["87256"] = 4,
	["33206"] = 3, ["47585"] = 5, ["22812"] = 2,
	["102342"] = 2, ["19263"] = 5, ["5277"] = 5,
	["1022"] = 5, ["31821"] = 3, ["498"] = 2,
	["642"] = 5, ["86659"] = 4, ["31850"] = 4,
	["118038"] = 5, ["114029"] = 2, ["871"] = 3,
	["120954"] = 2, ["131523"] = 5
}

local function safename(id)
	local n = GetSpellInfo(id)
	if not n then
		if type(id) == "string" then
			n = id
		else
			SV:Debugger('|cffFF9900SVUI:|r Spell not found: (#ID) '..id)
			n = "Voodoo Doll";
		end
	end
	return n
end

for k, x in pairs(FilterIDs) do
	local src = {};
	for id in x:gmatch("([^,]+)") do
		if(id) then
			local saved
			local n = safename(id);
			local p = FilterOverrides[tostring(id)] or 0;
			if k == "Strict" then
				saved = {['enable'] = true, ['spellID'] = id, ['priority'] = p}
			else
				saved = {['enable'] = true, ['priority'] = p}
			end
			src[n] = saved
		end
	end
	CONFIGS.filter[k] = src
end

CONFIGS.filter.BuffWatch = CLASS_WATCH_INDEX[SV.class] or {}
CONFIGS.filter.PetBuffWatch = PET_WATCH
--[[
##########################################################
CREATE DB
##########################################################
]]--
local function tablecopy(d, s)
    if type(s) ~= "table" then return end
    if type(d) == "table" then
        for k, v in pairs(s) do
            if type(v) == "table" then
                if not rawget(d, k) then rawset(d, k, {}) end
                tablecopy(d[k], v)
            else
                if rawget(d, k) == nil then
                    rawset(d, k, v)
                end
            end
        end
    end
end

local function importdata(s, d)
    if type(d) ~= "table" then d = {} end
    if type(s) == "table" then
        for k,v in pairs(s) do
            if type(v) == "table" then
                v = importdata(v, d[k])
            end
            d[k] = v
        end
    end
    return d
end

local function setdefaults(t, key, sub, sub2)
    local sv = rawget(t, "profile")
    local src = rawget(t, "defaults")
    local savedProfile = sv[key]
    if(sub2 and sv[key] and sv[key][sub]) then
        savedProfile = sv[key][sub][sub2]
    elseif(sub and sv[key]) then
        savedProfile = sv[key][sub]
    else
        savedProfile = sv[key]
    end
    if(savedProfile) then
        for k,v in pairs(savedProfile) do
            savedProfile[k] = nil
        end
    else
        sv[key] = {}
    end
    tablecopy(sv[key], src[key])
end

local function getdefaults(t, key)
    local src = rawget(t, "defaults")
    return src[key] or {}
end

local function removedefaults(db, src, nometa)
    if(type(src) ~= "table") then return end
    if(not nometa) then
        setmetatable(db, nil)
    end
    for k,v in pairs(src) do
        if type(v) == "table" and type(db[k]) == "table" then
            removedefaults(db[k], v, nometa)
            if next(db[k]) == nil then
                db[k] = nil
            end
        else
            if db[k] == v then
                db[k] = nil
            end
        end
    end
end

local function resetprofile(t)
    local sv = rawget(t, "profile")
    local src = rawget(t, "defaults")
    for k,v in pairs(sv) do
        sv[k] = nil
    end
end

local function importprofile(t, key)
    local sv = rawget(t, "profile")
    local dv = rawget(t, "defaults")
    local globals = SV.Global
    local src = globals.profiles[key]
    if(not src) then return end
    for k,v in pairs(sv) do
        sv[k] = nil
    end
    tablecopy(sv, src)
    sv.copyKey = key
    ReloadUI()
end

local function importprompt(t, key)
    SV.SystemAlert["COPY_PROFILE_PROMPT"].text = L["Are you sure you want to copy the profile '" .. key .. "'?"]
    SV.SystemAlert["COPY_PROFILE_PROMPT"].OnAccept = function() importprofile(t, key) end
    SV:StaticPopup_Show("COPY_PROFILE_PROMPT")
end

local function exportprofile(t, key)
    local sv = rawget(t, "profile")
    local dv = rawget(t, "defaults")
    local globals = SV.Global
    if(not globals.profiles[key]) then globals.profiles[key] = {} end
    local saved = globals.profiles[key]
    tablecopy(saved, sv)
    for k,v in pairs(saved) do
        removedefaults(saved[k], dv[k])
    end
    globals.profileKeys[key] = key
    SV:SavedPopup()
end

local function removeprofile(t, key)
    local globals = SV.Global
    if(globals.profiles[key]) then globals.profiles[key] = nil end
    if(globals.profileKeys[key]) then globals.profileKeys[key] = nil end
    collectgarbage("collect")
end

local function insertdata(t, src)
    local sv = rawget(t, "profile")
    local dv = rawget(t, "defaults")
    for k,v in pairs(src) do
        if(not sv[k]) then sv[k] = {} end
        tablecopy(sv[k], src[k])
        if(not dv[k]) then dv[k] = {} end
        tablecopy(dv[k], src[k])

        rawset(t, k, sv[k])
    end
end

local function initializedata(t)
    local sv = rawget(t, "profile")
    if(not sv.copyKey or (sv.copyKey and type(sv.copyKey) ~= "string")) then
        sv.copyKey = pkey
    end
end

local function SanitizeDatabase()
    if(not SV.db) then return end
    local db = SV.db
    local src = SV.Configs
    for k,v in pairs(db) do
        if(src[k]) then
            removedefaults(db[k], src[k])
        end
    end
end

local DataBase_OnEvent = function(self, event)
    if event == "PLAYER_LOGOUT" then
        SanitizeDatabase()
    elseif(event == "ACTIVE_TALENT_GROUP_CHANGED") then
    	SV:UpdateDatabase()
    end
end

local metadatabase = {
    __index = function(t, k)
        if(not k or k == "") then return end
        local sv = rawget(t, "profile")
        local dv = rawget(t, "defaults")
        local src = dv and dv[k]
        if(not sv[k]) then sv[k] = {} end
        if(src) then
            tablecopy(sv[k], src)
        end
        rawset(t, k, sv[k])
        return rawget(t, k)
    end,
}

local METAPROFILE = function(sv)
	local key 		= 1
	if(sv.SAFEDATA.dualSpecEnabled) then
		key = GetSpecialization() or 1
		databaseListener:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
	end

    local db        = setmetatable({}, metadatabase)

    db.profile 	    = sv.STORED[key]
    db.protected    = sv.SAFEDATA
    db.defaults     = SV.Configs
    db.Init         = initializedata
    db.Append       = insertdata
    db.Reset        = resetprofile
    db.SetDefault   = setdefaults
    db.GetDefault   = getdefaults
    db.TalentSwap 	= changespec
    db.ToggleSpec   = togglespec
    db.Import       = importprompt
    db.Export       = exportprofile
    db.Remove       = removeprofile

    return db
end

function SV:AppendDefaults(newIndex, newData)
    self.Configs[newIndex] = newData
end

function SV:GetProfiles()
    return self.Global.profileKeys or {}
end

function SV:CheckProfiles()
    local hasProfile = false
    local list = self.Global.profileKeys or {}
    for key,_ in pairs(list) do
        hasProfile = true
    end
    return hasProfile
end

function SV:UpdateDatabase(partial)
	local sv = _G["SVUI_Profile"]
    twipe(self.db)

    self.db = METAPROFILE(sv)
    self.db:Init()
    self.db.profileKey = pkey

    --print("Update db")
    if(not partial) then
    	Registry:UpdateAll()
    end
end

function SV:ToggleSpecSwap(value)
	if(value) then
		databaseListener:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
		self:UpdateDatabase()
	else
		databaseListener:UnregisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
    end
end

function SV:TableSplice(targetTable, mergeTable)
    if type(targetTable) ~= "table" then targetTable = {} end

    if type(mergeTable) == 'table' then
        for key,val in pairs(mergeTable) do
            if type(val) == "table" then
                val = self:TableSplice(targetTable[key], val)
            end
            targetTable[key] = val
        end
    end
    return targetTable
end

function SV:SetDatabaseObjects(init)
    if(init) then
        self.db = tcopy(self.Configs, true)
    else
        self.Global = self:TableSplice(SVUI_Global, self.Global)
        self.Global.profileKeys = {}
        for k,v in pairs(self.Global.profiles) do
            self.Global.profileKeys[k] = k
        end

        local sv = _G["SVUI_Profile"]

        twipe(self.db)

        --print("Init db")
        self.db = METAPROFILE(sv)
        self.db:Init()
        self.db.profileKey = pkey

        databaseListener:RegisterEvent("PLAYER_LOGOUT")
        databaseListener:SetScript("OnEvent", DataBase_OnEvent)
    end
end