local NAME, ADDON = ...

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

local AceGUI = LibStub('AceGUI-3.0')
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

function SMDGT:SetupOptionsPanel()
	local panel = AceGUI:Create("BlizOptionsGroup")
	panel:SetName("dnnsGT Settings")
	panel:SetLayout("List")


	--local cb = AceGUI:Create("CheckBox")
	--cb:SetLabel("Activate Sounds")
	--cb:SetValue(SMDGTDB.active)
	--cb:SetCallback("OnValueChanged", function(_, _, active)
	--	SMDGTDB.active = active
	--	if active == false then
	--		--SMDGT:LoadSoundpack()
	--		ReloadUI()
	--		else
	--		SMDGT:LoadSoundpack()
	--		ReloadUI()
	--	end
	--end)
	--panel:AddChild(cb)

	local header = AceGUI:Create("Heading")
	header:SetText("Sound settings")
	header:SetFullWidth(true) -- Sorgt dafür, dass der Header die volle Breite des Frames einnimmt
	panel:AddChild(header)

	local label = AceGUI:Create("Label")
	label:SetText("Choose the sound package you want:")
	--label:SetFont("Fonts\\dnnsGT.ttf", 14)
	label:SetFullWidth(true)
	panel:AddChild(label)


	local dropdown = AceGUI:Create("Dropdown")
	dropdown:SetList(ADDON.soundpacks)
	dropdown:SetValue(SMDGTDB.selectedsound)
	dropdown:SetCallback("OnValueChanged", function(_, _, k)
		SMDGTDB.selectedsound = k
		--	if SMDGTDB.active then
		--		SMDGT:LoadSoundpack()
		--		SMDGT:LoadBorderpack()
		--		ReloadUI()
		--	end
	end)
	panel:AddChild(dropdown)

	local header2 = AceGUI:Create("Heading")
	header2:SetText("Border settings")
	header2:SetFullWidth(true) -- Sorgt dafür, dass der Header die volle Breite des Frames einnimmt
	panel:AddChild(header2)

	local labelb = AceGUI:Create("Label")
	labelb:SetText("Choose the border package you want:")
	--label:SetFont("Fonts\\dnnsGT.ttf", 14)
	labelb:SetFullWidth(true)
	panel:AddChild(labelb)

	local dropdown2 = AceGUI:Create("Dropdown")
	dropdown2:SetList(ADDON.borderpacks)
	dropdown2:SetValue(SMDGTDB.selectedborder)
	dropdown2:SetCallback("OnValueChanged", function(_, _, kk)
		SMDGTDB.selectedborder = kk
		--	if SMDGTDB.active then
		--		SMDGT:LoadBorderpack()
		--		SMDGT:LoadSoundpack()
		--		ReloadUI()
		--	end
	end)
	panel:AddChild(dropdown2)

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

	local header3 = AceGUI:Create("Heading")
	header3:SetText("For the settings to take effect you have to reload your interface")
	header3:SetFullWidth(true) -- Sorgt dafür, dass der Header die volle Breite des Frames einnimmt
	panel:AddChild(header3)

	local button = AceGUI:Create("Button")
	button:SetText("ReloadUI now")
	button:SetWidth(200)
	button:SetCallback("OnClick", ReloadInterface)
	panel:AddChild(button)

	InterfaceOptions_AddCategory(panel.frame)
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
