local addonName, addon = ...

local anchorFramesToCreate = {
	["GUI_AnchorPoint1"] = {
		text = "BattleNet Notification",
		framewidth = nil,
	},
	["GUI_AnchorPoint2"] = {
		text = "Achivement Alert",
		framewidth = nil,
	},
	["GUI_AnchorPoint3"] = {
		text = "Dungeon Complete",
		framewidth = nil,
	},
	["GUI_AnchorPoint4"] = {
		text = "Loot Frame",
		framewidth = nil,
	},
	["GUI_AnchorPoint5"] = {
		text = "Casting Bar",
		framewidth = nil,
	},
	["GUI_AnchorPoint6"] = {
		text = "Tooltip Anchor",
		framewidth = nil,
	},
	["GUI_AnchorPoint7"] = {
		text = "Context Menu",
		framewidth = nil,
	},
	["GUI_AnchorPoint8"] = {
		text = "Alt Power Bar",
		framewidth = nil,
	},
	["GUI_AnchorPoint9"] = {
		text = "Boss Target Frame",
		framewidth = nil,
	},
	["GUI_AnchorPoint10"] = {
		text = "Focus Frame",
		framewidth = nil,
	},
}

local function AnchorCreateFrame(name, data)
	local frame = CreateFrame(
		"Button",
		name,                    -- required
		UIParent -- default to UIParent
	)
	frame:SetHeight(data.frameheight or 20)
	frame:SetWidth(data.framewidth or 140)
	frame.bg = frame:CreateTexture(nil, "BACKGROUND")
	frame.bg:SetTexture(0, 0, 0, .7)
	frame.bg:SetAllPoints()
	frame.txt = frame:CreateFontString(nil, "ARTWORK")
	frame.txt:SetFont([[Fonts\FRIZQT__.TTF]], 12)
	frame.txt:SetPoint("CENTER", frame, "CENTER")
	frame.txt:SetTextColor(1, 1, 1, 1)
	frame.txt:SetText(data.text)
	frame:RegisterForClicks("AnyUp")
	frame:SetScript("OnClick", function(self, button)
		if button == "RightButton" then
			frame:Hide()
		end
	end)
	frame:SetScript('OnEnter', function(self)
		GameTooltip:SetOwner(self, 'ANCHOR_TOP', 0, 1)
		GameTooltip:AddLine("|cffeda55fCtrl+Left Click|r to Move", 0.2, 1, 0.2)
		GameTooltip:AddLine("|cffeda55fRight Click|r to hide anchor", 0.2, 1, 0.2)
		GameTooltip:Show()
	end)
	frame:SetScript('OnLeave', addon.HideTooltip)
	return frame
end

for name, data in pairs(anchorFramesToCreate) do
	AnchorCreateFrame(name, data)
end

--[[-----------------------------------------------------------------------------
GUI Anchor Frames
-------------------------------------------------------------------------------]]
function addon:GUIDefaultAnchors()
	GUI_AnchorPoint1:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 200, -70) --BattleNet Toast Frame Anchor
	GUI_AnchorPoint2:SetPoint("TOPRIGHT", UIParent, "TOPRIGHT", -200, -70) --Achivement Alert Anchor
	GUI_AnchorPoint3:SetPoint("BOTTOM", UIParent, "BOTTOM", 0, 328) --DungeonCompletion Alert Anchor
	GUI_AnchorPoint4:SetPoint("TOP", UIParent, "TOP", 0, -50) --Loot Frame Anchor
	GUI_AnchorPoint5:SetPoint("CENTER", UIParent, "CENTER", 0, -50) --Casting Bar Anchor
	GUI_AnchorPoint6:SetPoint('BOTTOMLEFT', UIParent, 'BOTTOMLEFT', 35, 450) --Tooltip Anchor
	GUI_AnchorPoint7:SetPoint("RIGHT", UIParent, "RIGHT", -250, 0) --Context Menu
	GUI_AnchorPoint8:SetPoint("CENTER", UIParent, "CENTER", 0, 0) --PlayerAltPowerBar
	GUI_AnchorPoint9:SetPoint("CENTER", UIParent, "CENTER", 0, 0) --BossTargetFrame
	GUI_AnchorPoint10:SetPoint("CENTER", UIParent, "CENTER", 0, 0) --FocusFrame
end

function addon:ShowAnchorPoints()
	if not GUI_AnchorPoint1:IsShown() then
		for id = 1, 10 do
			_G["GUI_AnchorPoint"..id]:Show()
		end
	elseif GUI_AnchorPoint1:IsShown() then
		for id = 1, 10 do
			_G["GUI_AnchorPoint"..id]:Hide()
		end
	end
end

addon:GUIDefaultAnchors()


--[[-----------------------------------------------------------------------------
Loot Frame Anchors
-------------------------------------------------------------------------------]]

--function addon:LootFrameAnchors()
	--if addon.settings.LootGrowDirection then 
		--_G["GroupLootFrame1"]:ClearAllPoints()
		--_G["GroupLootFrame1"]:SetPoint("TOP", GUI_AnchorPoint4, "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)
	--else
		--_G["GroupLootFrame1"]:ClearAllPoints()
		--_G["GroupLootFrame1"]:SetPoint("BOTTOM", GUI_AnchorPoint4, "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

--[[-----------------------------------------------------------------------------
Achivement Frame Anchors
-------------------------------------------------------------------------------]]
--function AchievementAlertFrame_FixAnchors () -- A bliz function replacement 
	--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", GUI_AnchorPoint2, "BOTTOM", 0, 0);
			--return;
		--end
	--end
--end
--function AlertFrame_SetAchievementAnchors(alertAnchor)
  -- skip work if there hasn't been an achievement toast yet
  --if ( AchievementAlertFrame1 ) then
    --for i = 1, MAX_ACHIEVEMENT_ALERTS do
      --local frame = _G["AchievementAlertFrame"..i];
      --if ( frame and frame:IsShown() ) then
        --frame:SetPoint("BOTTOM", alertAnchor, "TOP", 0, 10);
        --alertAnchor = frame;
      --end
    --end
  --end
  --return alertAnchor;
--end



--function AchievementAlertFrame_GetAlertFrame() -- A bliz function replacement
	--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", GUI_AnchorPoint2, "BOTTOM", 0, 0);
			--else
				--frame:ClearAllPoints()
				--frame:SetPoint("TOP", previousFrame, "BOTTOM", 0, -10);
			--end
			--return frame;
		--end
		--previousFrame = frame;
	--end
	--return nil;
--end

--[[-----------------------------------------------------------------------------
Dungeon Completion Anchors
-------------------------------------------------------------------------------]]
--function DungeonCompletionAlertFrame_FixAnchors() -- A bliz function replacement
	--DungeonCompletionAlertFrame1:ClearAllPoints()
	--DungeonCompletionAlertFrame1:SetPoint("TOP", GUI_AnchorPoint3, "BOTTOM", 0, 0);
	--addon:LootFrameAnchors()
--end

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

--[[-----------------------------------------------------------------------------
Player Alt Power Bar Anchor
-------------------------------------------------------------------------------]]

--[[-----------------------------------------------------------------------------
BossTargetFrame Anchor
-------------------------------------------------------------------------------]]

--[[-----------------------------------------------------------------------------
Set Main AlertFrame Anchor
-------------------------------------------------------------------------------]]
local function AnchorMainAlertFrame()
	AlertFrame:ClearAllPoints()
	AlertFrame:SetPoint('TOP', GUI_AnchorPoint2, 'BOTTOM')
end

--[[-----------------------------------------------------------------------------
Initialize anchors
-------------------------------------------------------------------------------]]
addon.RegisterEvent("InitializeAnchors", 'PLAYER_LOGIN', function(self, event)
	addon.UnregisterEvent(self, event)
	for id = 1, 10 do
		_G["GUI_AnchorPoint"..id]:Hide()
	end
	--addon:LootFrameAnchors()
	AnchorMainAlertFrame()
end)