extends CanvasLayer

@onready var fade_rect: ColorRect = $FadeRect
@onready var end_label: Label = $EndLabel

@export var main_menu_scene: String = "res://scenes/start.tscn"

var ending_active := false
var ending_ended := false


func start_ending():
	ending_active = true
	ending_ended = false

	# Stop player input + show mouse
	Input.mouse_mode = Input.MOUSE_MODE_VISIBLE

	# Fade reset
	fade_rect.modulate.a = 0.0
	end_label.modulate.a = 0.0

	# Fade screen to white, then text
	var tween = create_tween()
	tween.tween_property(fade_rect, "modulate:a", 1.0, 2.0)
	tween.tween_interval(0.5)
	tween.tween_property(end_label, "modulate:a", 1.0, 2.0)
	
	await get_tree().create_timer(4.0).timeout
	ending_ended = true


func _input(event):
	if not ending_active:
		return
	
	if not ending_ended:
		return

	# Only allow click after ending begins
	if event is InputEventMouseButton and event.pressed:
		get_tree().change_scene_to_file(main_menu_scene)
