class_name GunkAlarm extends SignalNode
## Raises the grunk alert when triggered.

const ALERT_DELTA := 1

## Time to wait for a pulse signal before triggering.
@export var pulse_timeout := 6.0

var _busy := false

@onready var mesh_instance_3d: MeshInstance3D = %MeshInstance3D
@onready var animation_player: AnimationPlayer = %AnimationPlayer
@onready var pulse_listener_timer: Timer = %PulseListenerTimer


## Trigger this alarm.
##
## Will not trigger if this alarm is busy (e.g. on cooldown)
func trigger() -> void:
	if not _busy:
		_busy = true
		Game.manager.raise_alert(ALERT_DELTA)
		animation_player.play("trigger")


## Pulse this alarm, resetting the pulse countdown until triggering.
##
## Note that alarms will not begin the pulse countdown until receiving the first pulse.
## On networks without a heart, the pulse timeout will be completely ignored.
func pulse() -> void:
	pulse_listener_timer.start(pulse_timeout)


func _process(delta: float) -> void:
	super._process(delta)
	# TODO actual model & animation
	var material: StandardMaterial3D = mesh_instance_3d.mesh.surface_get_material(0)
	var value := 1.0 - pct_damage()
	material.albedo_color = Color(value, value, value)

	# TODO pulse animation


func _on_animation_finished(anim_name: StringName) -> void:
	if anim_name == "trigger":
		_busy = false
