local AddName, Ns = ...
local L = Ns.L
local HOptionsOffsetX, HOptionsOffsetY, mainMenuYOffset, separatorOffsetY = 7, 3, -82, 30
local sidePanelWidth
local redColor = { r=1, g=0, b=0.658 }
local orangeColor = { r=1, g=0.741, b=0.313 }
local white = { r=1, g=1, b=1 }
--Ns.ConfigDefaultFont = "Fonts/ARIALN.ttf"

-- Moved back to Widgets.lua
--Ns.ConfigDefaultFont = "Interface\\AddOns\\"..AddName.."\\fonts\\GoogleSans-Medium.ttf"
--Ns.ConfigDefaultFont = Ns.LSM:Fetch("font", "GSans")
local addVer = C_AddOns.GetAddOnMetadata(AddName, "Version")

if GetLocale() == 'zhCN' or GetLocale() == 'zhTW' or GetLocale() == 'koKR' then
    Ns.ConfigDefaultFont = Ns.LSM:Fetch("font", Ns.LSM:GetDefault("font"))
end
Ns.ConfigDefaultFontSize = 16
Ns.TooltipYOffset = 3
-----------------------------------------------
--			SV Items and Groupings			---
-----------------------------------------------
Ns.SettingsGroupOrder = {
	[1] = { stat="HUD", svnotintable=true, icon="HUD", widget={ type="TopTab" }, activeonload=true, },
	[2] = { stat="Fonts", svnotintable=true, centeralign=true, icon="Fonts", widget={ type="TopTab" }, },
	[3] = { stat="Text", svnotintable=true, icon="Spacing", widget={ type="TopTab" }, },
	[4] = { stat="MinimapT", svnotintable=true, icon="MinimapIcon", widget={ type="TopTab" }, },
	[5] = { stat="FAQ", svnotintable=true, icon="Misc", widget={ type="TopTab" }, },
}

Ns.LayoutGroupOrder = {
	[1] = { stat="DisplayOrder", description=true, svnotintable=true, icon="Layout", widget={ type="TopTab" }, enabledval=3, activeonload=true, },
}

Ns.LayoutDisplayOrder = { ignoretoptabs=true, 
	{ stat="StatOrderSettings", icon="", svnotintable=true, spellclass="DisplayOrder", widget={ type="DisplayOrderConfig", data="Profiles", width=200}, },
}

Ns.EventsGroupOrder = {
	[1] = { stat="Events", svnotintable=true, icon="Events", widget={ type="TopTab" }, activeonload=true, },
}

Ns.EventsDisplayOrder = { ignoretoptabs=true, 
	{ stat="EventEnable", spellclass="Events", icon="", default=false, widget={ type="CheckBox", }, tooltip=true, },
	{ stat="EventWorld", spellclass="Events", icon="", default=false, widget={ type="CheckBox", }, newline=true, },
	{ stat="EventDungeon", spellclass="Events", icon="", default=false, widget={ type="CheckBox", }, },
	{ stat="EventRaid", spellclass="Events", icon="", default=false, widget={ type="CheckBox", }, },
	{ stat="EventPvP", spellclass="Events", icon="", default=false, widget={ type="CheckBox", }, newline=true, },
	{ stat="EventArena", spellclass="Events", icon="", default=false, widget={ type="CheckBox", }, },
	{ stat="EventCombat", spellclass="Events", icon="", default=false, widget={ type="CheckBox", }, },
}

Ns.ProfileGroupOrder = {
	[1] = { stat="Profiles", svnotintable=true, icon="Profiles", widget={ type="TopTab" }, activeonload=true, },
}

Ns.ProfileDisplayOrder = { ignoretoptabs=true, 
	{ stat="Profiles", icon="", svnotintable=true, spellclass="Profiles", widget={ type="ProfileConfig", data="Profiles", width=200, }, },
}

Ns.SettingsDisplayOrder = {
	{ stat="HideHUD", icon="", spellclass="HUD", default=false, widget={ type="CheckBox", }, },
	{ stat="LockHUD", icon="", spellclass="HUD", default=false, widget={ type="CheckBox", }, },
	{ stat="PanelDisplay", icon="", spellclass="HUD", default=false, widget={ type="CheckBox", }, },
	{ stat="HUDStrata", icon="", spellclass="HUD", default="MEDIUM", widget={ type="DropList", data="Strata", width=120, }, newline=true, },
	{ stat="HUDBgColor", icon="", spellclass="HUD", default={ r=0.1, g=0.1, b=0.1, }, widget={ type="ColorPicker", }, },
	{ stat="HUDBgAlpha", icon="", spellclass="HUD", default=0, widget={ type="Slider", min=0, max=1, valuestep=0.01, width=120, }, },
	{ stat="ResetPosition", icon="", spellclass="HUD", default=false, widget={ type="PlainButton", }, },
	{ stat="StatFont", icon="", spellclass="Fonts", default="Arial Narrow",  widget={ type="DropList", data="Fonts", width=170, }, },
	{ stat="StatFontSize", icon="", spellclass="Fonts", default=16, widget={ type="Slider", min=8, max=45, valuestep=1, width=140, }, },
	{ stat="StatFontFlags", icon="", spellclass="Fonts", default="", widget={ type="DropList", data="FontFlags", width=140, }, }, -- no table="Stat" entry uses the Ns.DropListTables[stat] table  -- ["OutlineTable"] is in a table of tables in Config.
	{ stat="StatIcons", icon="", spellclass="HUD", default=true, newline=true, widget={ type="CheckBox", }, subgroup={ text="Display", icon="Display", }, },
	{ stat="StatTextCaps", spellclass="HUD", icon="", default=false, widget={ type="CheckBox", }, },
	{ stat="TextSeparator", icon="", spellclass="HUD", default=":", widget={ type="DropList", data="TextDataSeparator", width=90, },  },
	{ stat="StatTextColor", icon="", spellclass="HUD", default={ r=1, g=1, b=1, }, widget={ type="ColorPicker", }, newline=true, },
	{ stat="ClassColors", spellclass="HUD", icon="", default=false, widget={ type="CheckBox", }, },
	{ stat="SplitColors", spellclass="HUD", icon="", default=false, widget={ type="CheckBox", }, tooltip=true, },
	{ stat="DataColor", icon="", spellclass="HUD", default={ r=1, g=1, b=1, }, widget={ type="ColorPicker", }, },
	{ stat="StatWidth", icon="", spellclass="Text", default=150, widget={ type="Slider", min=70, max=300, valuestep=0.01, width=140, }, tooltip=true, },
	{ stat="StatSpacingH", icon="", spellclass="Text", default=1, widget={ type="Slider", min=1, max=80, width=140, }, },
	{ stat="StatSpacingV", icon="", spellclass="Text", default=0, widget={ type="Slider", min=0, max=80, valuestep=0.01, width=140, }, },
	{ stat="StatAlignment", icon="", spellclass="Text", default="TOPLEFT", widget={ type="DropList", data="Alignments", width=90, }, newline=true, },
	{ stat="IconAlignment", icon="", spellclass="Text", default="TOPLEFT", widget={ type="DropList", data="IconAlignments", width=90, },  },
	{ stat="Minimap", icon="", spellclass="MinimapT", default={ Show=true, }, widget={ type="MinimapButton", }, subgroup={ text="MinimapGroupText", icon="MinimapIcon", }, },
	{ stat="CompButton", spellclass="MinimapT", icon="", default=true, widget={ type="CheckBox", }, },
	{ stat="FAQ", icon="", svnotintable=true, spellclass="FAQ", widget={ type="FAQConfig", data="Profiles", width=200, }, },
}

--------------------------------------
--			Droplist tables			--
--------------------------------------
function Ns.CopyTable(src, dest)
	for index, value in pairs(src) do
		if type(value) == "table" then
			dest[index] = {}
			Ns.CopyTable(value, dest[index])
		else
			dest[index] = value
		end
	end
end

function Ns:CheckForStats(currentprofile, class)
	local statSettings = Ns:GetClassDefaults(class)
	for k, v in pairs(Ns.DefaultOrder) do
		if not currentprofile.Stats[v.stat] then
			currentprofile.Stats[v.stat] = {}
			currentprofile.Stats[v.stat].Show = statSettings[v.stat] or false
			if v.options then
				for o,_ in pairs(v.options) do
					for _, so in pairs(Ns.Options) do
						if so.stat == o and so.default then
							currentprofile.Stats[v.stat][so.stat] = so.default
							break
						end
					end
				end
			end
		end
	end
end

function Ns:CreateProfile(newprofile, currentprofile, class) -- copies settings from the current to the new one
	if not currentprofile then
		if not class then
			print("Sinstats: No profile to copy from")
			return
		end
		currentprofile = {}
		for _, options in pairs(Ns.SettingsDisplayOrder) do
			currentprofile[options.stat] = options.default
		end
		currentprofile.Stats = {}
		Ns:CheckForStats(currentprofile, class)
	end
	SinStatsDB.profiles[newprofile] = {}
	Ns.CopyTable(currentprofile, SinStatsDB.profiles[newprofile])
end

------------------------------------------
--			Design Functions			--
------------------------------------------
local function DisplayHorizontal(self, anchorframe, align, astab, noicons, ignoretoptabs)--, displayOrder, key, matchwith, noicons)
	local ChildAnchor
	anchorframe.HasScroll = nil
	if not astab then
		ChildAnchor = Ns:GetWidget("AnchorScroll" , anchorframe, 99999)
		anchorframe.HasScroll = ChildAnchor
		ChildAnchor.MyInformation = ChildAnchor.MyInformation or {}
		ChildAnchor:SetPoint("TOPLEFT", anchorframe.Description, 0, -30)
		ChildAnchor:SetPoint("BOTTOMRIGHT", Ns.ConfigFrame, -50, 10)
	else
		ChildAnchor = anchorframe
	end
	local largestWidgetWidth, largestWidgetHeight, sepOffset, tooltipOffset = 0, 0, 0
	widget = widget and strtrim(widget) or nil
	local tipped = {}
	for k, v in ipairs(self.DisplayOrder) do
		if not v.spellclass or v.spellclass == self.MyInformation.stat or (self.MyInformation.options and self.MyInformation.options[v.spellclass]) then
			local f = Ns:GetWidget(astab and "TopTab" or v.widget.type, ChildAnchor.ScrollChild and ChildAnchor.ScrollChild or anchorframe, k)
			anchorframe:AddWidget(f, ChildAnchor.ScrollChild and ChildAnchor.ScrollChild or anchorframe)
			f.ActiveOnInit = astab and v.activeonload -- must come before f:Init
			f:Init(anchorframe, self, noicons)
			local w = f:GetWidth()
			local h = f:GetHeight()
			if f.Tip then
				local tipW = f.Tip:GetWidth() + 20
				if tipW > largestWidgetWidth then
					largestWidgetWidth = tipW
				end
				tipped[f] = true
				if not tooltipOffset then
					tooltipOffset = f.Tip:GetHeight() + Ns.TooltipYOffset
				end
			end
			if w > largestWidgetWidth then
				largestWidgetWidth = w
			end
			if h > largestWidgetHeight then
				largestWidgetHeight = h
			end
			if v.tooltip then
				f.Tooltip = Ns:GetWidget("Tooltip", f)
				f.Tooltip:Init(f, v)
			end
		end
	end
	largestWidgetHeight = ceil(largestWidgetHeight)
	largestWidgetWidth = ceil(largestWidgetWidth)
 	local maxItemsPerRow = floor(Ns.maxOptionsWidth / (largestWidgetWidth + HOptionsOffsetX))
 	local rows, modOffset = 0, 1
 	local firstOnRow = ChildAnchor.ScrollChild and ChildAnchor.ScrollChild or anchorframe
 	local lastAnchor = firstOnRow
 	local cols = min(maxItemsPerRow, #anchorframe.CurrentWidgets)
 	local pageAlignment = anchorframe.CenterAlign and "TOP" or "TOPLEFT"
 	--local headerHeight = anchorframe.Description:GetHeight() + 24 -- y offset between header and top tabs
 	local headerHeight = 14 -- y offset between header and top tabs
 	local SubGroupOs, SubGroupLabel
	local visibleRowCount = 0
	local addTip
	if ignoretoptabs then
		largestWidgetHeight = 0
	else
		for k, v in ipairs(anchorframe.CurrentWidgets) do
			if astab then
				v:SetWidth(largestWidgetWidth)
			end
			v:ClearAllPoints()
			local modVal = (k-modOffset) % maxItemsPerRow
			if k == 1 or v.MyInformation.newline or modVal == 0 then
				addTip = 0
				for i=k, k + maxItemsPerRow do
					if anchorframe.CurrentWidgets[i] and anchorframe.CurrentWidgets[i].newline then
						break
					end
					if tipped[anchorframe.CurrentWidgets[i]] then
						addTip = tooltipOffset
						break
					end
				end
				local yos = addTip
				if k == 1 then
					yos = (HOptionsOffsetY * 2) + (headerHeight + 2) + anchorframe.Label:GetHeight()
				else
					if v.MyInformation.newline then
						modOffset = modOffset + modVal
					end
				end
				if v.Tip then
					yos = yos + 17  -- CHANGES THE Y OFFSET OF 1ST ROW AND 2ND ROW OPTIONS - TOGETHER
				end
			
				rows = rows + 1
				yos = k == 1 and -yos or -(yos + largestWidgetHeight + HOptionsOffsetY)
				if v.MyInformation.newline and v.MyInformation.subgroup then
					SubGroupOs = (rows + 2) * largestWidgetHeight + separatorOffsetY + 20 -- 2nd breakline offset
					local text = v.MyInformation.subgroup.text and v.MyInformation.subgroup.text or v.MyInformation.subgroup.stat
					SubGroupLabel = format(Ns.WidgetDisplayFormat, Ns:GetSpellIcon(v.MyInformation.subgroup), L[text])
					--local text = v.MyInformation.subgroup.text and v.MyInformation.subgroup.text or v.MyInformation.subgroup.stat
					--local iconSource = v.MyInformation.subgroup.icon and v.MyInformation.subgroup or v
					local multi = visibleRowCount == 1 and 32 or 25
					yos = yos - ((HOptionsOffsetY - 0.5) * multi) -- 2nd row options offset
				end
				v:SetPoint("TOPLEFT", firstOnRow, "TOPLEFT", 0, yos)
				if k ~= 1 then
					local p, r, t, x, y = firstOnRow:GetPoint(1)
					if y then
						sepOffset = sepOffset + firstOnRow:GetHeight() + y + yos
					end
					visibleRowCount = visibleRowCount + 1
				end
				firstOnRow = v
				--savYos = yos
			else
				v:SetPoint(pageAlignment, lastAnchor, largestWidgetWidth + HOptionsOffsetX, 0)
			end
			lastAnchor = v
			if v.SetLink then
				v:SetLink(anchorframe, self)
			end
			v:Show() -- required because widgets are hidden when they are "killed"
		end
	end
	if visibleRowCount == 2 then
		sepOffset = sepOffset + -(anchorframe.CurrentWidgets[1]:GetHeight() + (HOptionsOffsetY * 2))
	end
	local width
	if SubGroupLabel or not astab then
		anchorframe.TopSeparator = Ns:GetWidget("Separator", anchorframe)
		anchorframe.TopSeparator:SetPoint("CENTER", anchorframe, "TOP", 0, -28)
		anchorframe.TopSeparator:Show()
		SubGroupOs = SubGroupOs or (rows + 1) * (largestWidgetHeight + separatorOffsetY)
		if SubGroupLabel then -- Adds bottom separator Minimap/Alignment
			anchorframe.BottomSeparator = Ns:GetWidget("Separator", anchorframe)

			if not astab then
				anchorframe.BottomSeparator:SetParent(ChildAnchor.ScrollChild)
			end
			anchorframe.BottomSeparator:Init()
			anchorframe.BottomSeparator:SetTextColor(1,1,1)
			anchorframe.BottomSeparator:SetLabel(SubGroupLabel)
			anchorframe.BottomSeparator:SetFont(Ns.ConfigDefaultFont2, Ns.ConfigDefaultFontSize - 2) -- font size
			if not astab then
				anchorframe.BottomSeparator:SetPoint("CENTER", ChildAnchor.ScrollChild, "TOP", 0, -SubGroupOs)
			else
				anchorframe.BottomSeparator:SetPoint("CENTER", anchorframe, "TOP", 0, -SubGroupOs)
			end
			anchorframe.BottomSeparator:Show()
		else -- Adds bottom separator with description text
			anchorframe.BottomSeparator = Ns:GetWidget("Separator", anchorframe)
			anchorframe.BottomSeparator:Hide()
			if not astab then
				anchorframe.BottomSeparator:SetParent(ChildAnchor.ScrollChild)
			end
			anchorframe.BottomSeparator:Init(true)
			anchorframe.BottomSeparator:ClearAllPoints()
			anchorframe.BottomSeparator:SetPoint("CENTER", ChildAnchor.ScrollChild, "TOP", 0, sepOffset + -10)-- , "BOTTOM", 23, -SubGroupOs + 30)
			if self.MyInformation.options or self.MyInformation.description then
				anchorframe.BottomSeparator:AnchorLabel("TOPLEFT", 5, -15, true)
				local text = string.trim(L[self.MyInformation.stat.."Description"])
				if text ~= "" then
					anchorframe.BottomSeparator:SetLabel(text)
					anchorframe.BottomSeparator:Show()
				end
			end
			anchorframe.BottomSeparator:SetTextColor(1,1,1) -- description text for stats
			anchorframe.BottomSeparator:SetFont(Ns.ConfigDefaultFont, Ns.ConfigDefaultFontSize - 4) -- font size
			anchorframe.BottomSeparator:EnableToolip(true)
			
		end

		width = Ns.SeparatorWidth
	else
		width = cols * (largestWidgetWidth + HOptionsOffsetX) - (rows == 1 and HOptionsOffsetX or 0)
	end
	local height
	if ignoretoptabs then
		height = HOptionsOffsetY + 20
	else
		height = rows * (largestWidgetHeight + HOptionsOffsetY) + HOptionsOffsetY + headerHeight + anchorframe.Label:GetHeight() + 20
	end
	anchorframe:SetAnchor(align, width, height)-- + headerHeight)
	if not astab then
		anchorframe:AddWidget(ChildAnchor)
		ChildAnchor.ScrollChild:SetSize(width, height)
	end
end

function Ns:UpdateProfile()
	for i=0, #Ns.ConfigFrame.MainMenuItems do
		if i == 0 then
			Ns.ConfigFrame.MainMenuItems[i].SvTable = Ns.Profile
		else
			Ns.ConfigFrame.MainMenuItems[i].SvTable = Ns.Profile.Stats
		end
	end
end

----------------------------------
--			Main Menu			--
----------------------------------
local function CreateMainMenu()
	local last, click
	for i=0, #Ns.SpellClass do
		local v = Ns.SpellClass[i]
		local f = Ns:GetWidget("MainMenuButton", Ns.ConfigFrame, sidePanelWidth) -- -10)
		f:SetEnabled(true)
		if string.trim(Ns.SpellClass[i].stat) == "" then
			f:SetEnabled(false)
 		end
		Ns.ConfigFrame.MainMenuItems[i] = f
		f:SetID(i)
		--f:SetTextColor(orangeColor.r, orangeColor.g, orangeColor.b)
		f:SetTextColor(1,1,1)
		f:SetLabel(format(Ns.WidgetDisplayFormat, Ns:GetSpellIcon(v), L[Ns.SpellClass[i].stat]))--, "THICKOUTLINE")
		--f:SetFont(Ns.ConfigDefaultFont2)
		f:SetFontSize(13)
		f.SpellClass = v.stat
		if i == 0 then -- Settings Tabs
			click = f
			f.DisplayOrder = Ns.SettingsGroupOrder
			f.GroupOrder = Ns.SettingsGroupOrder
			f.NextDisplayOrder = Ns.SettingsDisplayOrder
--			f.SvTable = Ns.Profile
		elseif i == 1 then
			f.DisplayOrder = Ns.LayoutGroupOrder
			f.GroupOrder = Ns.LayoutGroupOrder
			f.NextDisplayOrder = Ns.LayoutDisplayOrder
		elseif i == 2 then
			f.DisplayOrder = Ns.EventsGroupOrder
			f.GroupOrder = Ns.EventsGroupOrder
			f.NextDisplayOrder = Ns.EventsDisplayOrder
		elseif i == 3 then
			f.DisplayOrder = Ns.ProfileGroupOrder
			f.GroupOrder = Ns.ProfileGroupOrder
			f.NextDisplayOrder = Ns.ProfileDisplayOrder
		else  -- SpellClass Tabs
			f.DisplayOrder = Ns.DefaultOrder
			f.GroupOrder = Ns.SpellClass
			f.NextDisplayOrder = Ns.Options --Ns.OptionsList --Ns.GetStatOptionDefaults
--			f.SvTable = Ns.Profile.Stats
		end		
		f.MyInformation = v
		f.DisplayFunc = DisplayHorizontal
		if i == 0 then
			f:SetPoint("TOPLEFT", Ns.ConfigFrame.TopBorder, "LEFT", 0, mainMenuYOffset) -- the first main menu item position relative to the top border line
			last = f
		else
			f:SetPoint("TOPLEFT", last, "BOTTOMLEFT", 0, -5)
			last = f
		end
 		Ns:UpdateProfile()
	end
	click:Click()
end

--------------------------------------
--			Config frame			--
--------------------------------------
local function ColorBorders(self, r, g, b, a)
    for i=1, 4 do
        self.Borders[i]:SetColorTexture(r, g, b, a and a or 1)
    end
end

local function CreateBorder(frame, size)
    local f = frame
    local t = size or 1
    f.SetBorderColor = f.SetBorderColor or ColorBorders
    f.Borders = f.Borders or {}
    local offset = (t/2)
    for i=1, 4 do
        if not f.Borders[i] then
            f.Borders[i] = f:CreateLine(nil, "BORDER", nil, 0)
        end
        local b = f.Borders[i]
        b:SetThickness(t)
        b:SetColorTexture(1, 1, 1, 1)
        if i==1 then
            b:SetStartPoint("TOPLEFT", -offset, 0)
            b:SetEndPoint("TOPRIGHT", offset, 0)
        elseif i==2 then
            b:SetStartPoint("TOPRIGHT", 0, offset)
            b:SetEndPoint("BOTTOMRIGHT", 0, -offset)
        elseif i==3 then
            b:SetStartPoint("BOTTOMRIGHT", offset, 0)
            b:SetEndPoint("BOTTOMLEFT", -offset, 0)
        else
            b:SetStartPoint("BOTTOMLEFT", 0, -offset)
            b:SetEndPoint("TOPLEFT", 0, offset)
        end
    end
end

local f = CreateFrame("frame", "SinStatsConfigFrame", UIParent)
f:SetPoint("CENTER")
f:SetMovable(true)
f:SetUserPlaced(true)
f:EnableMouse(true)
f:Hide()

local function InitConfig()
	local f = SinStatsConfigFrame
	Ns.ConfigFrame = f
	f.MainMenuItems = {} -- Toplevel items always visible

	f.Background = f:CreateTexture()
	f.Background:SetAllPoints()
	f.Background:SetColorTexture(0.09020, 0.09020, 0.09020, 1)
	f:SetSize(Ns.ConfigWidth or 800, Ns.ConfigHeight or 500)

	f.SideBackground = f:CreateTexture()
	f.SideBackground:SetDrawLayer("ARTWORK", 1)
	f.SideBackground:SetSize(151, Ns.ConfigHeight)
	f.SideBackground:SetPoint("TOPLEFT", 0, 0)
	f.SideBackground:SetColorTexture(0.08020, 0.08020, 0.08020, 0.5)

	f:SetClampedToScreen(false)
	f:RegisterForDrag("LeftButton")
	f:SetScript("OnDragStart", function(self) self:StartMoving() end)
	f:SetScript("OnDragStart", function(self) self:StartMoving() end)
	f:SetScript("OnDragStop", function(self) self:StopMovingOrSizing() end)
	f:SetFrameStrata("HIGH")
	tinsert(UISpecialFrames, Ns.ConfigFrame:GetName())
	f.Close = CreateFrame('Button', '$parentClose', f, "UIPanelCloseButton")
	f.Close:SetPoint('TOPRIGHT', -11, -5)
	f.Close:SetSize(15, 15)
	f.Close:SetFrameLevel(4)
	f.Close:SetHighlightTexture(Ns.TexturePath.."Close")
	f.Close:GetHighlightTexture():SetVertexColor(redColor.r, redColor.g, redColor.b, 1)
	f.Close:SetNormalTexture(Ns.TexturePath.."Close")
	f.Close:GetNormalTexture():SetVertexColor(white.r, white.g, white.b, 0.3)
	f.Close:SetPushedTexture(Ns.TexturePath.."Close")
	f.Close:GetPushedTexture():SetVertexColor(redColor.r, redColor.g, redColor.b, 0.9)
	f.Close:SetScript('OnClick', function(self)
		self:GetParent():Hide()
	end)

	f.Title = f:CreateFontString("$parentTitle", "OVERLAY")
	f.Title:SetFont(Ns.ConfigDefaultFont, Ns.ConfigDefaultFontSize)
	f.Title:SetPoint("TOP", 0, -15)
	f.Logo = f:CreateTexture("$parentLogo", "OVERLAY")
	f.Logo:SetTexture(Ns.TexturePath.."SinStatsLogo")
--[[
	f.Logo:SetSize(300, 300) -- So the frame isn't mostly blank when first opened (MainMenuScripts.OnClick for where it's moved)
	f.Logo:SetPoint("CENTER", 70, -10)
]]--
	f.Logo:SetSize(75, 75)
	f.Logo:SetPoint("TOPLEFT", 25, 3)

	f.Caption = CreateFrame("Frame", nil, f) -- Does the actual dragging
	f.Caption:SetPoint("TOPLEFT")
	f.Caption:SetPoint("BOTTOMRIGHT", f, "TOPRIGHT", 0, -50)

	f.TopBorder = f:CreateLine()
	f.TopBorder:SetDrawLayer("OVERLAY", 1)
	f.TopBorder:SetThickness(3)
	f.TopBorder:SetAlpha(0.5)
	f.TopBorder:SetStartPoint("BOTTOMLEFT", f.Caption, 10, 100)
	f.TopBorder:SetEndPoint("BOTTOMRIGHT", f.Caption, -10, -2)
	f.TopBorder:SetColorTexture(0.05, 0.05, 0.05, 0)

	f.SideBorder = f:CreateLine()
	f.SideBorder:SetDrawLayer("OVERLAY", 1)
	f.SideBorder:SetThickness(1)
	f.SideBorder:SetAlpha(1)
	f.SideBorder:SetStartPoint("LEFT", f.TopBorder, 141, 0)
	f.SideBorder:SetEndPoint("BOTTOMLEFT", f, 151, 0)
	--f.SideBorder:SetColorTexture(unpack(borderColor))
	f.SideBorder:SetColorTexture(0, 0, 0)

	-- Corner textures
	f.topRight = f:CreateTexture("$parentTopRight", "OVERLAY")
	f.topRight:SetTexture(Ns.TexturePath.."top_right-corner")
	f.topRight:SetSize(20, 20)
	f.topRight:SetPoint("TOPRIGHT", 3, 3)

	f.bottomLeft = f:CreateTexture("$parentBotLeft", "OVERLAY")
	f.bottomLeft:SetTexture(Ns.TexturePath.."bottom_left-corner")
	f.bottomLeft:SetSize(20, 20)
	f.bottomLeft:SetPoint("BOTTOMLEFT", -3, -3)

	f.bottomRight = f:CreateTexture("$parentBotRight", "OVERLAY")
	f.bottomRight:SetTexture(Ns.TexturePath.."bottom_right-corner")
	f.bottomRight:SetSize(20, 20)
	f.bottomRight:SetPoint("BOTTOMRIGHT", 3, -3)

	f.topLeft = f:CreateTexture("$parentTopLeft", "OVERLAY")
	f.topLeft:SetTexture(Ns.TexturePath.."top_left-corner")
	f.topLeft:SetSize(20, 20)
	f.topLeft:SetPoint("TOPLEFT", -3, 3)

	-- Version
	f.Version = f:CreateFontString("$parentTitle", "OVERLAY")
	f.Version:SetFont(Ns.ConfigDefaultFont2, Ns.ConfigDefaultFontSize - 6)
	f.Version:SetPoint("BOTTOMLEFT", 65, 1)
	f.Version:SetText("|cff71ffc9" .. addVer .. "|r")

	local _, _, _, y = f.SideBorder:GetStartPoint()
	sidePanelWidth = select(3, f.SideBorder:GetStartPoint())
	Ns.maxOptionsWidth = f:GetWidth() - sidePanelWidth - 30 -- -30 right indent size
	Ns.SeparatorWidth  = Ns.maxOptionsWidth / 1.15
	f.TopAnchor = CreateFrame("Frame", nil, f)
	f.TopAnchor:SetSize(5, 2)
	f.TopAnchor:SetPoint("TOP", (sidePanelWidth/2), y-10) -- x,y entire right side panel content
	Ns.TopAnchor = f.TopAnchor

	CreateMainMenu()
	CreateBorder(f, 2)
	ColorBorders(f, 0, 0, 0)
end

function Ns:ToggleConfig()
	if not Ns.ConfigFrame then
		InitConfig()
	end
	Ns.ConfigFrame:SetShown(not Ns.ConfigFrame :IsShown())
end