local addonName, addon = ...
_G[addonName] = addon

--[[-----------------------------------------------------------------------------
Alert Frame
-------------------------------------------------------------------------------]]
function addon:LootFrameAnchors()
	
	if not addon.settings.LootGrowDirection or addon.settings.LootGrowDirection == true then 
		_G["GroupLootFrame1"]:ClearAllPoints()
		_G["GroupLootFrame1"]:SetPoint("TOP", GUIAnchorPoint4, "BOTTOM", 0, 0)
		GroupLootFrame2:ClearAllPoints()
		GroupLootFrame2:SetPoint("TOP", GroupLootFrame1, "BOTTOM", 0, -10)
		GroupLootFrame3:ClearAllPoints()
		GroupLootFrame3:SetPoint("TOP", GroupLootFrame2, "BOTTOM", 0, -10)
		GroupLootFrame4:ClearAllPoints()
		GroupLootFrame4:SetPoint("TOP", GroupLootFrame3, "BOTTOM", 0, -10)
	elseif addon.settings.LootGrowDirection == false then
		_G["GroupLootFrame1"]:ClearAllPoints()
		_G["GroupLootFrame1"]:SetPoint("BOTTOM", GUIAnchorPoint4, "TOP", 0, 0)
		GroupLootFrame2:ClearAllPoints()
		GroupLootFrame2:SetPoint("BOTTOM", GroupLootFrame1, "TOP", 0, 10)
		GroupLootFrame3:ClearAllPoints()
		GroupLootFrame3:SetPoint("BOTTOM", GroupLootFrame2, "TOP", 0, 10)
		GroupLootFrame4:ClearAllPoints()
		GroupLootFrame4:SetPoint("BOTTOM", GroupLootFrame3, "TOP", 0, 10)
	end
end

function AchievementAlertFrame_FixAnchors ()
	if ( not AchievementAlertFrame1 ) then
		return;
	end
	
	for i=NUM_GROUP_LOOT_FRAMES, 1, -1  do
		local frame = _G["GroupLootFrame"..i];
		if ( frame and frame:IsShown() ) then
			AchievementAlertFrame1:ClearAllPoints()
			AchievementAlertFrame1:SetPoint("TOP", GUIAnchorPoint2, "BOTTOM", 0, 0);
			return;
		end
	end
end

function AchievementAlertFrame_GetAlertFrame()
	local name, frame, previousFrame;
	for i=1, MAX_ACHIEVEMENT_ALERTS do
		name = "AchievementAlertFrame"..i;
		frame = _G[name];
		if ( frame ) then
			if ( not frame:IsShown() ) then
				return frame;
			end
		else
			frame = CreateFrame("Button", name, UIParent, "AchievementAlertFrameTemplate");
			if ( not previousFrame ) then
				frame:ClearAllPoints()
				frame:SetPoint("TOP", GUIAnchorPoint2, "BOTTOM", 0, 0);
			else
				frame:ClearAllPoints()
				frame:SetPoint("TOP", previousFrame, "BOTTOM", 0, -10);
			end
			return frame;
		end
		previousFrame = frame;
	end
	return nil;
end

function DungeonCompletionAlertFrame_FixAnchors()
	DungeonCompletionAlertFrame1:ClearAllPoints()
	DungeonCompletionAlertFrame1:SetPoint("TOP", GUIAnchorPoint3, "BOTTOM", 0, 0);
	addon:LootFrameAnchors()
end



--[[-----------------------------------------------------------------------------
BattleNet notifications
-------------------------------------------------------------------------------]]
hooksecurefunc("BNToastFrame_Show", function(self)
	BNToastFrame:ClearAllPoints()
	BNToastFrame:SetPoint("TOP", GUIAnchorPoint1, "BOTTOM", 0, 0)
end)

addon.RegisterEvent("InitializeAlerts", 'PLAYER_ENTERING_WORLD', function(self, event)
	addon.UnregisterEvent(self, event)
	addon:LootFrameAnchors()
end)