local addonName, addon = ...

local dataObj, tooltip

--[[-----------------------------------------------------------------------------
LFG Menu
-------------------------------------------------------------------------------]]
local menu = LibStub('LibMenuAssist-1.0'):New()
menu.xOffset = 0
menu.yOffset = 0

menu.initialize = function(self, level)
	local info, grouped, leader = UIDropDownMenu_CreateInfo()
	if GetNumRaidMembers() > 0 then
		grouped, leader = true, IsRaidLeader()
	elseif GetNumPartyMembers() > 0 then
		grouped, leader = true, IsPartyLeader()
	end
 
	if IsInLFGDungeon() then
		info.text = TELEPORT_OUT_OF_DUNGEON
		info.func = MiniMapLFGFrame_TeleportOut
		UIDropDownMenu_AddButton(info)
	elseif grouped and IsPartyLFG() then
		info.text = TELEPORT_TO_DUNGEON
		info.func = MiniMapLFGFrame_TeleportIn
		UIDropDownMenu_AddButton(info)
	end

	local proposal, _, _, _, _, _, accepted = GetLFGProposal()
	if not proposal then
		local text = (select(3, GetLFGInfoServer()) and LEAVE_QUEUE) or (IsListedInLFR() and (grouped and UNLIST_MY_GROUP or UNLIST_ME))
		if text then
			info.text = text
			info.func = LeaveLFG
			info.disabled = grouped and not leader
			UIDropDownMenu_AddButton(info)
		end
	elseif not accepted then
		info.text = ENTER_DUNGEON
		info.func = AcceptProposal
		UIDropDownMenu_AddButton(info)
		info.text = LEAVE_QUEUE
		info.func = RejectProposal
		UIDropDownMenu_AddButton(info)
	end
end

--[[-----------------------------------------------------------------------------
Eye animation
-------------------------------------------------------------------------------]]
local iconCoords, index, shown, timer = CreateFrame('Frame', nil, UIParent), 1, false, 0
iconCoords:Hide()

iconCoords:SetScript('OnEvent', function(self, event)
	if select(3, GetLFGInfoServer()) or IsListedInLFR() or GetLFGRoleUpdate() then
		if not shown then
			index, shown, timer = 1, true, 0
			self:Show()
		end
	elseif shown then
		dataObj.iconCoords, shown = self[1], false
		self:Hide()
	end
	if tooltip then
		UpdateTooltip()
	else
		menu:Refresh()
	end
end)
iconCoords:RegisterEvent('LFG_PROPOSAL_UPDATE')
iconCoords:RegisterEvent('LFG_ROLE_CHECK_UPDATE')
iconCoords:RegisterEvent('LFG_UPDATE')
iconCoords:RegisterEvent('PLAYER_ENTERING_WORLD')

iconCoords:SetScript('OnShow', function(self)
	self:SetScript('OnShow', nil)
	for index = 1, 28 do
		local L, T = (index % 8) * 0.125, floor(index / 8) * 0.25
		self[index + 1] = { L + 0.02, L + 0.105, T + 0.04, T + 0.21 }
	end
end)
iconCoords[1] = { 0.02, 0.105, 0.04, 0.21 }

iconCoords:SetScript('OnUpdate', function(self, elapsed)
	timer = timer + elapsed
	if timer < 0.1 then return end
	timer = 0
	if index <= 28 then
		index = index + 1
	else
		index = 1
	end
	dataObj.iconCoords = self[index]
end)

--[[-----------------------------------------------------------------------------
Tooltip
-------------------------------------------------------------------------------]]
local tooltipOnHide

local function HideTooltip(self, ...)
	tooltip = nil
	self:SetScript('OnHide', tooltipOnHide)
	if tooltipOnHide then
		tooltipOnHide(self, ...)
		tooltipOnHide = nil
	end
	LFDSearchStatus:Hide()
end

local function UpdateTooltip()
	tooltip:SetText(LOOKING_FOR_DUNGEON, 1, 1, 1)
	if GetLFGProposal() then
		tooltip:AddLine(DUNGEON_GROUP_FOUND_TOOLTIP, nil, nil, nil, 1)
		if not LFDDungeonReadyPopup:IsShown() then
			tooltip:AddLine(" ")
			tooltip:AddLine(CLICK_HERE_FOR_MORE_INFO, nil, nil, nil, 1)
		end
	elseif select(3, GetLFGInfoServer()) then
		if not LFDSearchStatus:IsVisible() then
			local _, y = tooltip:GetCenter()
			LFDSearchStatus:ClearAllPoints()
			if y > UIParent:GetHeight() / 2 then
				LFDSearchStatus:SetPoint('TOP', tooltip, 'BOTTOM')
			else
				LFDSearchStatus:SetPoint('BOTTOM', tooltip, 'TOP')
			end
			LFDSearchStatus:SetParent(tooltip)
			LFDSearchStatus:Show()
		end
	elseif GetLFGRoleUpdate() then
		tooltip:AddLine(ROLE_CHECK_IN_PROGRESS_TOOLTIP, nil, nil, nil, 1)
	elseif IsListedInLFR() then
		tooltip:SetText(LOOKING_FOR_RAID, 1, 1, 1)
		tooltip:AddLine(YOU_ARE_LISTED_IN_LFR, nil, nil, nil, 1)
	elseif IsPartyLFG() and (GetNumPartyMembers() > 0 or GetNumRaidMembers() > 0) then
		tooltip:AddLine(YOU_ARE_IN_DUNGEON_GROUP, nil, nil, nil, 1)
	end
	tooltip:Show()
end

LFDSearchStatus:HookScript('OnHide', function(self)
	self:SetParent(UIParent)
	self:ClearAllPoints()
	self:SetPoint('CENTER')
end)

LFDSearchStatus:SetClampedToScreen(true)

--[[-----------------------------------------------------------------------------
LibDataBroker Object
-------------------------------------------------------------------------------]]
dataObj = LibStub('LibDataBroker-1.1'):NewDataObject(addonName, {
	type = 'launcher',
	label = LOOKING_FOR_DUNGEON,
	icon = [[Interface\LFGFrame\LFG-Eye]],
	iconCoords = iconCoords[1],

	OnClick = function(self, button)
		menu:Close()
		if tooltip then
			tooltip:Hide()
		end
		if button ~= 'RightButton' then
			if GetLFGProposal() then
				if not LFDDungeonReadyPopup:IsShown() then
					StaticPopupSpecial_Show(LFDDungeonReadyPopup)
				end
			elseif IsListedInLFR() then
				ToggleLFRParentFrame()
			else
				ToggleLFDParentFrame()
			end
		elseif IsPartyLFG() or IsInLFGDungeon() or GetLFGProposal() or select(3, GetLFGInfoServer()) or GetLFGRoleUpdate() or IsListedInLFR() then
			local _, y = self:GetCenter()
			if y > UIParent:GetHeight() / 2 then
				menu.point, menu.relativeTo, menu.relativePoint = 'TOP', self, 'BOTTOM'
			else
				menu.point, menu.relativeTo, menu.relativePoint = 'BOTTOM', self, 'TOP'
			end
			menu:Open()
		else
			ToggleLFRParentFrame()
		end
	end,

	OnTooltipShow = function(self)
		menu:Close()
		tooltip, tooltipOnHide = self, self:GetScript('OnHide')
		self:SetScript('OnHide', HideTooltip)
		UpdateTooltip()
	end
})

--[[-----------------------------------------------------------------------------
Alter Minimap
-------------------------------------------------------------------------------]]
if (GetAddOnMetadata(addonName, "X-AlterMinimap") or ""):lower() ~= "true" then return end

MiniMapLFGFrame:Hide()
MiniMapLFGFrame.Show = MiniMapLFGFrame.Hide
MiniMapLFGFrame:UnregisterAllEvents()
