extends Node3D

signal cleared
signal activated

const CLEAR_THRESHOLD := 1500

@export var enabled := false

@onready var animation_player: AnimationPlayer = %AnimationPlayer
@onready var light_animation: AnimationPlayer = %LightAnimation
@onready var gunk_body: GunkBody = %GunkBody
@onready var interactive: Interactive = %Interactive


func _ready() -> void:
	if enabled:
		enable()


## Called once all gunk is cleared. Enables interaction & plays animations
func enable() -> void:
	enabled = true

	# Clear off the rest of the gunk
	gunk_body.clear_all()
	light_animation.play("success")
	interactive.enabled = true


## Called once the switch is no longer usable. Disables interaction & plays animations
func disable() -> void:
	if enabled:
		light_animation.queue("disable")

	enabled = false
	interactive.enabled = false


func _activate() -> void:
	animation_player.play("activate")
	activated.emit()
	# Disable while animation is playing
	interactive.enabled = false


func _animation_finished(anim_name: StringName) -> void:
	if anim_name == "activate":
		interactive.enabled = enabled


func _on_gunk_body_clear_total_updated(clear_total: float) -> void:
	if not enabled and clear_total >= CLEAR_THRESHOLD:
		enable()
