extends Control

@export var animation: AnimationPlayer = null
@export var pause_sound: AudioStream = null
@export var unpause_sound: AudioStream = null
@export var pause_delay: float = 0.2

var is_pause_enable: bool = false

var _paused: bool = false
var _pause_timer: float = 0.0


func _ready() -> void:
	hide()


func _physics_process(delta: float) -> void:
	_pause_timer += delta
	if not is_pause_enable:
		if _paused:
			_paused = false
			hide()
			get_tree().paused = false
		return
	if Input.is_action_just_pressed("start") or Input.is_action_just_pressed("pc_pause"):
		if _pause_timer < pause_delay:
			return
		_pause_timer = 0.0
		_paused = not _paused
		visible = _paused
		if _paused:
			animation.seek(0)
			get_tree().paused = true
			PulseSound1.load_and_play(pause_sound)
		else:
			PulseSound1.load_and_play(unpause_sound)
			await PulseSound1.finished
			get_tree().paused = false


func pause_enable() -> void:
	is_pause_enable = true


func pause_disable() -> void:
	is_pause_enable = false
