local addonName, addon = ...

local defaults = {
	class = true
}

--[[-----------------------------------------------------------------------------
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 realm = GetRealmName()

	local settings = _G[addonName .. "Settings"]
	if type(settings) ~= 'table' then
		settings = { }
		_G[addonName .. "Settings"] = settings
	end
	if type(settings[realm]) ~= 'table' then
		settings[realm] = { }
	end
	if type(settings[realm][name]) ~= 'table' then
		settings[realm][name] = { class = class }
	end

	settings.version = nil		-- Reset value on logout (so can iterate over data without issue)
	addon.settings = setmetatable(settings[realm][name], { __index = defaults })
	return settings
end)

--[[-----------------------------------------------------------------------------
Character Specific WTF Reset.
-------------------------------------------------------------------------------]]
function addon:ResetWTF()
	local name, class = UnitClass('player')
	name = UnitName('player')
	local realm = GetRealmName()
	local settings = _G[addonName .. "Settings"]
	
	if type(settings[realm][name]) == 'table' then
		settings[realm][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')
end)

--[[-----------------------------------------------------------------------------
LOD Configuration
-------------------------------------------------------------------------------]]
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()
SlashCmdList[addonName] = addon.configPanel

InterfaceOptionsFrame:SetFrameStrata('HIGH')
InterfaceOptionsFrame.SetFrameStrata = addon.DoNothing

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

--[[-----------------------------------------------------------------------------
In Game Help System Setup
-------------------------------------------------------------------------------]]
function addon:LoadGUIHelp()
	addon.configPanel()
	GUIHelpInitiate()
end

local helpButton = CreateFrame('button', nil, InterfaceOptionsFrame)
helpButton:SetHeight(40)
helpButton:SetWidth(25)
helpButton:SetPoint('TOP', InterfaceOptionsFrame, 'TOP', 190, -37)
helpButton:RegisterForClicks("AnyUp")
helpButton:SetFrameStrata("DIALOG")
helpButton:SetNormalTexture([[Interface\BUTTONS\UI-MicroButton-Help-Up]])
helpButton:SetHighlightTexture([[Interface\BUTTONS\UI-MicroButton-Hilight]])
helpButton:SetPushedTexture([[Interface\BUTTONS\UI-MicroButton-Help-Down]])
local HelpText = helpButton:CreateFontString(nil, 'DIALOG')
HelpText:SetFont([[Fonts\FRIZQT__.TTF]], 14)
HelpText:SetText("Help")
HelpText:SetPoint("RIGHT", helpButton, "LEFT", 0, -5)
HelpText:SetTextColor(1, .10, .10, 1)

helpButton:SetScript('OnClick', function(self)
	
	if button == 'LeftButton' then
		GUIHelpInitiate()
	else
		GUIHelpInitiate()
	end
	
end)
helpButton:Hide()

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

addon.configPanel:SetScript('OnShow', function()
	InterfaceOptionsFrame:SetWidth(800)
	logo:Show()
	helpButton:Show()
end)