local NAME, ADDON = ...

local SMDGT = LibStub("AceAddon-3.0"):NewAddon(NAME)
local LSM = LibStub("LibSharedMedia-3.0")

function SMDGT:OnInitialize()
	if not SMDGTDB then
		-- defaults
		SMDGTDB = {
			active = true,
			selectedsound = "RealisticFemaleGerman",
			selectedborder = "GoldAndSilver"
		}
	end

	self:SetupOptionsPanel()

	self:LoadSoundpack()
	self:LoadBorderpack()
	self:LoadFonts()
end

local function createDDSetting(category, var, varTbl, varType, name, default, tooltip, settingsTbl, getCallback, setCallback)
	local function GetOptions()
		local container = Settings.CreateControlTextContainer()
		for ddKey, ddText in pairs(settingsTbl) do
			container:Add(ddKey, ddText)
		end
		return container:GetData()
	end
	local setting = Settings.RegisterAddOnSetting(category, name, var, varType, default)
	local initializer = Settings.CreateDropdownInitializer(setting, GetOptions, tooltip)
	Settings.SetOnValueChangedCallback(var, setCallback, initializer)
	setting:SetValue(getCallback())
	return initializer
end

local function getDBFunc(funcType, setting)
	if funcType == "GETTER" then
		return function ()
			return SMDGTDB[setting]
		end
	elseif funcType == "SETTER" then
		return function (...)
			local newValue = select(3, ...)
			SMDGTDB[setting] = newValue
		end
	elseif funcType == "GETTERSETTER" then
		return getDBFunc("GETTER", setting), getDBFunc("SETTER", setting)
	end
end

function SMDGT:SetupOptionsPanel()

	local function ReloadInterface()
		SMDGT:LoadSoundpack()
		SMDGT:LoadBorderpack()
		ReloadUI()
	end

	-- New
	local category, layout = Settings.RegisterVerticalLayoutCategory(NAME)
	Settings.RegisterAddOnCategory(category)

	-- the "layout:AddInitializer" can be removed when using the old way
	layout:AddInitializer(createDDSetting(category, "border", {}, Settings.VarType.String, "Border Package", "GoldAndSilver", "Choose the sound package that you want.", ADDON.borderpacks, getDBFunc("GETTERSETTER", "selectedborder")))
	layout:AddInitializer(createDDSetting(category, "sound", {}, Settings.VarType.String, "Sound Package", "RealisticFemaleGerman", "Choose the sound package that you want.", ADDON.soundpacks, getDBFunc("GETTERSETTER", "selectedsound")))
	
	local initializer = CreateSettingsButtonInitializer("", "Apply & Reload", ReloadInterface, "Apply Changes and Reload (For the settings to take effect you have to reload your interface)", true);

	layout:AddInitializer(initializer)
end

function SMDGT:LoadSoundpack()
	local basePath = "Interface\\Addons\\" .. NAME .. "\\Soundpacks\\" .. SMDGTDB.selectedsound .. "\\"

	for k, v in pairs(ADDON.sounds) do
		LSM:Register("sound", "|cFFFFFF00dnnsGT:|r " .. k, basePath .. v .. ".mp3")
	end

	self:UpdateThirdPartyAddonPaths()
end

function SMDGT:LoadBorderpack()
	local basePath = "Interface\\Addons\\" .. NAME .. "\\Borderpacks\\" .. SMDGTDB.selectedborder .. "\\"

	for kk, v in pairs(ADDON.borders) do
		LSM:Register("border", "|cFFFFFF00dnnsGT:|r " .. kk, basePath .. v .. ".tga")
	end

	self:UpdateThirdPartyAddonPaths()
end

function SMDGT:LoadFonts()
	LSM:Register("font", "|cFFFFFF00dnnsGT|r", [[Interface\Addons\SharedMedia_dnnsGT\Fonts\dnnsGT.ttf]])

	self:UpdateThirdPartyAddonPaths()
end

function SMDGT:UpdateThirdPartyAddonPaths()
	if WeakAuras then
		self:UpdateWeakAuraSoundsPaths()
	end
end

function SMDGT:UpdateWeakAuraSoundsPaths()
	local UpdatePath = function(data)
		if (not data) or (type(data) ~= "table") or (not data.sound) or (type(data.sound) ~= "string") then return end
		if data.sound:find(NAME) then
			if not data.sound:find(SMDGTDB.selectedsound) then
				-- old soundpack path present, need to update
				local idx = data.sound:find([[\[^\]*$]])
				local p = data.sound:sub(1, idx - 1)
				p = p:sub(1, p:find([[\[^\]*$]]) - 1)
				local s = data.sound:sub(idx + 1)
				data.sound = p .. "\\" .. SMDGTDB.selectedsound .. "\\" .. s
			end
		end
	end

	for _, aura in pairs(WeakAurasSaved.displays) do
		local actions = aura.actions
		UpdatePath(actions.init)
		UpdatePath(actions.start)
		UpdatePath(actions.finish)
		for _, condition in ipairs(aura.conditions) do
			for _, change in ipairs(condition.changes) do
				UpdatePath(change.value)
			end
		end
	end
end
