extends Node2D

@export var tutorial_cutscene : TutorialCutscenes
@export var interactable_system : InteractableSystem
@onready var area_2d: Area2D = $Area2D
@onready var timer: Timer = $Timer

var was_system_activated_yet : bool = false
var was_door_passed_yet : bool = false

func _ready() -> void:
	if not interactable_system:
		push_error("No interactable system assigned!")
		assert(false)
	if not tutorial_cutscene:
		push_error("No tutorial cutscene assigned!")
		assert(false)
	
	interactable_system.state_changed.connect(start_waiting)

func start_waiting(system_active : bool):
	if was_system_activated_yet or Globals.skip_cutscenes:
		return
	
	if system_active:
		was_system_activated_yet = true
		timer.start(10)

func _on_area_2d_body_entered(body: Node2D) -> void:
	print(body.name)
	if body is PlayerBase:
		was_door_passed_yet = true

func _on_timer_timeout() -> void:
	if was_door_passed_yet or Globals.skip_cutscenes or Globals.remnant_cutscene_done:
		Globals.remnant_cutscene_done = true
		return
	tutorial_cutscene.show()
	tutorial_cutscene.start_dialogue()
	Globals.remnant_cutscene_done = true
