local addonName, addon = ...
local defaults = {
	class = true
}

local realmName = GetRealmName()
--[[-----------------------------------------------------------------------------
RegisterDefaultSetting - Allow modules to set their defaults
-------------------------------------------------------------------------------]]
function addon:RegisterDefaultSetting(key, value)	-- Use caution if using this after ADDON_LOADED
	defaults[key] = value
end

--[[-----------------------------------------------------------------------------
Load settings, apply version conversions as needed, and link to defaults
-------------------------------------------------------------------------------]]
addon.RegisterEvent("Settings-Load", 'ADDON_LOADED', function(self, event, name)
	if name ~= addonName then return end
	addon.UnregisterEvent(self, event)
	local name, class = UnitClass('player')
	name = UnitName('player')
	local settings = _G[addonName .. "Settings"]
	if type(settings) ~= 'table' then
		settings = { }
		_G[addonName .. "Settings"] = settings
	end
	if type(settings[realmName]) ~= 'table' then
		settings[realmName] = { }
	end
	if type(settings[realmName][name]) ~= 'table' then
		settings[realmName][name] = { class = class }
	end
	settings.version = nil -- Reset value on logout (so can iterate over data without issue)
	addon.settings = setmetatable(settings[realmName][name], { __index = defaults })
	if not addon.settings.HelpViewed then
		addon:LoadGUIHelp()
	end
	if IsAddOnLoadOnDemand("GrimUI_DevTools") then 
		if addon.settings.loadDevTools == true then
			LoadAddOn("GrimUI_DevTools")
		end
	end
	return settings
end)

--[[-----------------------------------------------------------------------------
Character Specific WTF Reset.
-------------------------------------------------------------------------------]]
function addon:ResetWTF()
	local name, class = UnitClass('player')
	name = UnitName('player')
	local settings = _G[addonName .. "Settings"]
	if type(settings[realmName][name]) == 'table' then
		settings[realmName][name] = { class = class }
	end
	settings.version = nil	-- Reset value on logout 
	return settings 
end

--[[-----------------------------------------------------------------------------
Remove unused settings or settings that are the same as defaults
-------------------------------------------------------------------------------]]
addon.RegisterEvent("Settings-Unload", 'PLAYER_LOGOUT', function()
	local settings = addon.settings
	for key, value in pairs(settings) do
		if value == defaults[key] or defaults[key] == nil then
			settings[key] = nil
		end
	end
	_G[addonName .. "Settings"].version = GetAddOnMetadata(addonName, 'Version')
	_G[addonName .. "Settings"].lastVersion = GetAddOnMetadata(addonName, 'Version')
end)

--[[-----------------------------------------------------------------------------
LOD Configuration
-------------------------------------------------------------------------------]]
-- options
local LOA = LibStub and LibStub('LibOptionsAssist-1.0', true)
if not (LOA and select(2, GetAddOnInfo(addonName .. "_Config"))) then return end	-- Make sure config support exists
addon.configPanel = LOA:AddEntry(addonName, nil, addonName .. "_Config")
_G['SLASH_' .. addonName .. 1] = '/' .. addonName
_G['SLASH_' .. addonName .. 2] = '/' .. addonName:lower()
_G['SLASH_' .. addonName .. 3] = '/' .. addonName:upper()
_G['SLASH_' .. addonName .. 4] = '/gui'
SlashCmdList[addonName] = addon.configPanel
InterfaceOptionsFrame:SetFrameStrata('HIGH')
InterfaceOptionsFrame.SetFrameStrata = addon.DoNothing

--[[-----------------------------------------------------------------------------
GrimUI Options Frame Logo
-------------------------------------------------------------------------------]]
local GUI_Logo = CreateFrame('Frame', "GUI_Logo", InterfaceOptionsFrame)
GUI_Logo:SetSize(256, 256)
GUI_Logo:SetPoint('BOTTOM', InterfaceOptionsFrame, 'TOP', 0, -75)
GUI_Logo:SetBackdrop({
	bgFile = [[Interface\AddOns\GrimUI\Media\GrimUILogo]],
	edgeFile = nil,
	edgeSize = 0,
	tile = false,
	insets = { left = 0, right = 0, top = 0, bottom = 0 }
})
GUI_Logo:Hide()

--[[-----------------------------------------------------------------------------
In Game Help System Setup
-------------------------------------------------------------------------------]]
function addon:LoadGUIHelp()
	addon.configPanel()
	GUI_HelpInitiate()
end
local GUI_HelpButton = CreateFrame('Button', "GUI_HelpButton", InterfaceOptionsFrame)
GUI_HelpButton:SetSize(25, 40)
GUI_HelpButton:SetPoint('TOP', InterfaceOptionsFrame, 'TOP', 190, -37)
GUI_HelpButton:RegisterForClicks("AnyUp")
GUI_HelpButton:SetFrameStrata("DIALOG")
GUI_HelpButton:SetNormalTexture([[Interface\BUTTONS\UI-MicroButton-Help-Up]])
GUI_HelpButton:SetHighlightTexture([[Interface\BUTTONS\UI-MicroButton-Hilight]])
GUI_HelpButton:SetPushedTexture([[Interface\BUTTONS\UI-MicroButton-Help-Down]])
local HelpText = GUI_HelpButton:CreateFontString(nil, 'DIALOG')
HelpText:SetFont([[Fonts\FRIZQT__.TTF]], 14)
HelpText:SetText("Help")
HelpText:SetPoint("RIGHT", GUI_HelpButton, "LEFT", 0, -5)
HelpText:SetTextColor(1, .10, .10, 1)
GUI_HelpButton:SetScript('OnClick', function(self)
	if button == 'LeftButton' then
		GUI_HelpInitiate()
	else
		GUI_HelpInitiate()
	end
end)
GUI_HelpButton:Hide()

--[[-------------------------------------------------------------------------------
Config Panel Show/Hide Scripts - Put all config panel adjustment functions here!
---------------------------------------------------------------------------------]]
addon.configPanel:SetScript('OnHide', function()
	GUI_Logo:Hide()
	GUI_HelpButton:Hide()
end)

addon.configPanel:SetScript('OnShow', function()
	GUI_Logo:Show()
	GUI_HelpButton:Show()
end)

--[[-------------------------------------------------------------------------------
Default WTF Settings - Try them here to stop nextto error and other errors.
---------------------------------------------------------------------------------]]
--Settings.lua settings
addon:RegisterDefaultSetting("HelpViewed", false)
addon:RegisterDefaultSetting("loadDevTools", false)


--DevTools.lua settings
addon:RegisterDefaultSetting("showDevBar", true)

--HideFrames.lua

--Skins.lua settings
addon:RegisterDefaultSetting("useViewPort", false)
addon:RegisterDefaultSetting("VPLeftPos", 0)
addon:RegisterDefaultSetting("VPRightPos", 0)
addon:RegisterDefaultSetting("VPTopPos", 0)
addon:RegisterDefaultSetting("VPBottomPos", 90)
addon:RegisterDefaultSetting("artSetPoint", 0)
addon:RegisterDefaultSetting("artSetAlpha", 1)
addon:RegisterDefaultSetting("ShowAggroArt", true)
addon:RegisterDefaultSetting("resolution", false)
addon:RegisterDefaultSetting("uiSkin", [[Interface\AddOns\]] .. addonName .. [[\Skins\]] .. UnitFactionGroup('player'))

--Anchors.lua settings
addon:RegisterDefaultSetting("LootGrowDirection", true)

--FrameMover.lua
addon:RegisterDefaultSetting("movedFrames", true)
--addon:RegisterDefaultSetting("movedFrames", true)
addon:RegisterDefaultSetting("lockFrames", false)

--ChatFrames.lua settings
addon:RegisterDefaultSetting("chatInitialized", false)
addon:RegisterDefaultSetting("showCombatLog", true)
addon:RegisterDefaultSetting("showInfoLog", true)
addon:RegisterDefaultSetting("showChatTabs", false)
addon:RegisterDefaultSetting("useGrimChat", true)

--MiniMap.lua settings
addon:RegisterDefaultSetting("NotSoMinimap", false)
addon:RegisterDefaultSetting("ShowMiniMap", true)
addon:RegisterDefaultSetting("MapArt", true)
addon:RegisterDefaultSetting("MapButStyle", 1)
addon:RegisterDefaultSetting("GmapMask", "Interface\\BUTTONS\\WHITE8X8")
addon:RegisterDefaultSetting("NSmapMask", "Interface\\BUTTONS\\WHITE8X8")
addon:RegisterDefaultSetting("GmapAlpha", 1)
addon:RegisterDefaultSetting("NSmapAlpha", 1)
addon:RegisterDefaultSetting("NSmapScale", 1.5)

--DashBoard.lua settings
addon:RegisterDefaultSetting("autoRepair", true)
addon:RegisterDefaultSetting("mailShow", true)
addon:RegisterDefaultSetting("durabilityShow", true)
addon:RegisterDefaultSetting("backpackShow", true)
addon:RegisterDefaultSetting("moneyShow", true)
addon:RegisterDefaultSetting("clockShow", true)
addon:RegisterDefaultSetting("latencyShow", true)
addon:RegisterDefaultSetting("fpsShow", true)
addon:RegisterDefaultSetting("socialShow", true)
addon:RegisterDefaultSetting("guildShow", true)
addon:RegisterDefaultSetting("speedShow", true)
addon:RegisterDefaultSetting("clock24hr", false)
addon:RegisterDefaultSetting("GuildRepair", true)
addon:RegisterDefaultSetting("dashboardScale", 1)
addon:RegisterDefaultSetting("CurrentMail", 0)
addon:RegisterDefaultSetting("dashOnTop", false)

--ContextMenu.lua settings
addon:RegisterDefaultSetting("showContextMenu", false)

--TooltipNotes.lua settings

--Tooltips.lua settings
addon:RegisterDefaultSetting("loadTooltips", true)
addon:RegisterDefaultSetting("tooltipIconSize", 20)
addon:RegisterDefaultSetting("tooltipAnchor", false)
addon:RegisterDefaultSetting("showToT", true)
addon:RegisterDefaultSetting("showTitle", true)
addon:RegisterDefaultSetting("showRaidIcon", true)
addon:RegisterDefaultSetting("showHealthBar", true)
addon:RegisterDefaultSetting("growDirection", 1)
addon:RegisterDefaultSetting("showSpellIcon", true)
addon:RegisterDefaultSetting("showItemIcon", true)
--Backpack.lua settings
--Hud.lua settings
addon:RegisterDefaultSetting("hudShow", true)
addon:RegisterDefaultSetting("hudWidth", 100)
addon:RegisterDefaultSetting("hudHeight", 100)
addon:RegisterDefaultSetting("hudScale", 1)
--ActionBars.lua settings
--ExpRepBar.lua settings
addon:RegisterDefaultSetting("showExpBar", true)
addon:RegisterDefaultSetting("showRepBar", true)
addon:RegisterDefaultSetting("xpBarvar", "xpTot")
addon:RegisterDefaultSetting("repBarvar", "repTot")
addon:RegisterDefaultSetting("showGuild", false)

--TargetFrame.lua settings
addon:RegisterDefaultSetting("hideBlizTargetFrame", true)
--PlayerFrame.lua settings
addon:RegisterDefaultSetting("hideBlizPlayerFrame", true)
addon:RegisterDefaultSetting("showPlayerFrame", true)
addon:RegisterDefaultSetting("playerFrameScale", 1)
addon:RegisterDefaultSetting("playerHealthColorR", 0.231)
addon:RegisterDefaultSetting("playerHealthColorG", 0.682)
addon:RegisterDefaultSetting("playerHealthColorB", 0.419)
addon:RegisterDefaultSetting("playerHealthColorAlpha", 1)
addon:RegisterDefaultSetting("playerHealthTxtColorR", 1)
addon:RegisterDefaultSetting("playerHealthTxtColorG", 1)
addon:RegisterDefaultSetting("playerHealthTxtColorB", 1)
addon:RegisterDefaultSetting("playerHealthTxtColorAlpha", 1)
--PartyFrames.lua settings
addon:RegisterDefaultSetting("hideBlizPartyFrames", true)
addon:RegisterDefaultSetting("showPartyFrames", true)
addon:RegisterDefaultSetting("hideGrimPartyInRaid", true)
--StylePartyFrames.lua settings
addon:RegisterDefaultSetting("stylePartyFrames", 1)
--CastBar.lua settings

--Features_A.lua settings
addon:RegisterDefaultSetting("showBagBar", true)
addon:RegisterDefaultSetting("showMicroBar", true)
addon:RegisterDefaultSetting("showWatchFrame", true)

--Features_B.lua settings
addon:RegisterDefaultSetting("Muted", false)
addon:RegisterDefaultSetting("MutedVolume", 0.2)
addon:RegisterDefaultSetting("showVolume", true)
addon:RegisterDefaultSetting("autoDismount", true)
