local _G = _G
local GetReadyCheckStatus = _G.GetReadyCheckStatus
local IRF3 = _G[...]

local readyCheckFrame = CreateFrame("Frame")
readyCheckFrame:Hide()
readyCheckFrame:SetScript("OnUpdate", function(self, timer)
	self.timer = self.timer - timer
	if self.timer > 0 then
		if self.step == 2 then
			self.alpha = self.timer
			for _, header in pairs(IRF3.headers) do
				for _, member in pairs(header.members) do
					member.readyCheckIcon:SetAlpha(self.alpha)
				end
			end
		end
	elseif self.step == 1 then
		self.step, self.timer = 2, 1
	else
		self:Hide()
	end
end)
readyCheckFrame:SetScript("OnHide", function(self)
	IRF3.doReadyCheck = nil
	for _, header in pairs(IRF3.headers) do
		for _, member in pairs(header.members) do
			member.readyCheckIcon:Hide()
		end
	end
end)
readyCheckFrame:SetScript("OnEvent", function(self, event)
	if event == "READY_CHECK_FINISHED" and IRF3.doReadyCheck then
		self.step, self.timer = 1, 2.5
		self:Show()
	else
		self:Hide()
	end
end)
readyCheckFrame:RegisterEvent("READY_CHECK_FINISHED")
readyCheckFrame:RegisterEvent("RAID_ROSTER_UPDATE")
readyCheckFrame:RegisterEvent("PARTY_MEMBERS_CHANGED")
readyCheckFrame:RegisterEvent("PLAYER_REGEN_DISABLED")

function InvenRaidFrames3Member_UpdateReadyCheck(self)
	if IRF3.doReadyCheck then
		if GetReadyCheckStatus(self.unit) then
			CompactUnitFrame_UpdateReadyCheck(self)
		end
	else
		self.readyCheckIcon:Hide()
	end
end

function IRF3:READY_CHECK()
	readyCheckFrame:Hide()
	self.doReadyCheck = true
	for _, header in pairs(self.headers) do
		for _, member in pairs(header.members) do
			member.readyCheckIcon:SetAlpha(1)
			member.readyCheckIcon:SetTexture("")
			if member:IsVisible() then
				InvenRaidFrames3Member_UpdateReadyCheck(member)
			end
		end
	end
end