local AddName, Ns = ...
local L = Ns.L

------------------------------
--		Stat Options		--
------------------------------
local Both, Enhanced, Base, Damage, DamageTaken, Percentage, Rating, MainHand, OffHand, World, Realm, Heroism, Valor, Equipped, Percentage = 3, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1
local SameLevel, BossLevel, Regen, Casting, Critical, CritDamage, Total, Max, Average, Auto, Chance, Reduction, Bags, Honor, Kills, Rating = 1, 2, 1, 2, 1, 2, 1, 1, 2, 3, 1, 2, 3, 1, 2, 2


Ns.ConfigWidth = 970
Ns.ConfigHeight = 530 --700
Ns.targetLevel, Ns.ohID = 0, 0

local honorIcon = 301091
local faction = UnitFactionGroup("player")

if faction == "Horde" then honorIcon = 301089 end

------------------------------
--		Display Order		--
------------------------------
Ns.DefaultOrder = {
	{ stat="AP", onupdate=true, events={}, spell=6673, spellclass="Melee", widget={ type="CheckBox" }, options={ Show=true, Decimals_Default=true, }, subgroup=true, activeonload=true, },
	{ stat="APUD", onupdate=true, events={}, spell=17926, spellclass="Melee", widget={ type="CheckBox" }, options={ Show=true, Decimals_Default=true, }, subgroup=true },
	{ stat="RAP", onupdate=true, events={}, spell=19506, spellclass="Ranged", widget={ type="CheckBox" }, options={ Enhanced_Base=true, Decimals_Default=true, }, subgroup=true, activeonload=true, },
	{ stat="RAPUD", onupdate=true, events={}, spell=5502, spellclass="Ranged", widget={ type="CheckBox" }, options={ Show=true, Decimals_Default=true, }, subgroup=true },
	{ stat="DMG", onupdate=true, events={}, spell=12328, spellclass="Melee", widget={ type="CheckBox" }, options={ Main_Off_Auto=true, Display_Average=true, Decimals_Default=true }, subgroup=true },
	{ stat="mDPS", onupdate=true, events={}, spell=8512, spellclass="Melee", widget={ type="CheckBox" }, options={ Main_Off_Auto=true, Display_Average=true, Decimals_Default=true }, subgroup=true },
	{ stat="RDMG", onupdate=true, events={}, spell=19434, spellclass="Ranged", widget={ type="CheckBox" }, options={ Max_Average_Damage=true, Decimals_Default=true, }, subgroup=true },
	{ stat="rDPS", onupdate=true, events={}, spell=2643, spellclass="Ranged", widget={ type="CheckBox" }, options={ Max_Average_Damage=true, Decimals_Default=true, }, subgroup=true },
	{ stat="Fire", onupdate=true, events={}, spell=11366, spellclass="Spell", widget={ type="CheckBox" }, options={ Enhanced_Base=true, Decimals_Default=true, }, subgroup=true, activeonload=true, },
	{ stat="Frost", onupdate=true, events={}, spell=10181, spellclass="Spell", widget={ type="CheckBox" }, options={ Enhanced_Base=true, Decimals_Default=true, }, subgroup=true },
	{ stat="Arcane", onupdate=true, events={}, spell=5143, spellclass="Spell", widget={ type="CheckBox" }, options={ Enhanced_Base=true, Decimals_Default=true, }, subgroup=true },
	{ stat="Shadow", onupdate=true, events={}, spell=686, spellclass="Spell", widget={ type="CheckBox" }, options={ Enhanced_Base=true, Decimals_Default=true, }, subgroup=true },
	{ stat="Nature", onupdate=true, events={}, spell=16901, spellclass="Spell", widget={ type="CheckBox" }, options={ Enhanced_Base=true, Decimals_Default=true, }, subgroup=true },
	{ stat="Holy", onupdate=true, events={}, spell=7294, spellclass="Spell", widget={ type="CheckBox" }, options={ Enhanced_Base=true, Decimals_Default=true, }, subgroup=true },
	{ stat="Healing", onupdate=true, events={}, spell=139, spellclass="Spell", widget={ type="CheckBox" }, options={ Enhanced_Base=true, Decimals_Default=true, }, subgroup=true },
	{ stat="SpellUD", onupdate=true, events={}, spell=17959, spellclass="Spell", widget={ type="CheckBox" }, options={ Show=true, Decimals_Default=true, }, subgroup=true },
	{ stat="Crit", onupdate=true, events={}, spell=1719, spellclass="Melee", widget={ type="CheckBox" }, options={ WithBar=true, Level_Same_Boss=true, Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="CritCap", onupdate=true, events={}, spell=30920, spellclass="Melee", widget={ type="CheckBox" }, options={ WithBar="CritCap", Decimals_Two=true, }, subgroup=true },
	{ stat="RangedCrit", onupdate=true, events={}, spell=24395, spellclass="Ranged", widget={ type="CheckBox" }, options={ WithBar=true, Level_Same_Boss=true, Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="SpellCrit", onupdate=true, events={}, spell=11115, spellclass="Spell", widget={ type="CheckBox" }, options={ WithBar=true, Level_Same_Boss=true, Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="Hit", onupdate=true, events={}, spell=14294, spellclass="Melee", widget={ type="CheckBox" }, options={ WithBar=true, Enhanced_Base=true, Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="RangedHit", onupdate=true, events={}, spell=1130, spellclass="Ranged", widget={ type="CheckBox" }, options={ WithBar=true, Enhanced_Base=true, Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="SpellHit", onupdate=true, events={}, spell=11160, spellclass="Spell", widget={ type="CheckBox" }, options={ WithBar=true, Enhanced_Base=true, Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="HasteMelee", onupdate=true, events={}, spell=6774, spellclass="Melee", widget={ type="CheckBox" }, options={ WithBar=true, Enhanced_Base=true, Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="weaponSpeed", onupdate=true, events={}, spell=12974, spellclass="Melee", widget={ type="CheckBox" }, options={ Main_Off_Auto=true, Decimals_Two=true, }, subgroup=true },
	{ stat="HasteRanged", onupdate=true, events={}, spell=6150, spellclass="Ranged", widget={ type="CheckBox" }, options={ WithBar=true, Enhanced_Base=true, Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="rangedSpeed", onupdate=true, events={}, spell=3045, spellclass="Ranged", widget={ type="CheckBox" }, options={ Show=true, Decimals_Two=true, }, subgroup=true },
	{ stat="HasteCaster", onupdate=true, events={}, spell=12042, spellclass="Spell", widget={ type="CheckBox" }, options={ WithBar=true, Enhanced_Base=true, Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="MeleeMiss", onupdate=true, events={}, spell=13807, spellclass="Melee", widget={ type="CheckBox" }, options={ WithBar="Miss", Level_Same_Boss=true, Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="RangedMiss", onupdate=true, events={}, spell=19151, spellclass="Ranged", widget={ type="CheckBox" }, options={ WithBar="Miss", Level_Same_Boss=true, Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="SpellMiss", onupdate=true, events={}, spell=15060, spellclass="Spell", widget={ type="CheckBox" }, options={ WithBar="Miss", Level_Same_Boss=true, Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="ManaRegen", onupdate=true, events={}, spell=724, spellclass="Spell", widget={ type="CheckBox" }, options={ Regen_Normal_Casting=true, Decimals_Two=true, }, subgroup=true },
	{ stat="MP5", onupdate=true, events={}, spell=19742, spellclass="Spell", widget={ type="CheckBox" }, options={ Regen_Normal_Casting=true, Decimals_Two=true, }, subgroup=true },
	{ stat="Mitigation", onupdate=true, events={}, spell=16301, spellclass="Defense", widget={ type="CheckBox" }, options={ Enhanced_Base=true, Cap_Percent=true, Decimals_Two=true, }, subgroup=true, activeonload=true, },
	{ stat="Defense", onupdate=true, events={}, spell=12753, spellclass="Defense", widget={ type="CheckBox" }, options={ Enhanced_Base=true, Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="Armor", onupdate=true, events={}, spell=12962, spellclass="Defense", widget={ type="CheckBox" }, options={ Enhanced_Base=true, Decimals_Default=true, }, subgroup=true },
	{ stat="Dodge", onupdate=true, events={}, spell=24297, spellclass="Defense", widget={ type="CheckBox" }, options={ Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="Parry", onupdate=true, events={}, spell=3127, spellclass="Defense", widget={ type="CheckBox" }, options={ Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="Block", onupdate=true, events={}, spell=2565, spellclass="Defense", widget={ type="CheckBox" }, options={ Cap_Percent=true, Decimals_Two=true, }, subgroup=true },
	{ stat="FireResist", events={ UNIT_RESISTANCES={ "player", }, }, spell=18459, spellclass="Resistance", widget={ type="CheckBox" }, options={ Show=true, }, subgroup=true, activeonload=true, },
	{ stat="FrostResist", events={ UNIT_RESISTANCES={ "player", }, }, spell=10161, spellclass="Resistance", widget={ type="CheckBox" }, options={ Show=true, }, subgroup=true },
	{ stat="ShadowResist", events={ UNIT_RESISTANCES={ "player", }, }, spell=10958, spellclass="Resistance", widget={ type="CheckBox" }, options={ Show=true, }, subgroup=true },
	{ stat="ArcaneResist", events={ UNIT_RESISTANCES={ "player", }, }, spell=1449, spellclass="Resistance", widget={ type="CheckBox" }, options={ Show=true, }, subgroup=true },
	{ stat="NatureResist", events={ UNIT_RESISTANCES={ "player", }, }, spell=9858, spellclass="Resistance", widget={ type="CheckBox" }, options={ Show=true, }, subgroup=true },
	{ stat="Speed", onupdate=true, events={}, updatespeed=1, spell=2983, spellclass="Misc", widget={ type="CheckBox" }, options={ WithBar="Speed", Decimals_Two=true, }, subgroup=true, activeonload=true, },
	{ stat="TargetSpeed", onupdate=true, events={}, spell=23218, spellclass="Misc", widget={ type="CheckBox" }, options={ Show=true, Decimals_Two=true, }, subgroup=true },
	{ stat="Threat", onupdate=true, events={}, spell=355, spellclass="Misc", widget={ type="CheckBox" }, options={ WithBar=true, Percent_Rating=true, }, subgroup=true },
	{ stat="Durability", events={ UPDATE_INVENTORY_DURABILITY=true, }, spell=3100, spellclass="Misc", widget={ type="CheckBox" }, options={ WithBar="Dura", Show=true, AutoColorOff=true }, subgroup=true },
	{ stat="RepairCost", events={ UPDATE_INVENTORY_DURABILITY=true, }, spell=20593, spellclass="Misc", widget={ type="CheckBox" }, options={ Total_Equipped_Bags=true, }, subgroup=true },
	{ stat="Honor", onupdate=true, events={}, spell=honorIcon, spellclass="Misc", widget={ type="CheckBox" }, options={ Honor_Kills=true, }, subgroup=true },
	{ stat="BuffCounter", onupdate=true, events={}, spell=22888, spellclass="Misc", widget={ type="CheckBox" }, options={ WithBar="Buff", }, subgroup=true },
	{ stat="DebuffCounter", onupdate=true, events={}, spell=23154, spellclass="Misc", widget={ type="CheckBox" }, options={ Show=true, }, subgroup=true },
	{ stat="Lag", onupdate=true, events={}, spell=1265, spellclass="Misc", widget={ type="CheckBox" }, options={ World_Realm=true, }, subgroup=true },
	{ stat="FPS", onupdate=true, events={}, spell=19498, spellclass="Misc", widget={ type="CheckBox" }, options={ Show=true, }, subgroup=true },
	{ stat="Money", events={ PLAYER_MONEY=true, }, item=5061, spellclass="Misc", widget={ type="CheckBox" }, options={ Show=true, }, subgroup=true },
	{ stat="Guild", events={ GUILD_ROSTER_UPDATE=true, }, item=23705, spellclass="Misc", widget={ type="CheckBox" }, options={ Show=true, Display_Total=true }, subgroup=true },
	{ stat="Friends", events={ FRIENDLIST_UPDATE=true, }, item=22260, spellclass="Misc", widget={ type="CheckBox" }, options={ Show=true, }, subgroup=true },
}

------------------------------
--		Global Events		--
------------------------------
Ns.GlobalEvents = {
	UNIT_AURA={ "player", "target", },
	UNIT_INVENTORY_CHANGED={ "player", },
	UNIT_STATS={ "player", },
	UNIT_DEFENSE={ "player", },
	BAG_CLOSED=true,
	BIND_ENCHANT=true,
	CHARACTER_POINTS_CHANGED=true,
	PLAYER_LEVEL_UP=true,
	REPLACE_ENCHANT=true,
	PLAYER_TARGET_CHANGED=true,
}

------------------------------
--		Settings Groups		--
------------------------------
Ns.SpellClass = {
	[0] = { stat="Settings", grp=Ns.SettingsGroupOrder, order=Ns.SettingsDisplayOrder, icon="Settings", },
	[1] = { stat="Text", grp=Ns.TextGroupOrder, order=Ns.TextDisplayOrder, icon="Fonts", },
	[2] = { stat="Layout", grp=Ns.LayoutGroupOrder, order=Ns.LayoutDisplayOrder, icon="Layout", },
	[3] = { stat="Events", grp=Ns.EventsGroupOrder, order=Ns.EventsDisplayOrder, icon="Events", },
	[4] = { stat="Profiles", grp=Ns.ProfileGroupOrder, order=Ns.ProfileDisplayOrder, icon="Profiles", },
	[5] = { stat="", },
	[6] = { stat="Search", icon="Search", },
	[7] = { stat="Melee", icon="Physical", },
	[8] = { stat="Defense", icon="Defense", },
	[9] = { stat="Ranged", icon="Ranged", },
	[10] = { stat="Spell", icon="Spell", },
	[11] = { stat="Resistance", icon="Resistance", },
	[12] = { stat="Misc", icon="Miscs", },
	[13] = { stat="", },
	[14] = { stat="FAQ", icon="Misc", },
}

------------------------------
--		Stat Options		--
------------------------------
Ns.Options = {
	{ stat="Show", widget={ type="CheckBox", } },
	{ stat="CustName", widget={ type="EditBox", width=100, height=20, numeric=false, }, tooltip=true, },
	{ stat="Enhanced_Base", spellclass="Enhanced_Base", widget={ type="Radio", }, tooltip=true, default=1, labels={ "Enhanced", "Basic", "Both"}, newline=true },
	{ stat="Percent_Rating", spellclass="Percent_Rating", widget={ type="Radio", }, tooltip=true, default=1, labels={ "Percentage", "Rating", "Both"}, newline=true },
	{ stat="Level_Same_Boss", spellclass="Level_Same_Boss", widget={ type="Radio", }, tooltip=true, default=1, labels={ "SameLevel", "BossLevel", "Both"}, newline=true },
	{ stat="Regen_Normal_Casting", spellclass="Regen_Normal_Casting", widget={ type="Radio", }, tooltip=true, default=1, labels={ "OutOfCombat", "Casting", "Both"}, newline=true },
	{ stat="Max_Average_Damage", spellclass="Max_Average_Damage", widget={ type="Radio", }, tooltip=true, default=1, labels={ "Max", "Average", "Both"}, newline=true },
	{ stat="Main_Off_Auto", spellclass="Main_Off_Auto", widget={ type="Radio", }, tooltip=true, default=1, labels={ "MainHand", "OffHand", "Auto"}, newline=true },
	{ stat="Honor_Kills", spellclass="Honor_Kills", widget={ type="Radio", }, tooltip=true, default=1, labels={ "Honor", "Kills", "Both"}, newline=true },
	{ stat="Display_Average", spellclass="Display_Average", widget={ type="CheckBox", }, tooltip=true, default=false, labels={ "Display_Average"}, },
	{ stat="Display_Basic", spellclass="Display_Basic", widget={ type="CheckBox", }, tooltip=true, default=false, labels={ "Display_Basic"}, newline=true },
	{ stat="World_Realm", spellclass="World_Realm", widget={ type="Radio", }, tooltip=true, default=1, labels={ "World", "Realm", "Both"}, newline=true },
	{ stat="Total_Equipped_Bags", spellclass="Total_Equipped_Bags", widget={ type="Radio", }, tooltip=true, default=1, labels={ "Total", "Equipped", "Bags"}, newline=true },
	{ stat="Display_MainHand", spellclass="Display_MainHand", widget={ type="CheckBox", }, tooltip=true, default=false, labels={ "Display_MainHand"}, newline=true },
	{ stat="AutoColorOff", spellclass="AutoColorOff", widget={ type="CheckBox", }, tooltip=true, default=false, labels={ "AutoColorOff"}, newline=true, },
	{ stat="Cap_Percent", spellclass="Cap_Percent", default=100, tooltip=true, widget={ type="EditBox", width=60, height=20, numeric=true, format="%.2f%%", }, },
	{ stat="Display_Total", spellclass="Display_Total", widget={ type="CheckBox", }, default=false, labels={ "Display_Total"}, newline=true, },
	{ stat="Decimals_Default", spellclass="Decimals_Default", default=0, widget={ type="Slider", min=0, max=3, valuestep=1, width=140, } , newline=true, },
	{ stat="Decimals_Two", spellclass="Decimals_Two", default=2, widget={ type="Slider", min=0, max=3, valuestep=1, width=140, } , newline=true, },
	{ stat="ColorCustom", default=false, widget={ type="CheckBox", }, linked={ "ColorName", false, "ColorClass", false, "ColorValue", false, "ColorValueEndable", false, }, onclick="InitCustomColors", newline=true },
	{ stat="ColorName", default={ r=1, g=1, b=1, }, widget={ type="ColorPicker", }, },
	{ stat="ColorClass", default=false, widget={ type="CheckBox", }, },
	{ stat="ColorValueEndable", default=false, widget={ type="CheckBox", }, linked={ "ColorValue", false, }, newline=true },
	{ stat="ColorValue", default={ r=1, g=1, b=1, }, widget={ type="ColorPicker", }, },
	{ stat="ShowBar", spellclass="ShowBar", widget={ type="CheckBox", }, tooltip=true, default=false, labels={ "ShowBar"}, baropt=true, newline=true,},
	{ stat="BarMaxMode", spellclass="BarMaxMode", widget={ type="DropList", data="BarMaxMode", width=120, }, default=1, onchange="OnBarMaxModeChange", baropt=true, barmode="Default", },
	{ stat="SpeedBar", spellclass="SpeedBar", widget={ type="DropList", data="SpeedBar", width=120, }, default=1, baropt=true, barmode="Speed", },
	{ stat="BarMaxCustom", spellclass="BarMaxCustom", default=100, tooltip=true, widget={ type="EditBox", width=60, height=20, numeric=true, format="%.0f", }, baropt=true, barmode="Default,Speed", },
	{ stat="StaticBar", spellclass="StaticBar", widget={ type="CheckBox", }, tooltip=true, default=false, labels={ "StaticBar"}, baropt=true, staticopt=true, newline=true, },
	{ stat="BarTexture", spellclass="BarTexture", widget={ type="DropList", data="BarTextures", width=170, }, default="Blizzard", baropt=true, staticopt=true, newline=true, },
	{ stat="BarColor", spellclass="BarColor", default={ r=0.2, g=0.6, b=0.2, }, widget={ type="ColorPicker", }, baropt=true, staticopt=true, },
	{ stat="BarAutoColor", spellclass="BarAutoColor", widget={ type="CheckBox", }, tooltip=true, default=false, labels={ "BarAutoColor"}, baropt=true, linked={ "BarColor", true, }, },
	{ stat="BarAlpha", spellclass="BarAlpha", default=1, widget={ type="Slider", min=0, max=1, valuestep=0.01, width=145, }, baropt=true, staticopt=true, },
	{ stat="BarBgColor", spellclass="BarBgColor", default={ r=0.1, g=0.1, b=0.1, }, widget={ type="ColorPicker", }, baropt=true, },
	{ stat="BarBgAlpha", spellclass="BarBgAlpha", default=1, widget={ type="Slider", min=0, max=1, valuestep=0.01, width=145, }, baropt=true, },
}

--------------------------
--		Class & ID		--
--------------------------
Ns.byPlayerClass = {
	["DRUID"] = 11,
	["HUNTER"] = 3,
	["MAGE"] = 8,
	["PALADIN"] = 2,
	["PRIEST"] = 5,
	["ROGUE"] = 4,
	["SHAMAN"] = 7,
	["WARLOCK"] = 9,
	["WARRIOR"] = 1,
}

------------------------------
--		Class defaults		--
------------------------------
function Ns:GetClassDefaults(class)
	local defaults = {
		DRUID = {
			AP = true,
			Nature = true,
			Healing = true,
			MP5 = true,
			Crit = true,
			SpellCrit = true,
			SpellHit = true,
			SpellMiss = true,
			Mitigation = true,
			Speed = true,
			Durability = true,
		},
		HUNTER = {
			RAP = true,
			RDMG = true,
			rDPS = true,
			RangedCrit = true,
			RangedHit = true,
			HasteRanged = true,
			RangedMiss = true,
			RangedPen = true,
			Speed = true,
			Durability = true,
		},
		MAGE = {
			Frost = true,
			Fire = true,
			SpellCrit = true,
			SpellHit = true,
			SpellMiss = true,
			HasteCaster = true,
			ManaRegen = true,
			Speed = true,
			Durability = true,
		},
		PALADIN = {
			AP = true,
			Holy = true,
			Healing = true,
			MP5 = true,
			Crit = true,
			HasteMelee = true,
			Armor = true,
			SpellCrit = true,
			Mitigation = true,
			Speed = true,
			Durability = true,
		},
		PRIEST = {
			Healing = true,
			Shadow = true,
			SpellCrit = true,
			SpellHit = true,
			SpellMiss = true,
			HasteCaster = true,
			ManaRegen = true,
			Speed = true,
			Durability = true,
		},
		ROGUE = {
			AP = true,
			DMG = true,
			mDPS = true,
			weaponSpeed = true,
			Crit = true,
			CritCap = true,
			Hit = true,
			HasteMelee = true,
			Speed = true,
			Durability = true,
		},
		SHAMAN = {
			AP = true,
			Nature = true,
			Healing = true,
			MP5 = true,
			Crit = true,
			SpellCrit = true,
			SpellHit = true,
			SpellMiss = true,
			Speed = true,
			Durability = true,
		},
		WARLOCK = {
			Shadow = true,
			SpellCrit = true,
			SpellHit = true,
			SpellMiss = true,
			HasteCaster = true,
			ManaRegen = true,
			Speed = true,
			Durability = true,
		},
		WARRIOR = {
			AP = true,
			DMG = true,
			mDPS = true,
			Mitigation = true,
			Crit = true,
			CritCap = true,
			Hit = true,
			HasteMelee = true,
			Defense = true,
			Speed = true,
			Durability = true,
		},
	}
	return class and defaults[strupper(class)] or defaults
end
