-- << LOCAL FUNCTIONS >>

local function updateNotices(newNotice)
	for i,notice in pairs(currentNotices) do
		if not notice.Complete.Value then
			local tweenInfo = tweenInfoUp
			local destination
			if i == 1 then
				destination = template.Position + UDim2.new(0,-notice.Size.X.Offset,0,0)
			else
				local noticeBefore = currentNotices[i-1]
				destination = UDim2.new(notice.Position.X.Scale, notice.Position.X.Offset, notice.Position.Y.Scale, template.Position.Y.Offset-((i-1)*(template.Size.Y.Offset+5)))
			end
			if destination.Y.Offset > notice.Position.Y.Offset then
				tweenInfo = tweenInfoDown
			end
			if destination then
				main.tweenService:Create(notice, tweenInfo, {Position = destination}):Play()
			end
		end
	end
end

local function endNotice(notice)
	if notice:FindFirstChild("Complete") and not notice.Complete.Value then
		notice.Complete.Value = true
		for i,v in pairs(currentNotices) do
			if v == notice then
				table.remove(currentNotices, i)
				local tween = main.tweenService:Create(notice, tweenInfoEnd, {Position = notice.Position + UDim2.new(0,(notice.Size.X.Offset-XOffset),0,0)})
				tween:Play()
				tween.Completed:Wait()
				notice:Destroy()
			end
		end
	end
	updateNotices()
end

local function Queue()
	if noticeDe then
		noticeDe = false
		repeat wait()
			if #noticeQueue > 0 then
				local notice = noticeQueue[1]
				notice.Parent = notices
				if notice.NoticeType.Value == "Error" then
					main.audio.Error:Play()
				else
					main.audio.Notice:Play()
				end
				table.insert(currentNotices, 1, notice)
				--
				if notice.Countdown.Value < 900 then
					spawn(function()
						if notice.Countdown.Value > 0 then
							local alignTime = os.time()
							repeat wait() until os.time() ~= alignTime
							local originalDesc = notice.Desc.Text
							for i = 1, notice.Countdown.Value do
								if not notice:FindFirstChild("Desc") or notice.Desc.Text == "Teleporting..." then
									break
								end
								notice.Desc.Text = originalDesc.." ("..notice.Countdown.Value+1-i..")"
								wait(1)
							end
						else
							local startTick = tick()
							repeat wait(0.1) until tick() - startTick >= maxTime
						end
						endNotice(notice)
					end)
				end
				--
				updateNotices(true)
				--
				wait(waitTime)
				table.remove(noticeQueue,1)
			end
		until #noticeQueue < 1
		noticeDe = true
	end
end

