extends Node2D

onready var buttonPlayer = $"%ButtonPlayer"
onready var activatorArea = $"%ActivatorArea"
onready var indicatorPlayer = $"%IndicatorPlayer"
onready var elevator = get_node("./Portal")
onready var signalManager := get_tree().root.get_child(3).get_node("%SignalManager")
onready var unactivatable_timer := $Timer
onready var get_back_timer := $GetBackTimer

export var elevator_time := 30
var activatable = false

func _ready() -> void:
	get_back_timer.wait_time = elevator_time
	elevator.get_node("./portal").frame = 1
	elevator.monitoring = false

func _process(delta):
	if activatable && Input.is_action_just_released("interact"):
		selfActivate()
		signalManager.emit_signal("terminal_activated", elevator_time)
		get_back_timer.start()


func selfActivate():
	indicatorPlayer.play("onning")
	buttonPlayer.play("pushing")
	elevator.get_node("./portal").frame = 0
	elevator.monitoring = true
	#TODO dis importante
	activatorArea.set_deferred("monitoring", false)


func _on_ActivatorArea_area_entered(area:Area2D) -> void:
		$Label.visible = true
		$Label.text = " [e] \n Time: " + String(elevator_time) + " sec"
		activatable = true

func _on_ActivatorArea_area_exited(area:Area2D) -> void:
		unactivatable_timer.start()

func _on_Timer_timeout():
	$Label.visible = false
	activatable = false


func _on_GetBackTimer_timeout() -> void:
	signalManager.emit_signal("getback_timer_up")
