-- Pre Show Venue


local scene = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")

local EventSequencer = require(ReplicatedStorage:WaitForChild("EventSequencer"))
local CountdownTimersModule = require(ReplicatedStorage:WaitForChild("ClientCountdownModule"))
local FullScreenModule = require(ReplicatedStorage:WaitForChild("FullscreenModule"))
local MerchBooth = require(ReplicatedStorage:WaitForChild("MerchBooth"))
local FriendsLocator = require(ReplicatedStorage:WaitForChild("FriendsLocator"))
local BouncePlatforms = require(ReplicatedStorage:WaitForChild("BouncePlatforms"))

local localPlayer = game:GetService("Players").LocalPlayer

local Schema = EventSequencer.createSchema()

local Environment = nil
local ServerEnvironment = nil
local timer = nil
local timePositionObject = nil
local screenPart = nil

function setFriendsLocatorEnabled(enabled)
	FriendsLocator.configure({
		showAllPlayers = RunService:IsStudio(), -- Shows all players when testing, but only friends when live.
		thresholdDistance = enabled and 100 or math.huge, -- Locators will not show up if
	})
end

function enableBouncyPads()
	for _, part in pairs(CollectionService:GetTagged("Bouncy")) do
		BouncePlatforms.new(part, {
			debounce = 0.2,
			bounceVector = Vector3.new(0, 1, 0),
			bounceForce = 150,
		})
	end
	BouncePlatforms:enable()
end

function getTimeLengthRemaining()
	-- We wait 5 seconds because of eventSequencer's roughly 5 second grace period to load a scene before orchestrating
	return scene:GetAttribute("TimeLength") - timePositionObject.Value - 5
end

function updateTimerDate()
	if timePositionObject and timer then
		timer.UniversalEventDate = DateTime.fromUnixTimestamp(DateTime.now().UnixTimestamp + getTimeLengthRemaining())
	end
end

Schema.OnSetup = function(timePositionObjectParam)
	timePositionObject = timePositionObjectParam
	Environment = EventSequencer.getCurrentSceneEnvironment()
	ServerEnvironment = EventSequencer.getCurrentServerEnvironmentFromClient()

	if not MerchBooth.isMerchBoothEnabled() then
		MerchBooth.setEnabled(true)
	end

	screenPart = ServerEnvironment:WaitForChild("Stage"):WaitForChild("Display"):WaitForChild("CountdownBoardPart")
	FullScreenModule.CreateButtonToFocusScreen(screenPart)

	setFriendsLocatorEnabled(true)
	
	enableBouncyPads()

	local countdownTimerGUI = localPlayer:WaitForChild("PlayerGui"):WaitForChild("CountdownTimerGUI")
	local bubbleTimerUI = countdownTimerGUI:WaitForChild("TopFrameUI"):WaitForChild("TimerBubbleUI")
	local boardTimerUI = screenPart:WaitForChild("SurfaceGui"):WaitForChild("TimerScreenUI")

	timer = CountdownTimersModule:AddTimer({
		Enabled = false,
		UniversalEventDate = DateTime.fromUnixTimestamp(DateTime.now().UnixTimestamp + getTimeLengthRemaining()),
		BubbleTimerUI = bubbleTimerUI,
		BoardTimerUI = boardTimerUI,
		DisableBubbleWhenBoardVisible = true,
	})
end

Schema.OnRun = function()
	if timer then
		updateTimerDate()
		timer.Enabled = true
	end

	local zoomInOnCountdown = Schema:schedule({ -- Force focus the screen for final countdown
		StartTimes = { scene:GetAttribute("TimeLength") - 10 },
		OnStart = function()
			FullScreenModule.RemoveButton()
			if screenPart and screenPart.Parent then
				FullScreenModule.FocusScreen(screenPart)
			end
		end,
	})
end

Schema.OnEndScene = function()
	-- Disable all of these scene specific things

	if timer then
		timer.Enabled = false
		CountdownTimersModule:RemoveTimer(timer)
		timer = nil
	end

	FullScreenModule.RemoveButton()
	FullScreenModule.FocusScreen() -- Return camera to character

	if MerchBooth.isMerchBoothEnabled() then
		MerchBooth.setEnabled(false)
	end

	setFriendsLocatorEnabled(false)
end

return Schema
