local _, GF = ...

GF.Availability = {}

local restrictionFrame

local function inChatMessagingLockdown()
	local ci = C_ChatInfo
	if ci and ci.InChatMessagingLockdown then
		return ci.InChatMessagingLockdown()
	end
	return false
end

function GF.Availability:IsRestricted()
	if C_LFGInfo and C_LFGInfo.CanPlayerUsePremadeGroup then
		local canUse = C_LFGInfo.CanPlayerUsePremadeGroup()
		if not canUse then
			return true
		end
	end
	return inChatMessagingLockdown()
end

function GF.Availability:IsLfgPaused()
	return self._restricted == true
end

function GF.Availability:GetBlockMessage()
	if C_LFGInfo and C_LFGInfo.CanPlayerUsePremadeGroup then
		local canUse, failureReason = C_LFGInfo.CanPlayerUsePremadeGroup()
		if not canUse then
			return failureReason
		end
	end
	if inChatMessagingLockdown() then
		local L = GF.L or {}
		return L.UNAVAILABLE_RESTRICTED
			or "Secret-value restrictions are active. Please open Premade Groups, or leave the instance and open this addon again."
	end
	return nil
end

function GF.Availability:ReleaseBlizzardHandoff()
	if GF.SubtitleBar and GF.SubtitleBar.ReleaseBlizzardSearchBox then
		GF.SubtitleBar:ReleaseBlizzardSearchBox()
	end
	if GF.CreatePanel and GF.CreatePanel.ReleaseCreateFields then
		GF.CreatePanel:ReleaseCreateFields("addon")
	end
	if GF.CreateDrawer and GF.CreateDrawer.IsOpen and GF.CreateDrawer:IsOpen() then
		GF.CreateDrawer:Close(true)
	end
end

function GF.Availability:OnEnterRestricted()
	self:ReleaseBlizzardHandoff()
	local mf = GF.MainFrame
	if mf and mf.frame and mf.frame:IsShown() then
		local msg = self:GetBlockMessage()
		mf:HideFrame()
		if msg then
			self:NotifyBlocked(msg)
		end
	end
end

function GF.Availability:OnLeaveRestricted()
end

function GF.Availability:UpdateRestrictedState()
	local nowRestricted = self:IsRestricted()
	if nowRestricted and not self._restricted then
		self._restricted = true
		self:OnEnterRestricted()
	elseif not nowRestricted and self._restricted then
		self._restricted = false
		self:OnLeaveRestricted()
	end
	return self._restricted
end

function GF.Availability:ShouldProcessLfgEvent()
	self:UpdateRestrictedState()
	return not self._restricted
end

function GF.Availability:Init()
	if self._inited then
		return
	end
	self._inited = true
	self._restricted = false
	if not restrictionFrame then
		restrictionFrame = CreateFrame("Frame")
		restrictionFrame:SetScript("OnEvent", function()
			GF.Availability:UpdateRestrictedState()
		end)
	end
	restrictionFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
	restrictionFrame:RegisterEvent("PLAYER_LOGIN")
end

function GF.Availability:NotifyBlocked(msg)
	if not msg or msg == "" then
		return
	end
	if DEFAULT_CHAT_FRAME and DEFAULT_CHAT_FRAME.AddMessage then
		DEFAULT_CHAT_FRAME:AddMessage("|cffff0000GF:|r |cffff99cc" .. msg .. "|r")
	end
end
