-- Events

ClientOrchestration.ClientSceneMetadataEvent = Orchestra.Events.ClientEvents.ClientSceneMetadata
ClientOrchestration.SendMediaControllerConfig = Orchestra.Events.MediaControllerConfigEvents.SendMediaControllerConfig
ClientOrchestration.Seek = Orchestra.Events.ClientEvents.Seek
ClientOrchestration.Pause = Orchestra.Events.ClientEvents.Pause
ClientOrchestration.SceneEnded = Orchestra.Events.ClientEvents.SceneEnded
ClientOrchestration.SeekPermissionsEvent = Orchestra.Events.ClientEvents.SeekPermissions

ClientOrchestration.SeekerGuiComponent = nil

ClientOrchestration.playerGui = Players.LocalPlayer:WaitForChild("PlayerGui")

ClientOrchestration.taskLib = task
ClientOrchestration._warn = function(msg)
	warn(msg)
end

ClientOrchestration._require = function(file)
	return require(file)
end

ClientOrchestration.handleSeekPermissionsEvent = function(hasPermission)
	if hasPermission and not ClientOrchestration.SeekerGuiComponent then
		local SeekerGuiElement = ClientOrchestration.Roact.createElement(ClientOrchestration.SeekerGui)

		ClientOrchestration.SeekerGuiComponent = ClientOrchestration.Roact.mount(
			SeekerGuiElement,
			ClientOrchestration.playerGui,
			"SceneSeeker"
		)
	end

	if not hasPermission and ClientOrchestration.SeekerGuiComponent then
		if ClientOrchestration.SeekerGuiComponent then
			ClientOrchestration.Roact.unmount(ClientOrchestration.SeekerGuiComponent)
			ClientOrchestration.SeekerGuiComponent = nil
		end
	end
end

ClientOrchestration.ClientSceneMetadataRequestConnection = nil

ClientOrchestration.setupSchema = function()
	ClientOrchestration.isSeekable = false
	local lastCalledOrchestrationTime = ClientOrchestration.lastCalledOrchestrationTime

	local setupSuccess, setupError = ClientOrchestration.SchemaProcessorInstance:Setup()

	local isSameScene = lastCalledOrchestrationTime == ClientOrchestration.lastCalledOrchestrationTime

	if not setupSuccess then
		ClientOrchestration._warn("Client Schema Setup wasn't successful")
		ClientOrchestration._warn(setupError)
	elseif isSameScene then
		ClientOrchestration.isSeekable = true
	end
	return setupSuccess
end

ClientOrchestration.processSchema = function(isSeeking)
	local processSuccess, processError = ClientOrchestration.SchemaProcessorInstance:Process(isSeeking)

	if not processSuccess then
		ClientOrchestration._warn("Client Schema Process wasn't successful")
		ClientOrchestration._warn(processError)
	end
end

ClientOrchestration.cleanUp = function()
	if ClientOrchestration.SchemaProcessorInstance then
		ClientOrchestration.SchemaProcessorInstance:CleanUp()
	end
end

ClientOrchestration.handleSceneEnded = function()
	if ClientOrchestration.SchemaProcessorInstance then
		local endSceneSuccess, endSceneError = ClientOrchestration.SchemaProcessorInstance:EndScene()
		if not endSceneSuccess then
			ClientOrchestration._warn("Client End Scene wasn't successful")
			ClientOrchestration._warn(endSceneError)
		end
		ClientOrchestration.cleanUp()
	end
end
