extends Node2D

onready var activatorArea = $"%ActivatorArea"
onready var signal_manager := get_tree().root.get_child(4).get_node("%SignalManager")
onready var level_state := get_tree().root.get_child(4).get_node("%LevelState")
onready var blobby := $"../%Blobby"
onready var unactivatable_timer := $Timer
export var cost := 3

var activatable = false

func _ready():
	$Sprite.frame = 0

func _process(delta):
	# TODO Global currency count?? Maybe just level intern currency
	if activatable && Input.is_action_just_released("interact"):
		if(level_state.spend_currency(cost)):
			signal_manager.emit_signal("currency_updated")
			selfActivate()

func selfActivate():
	#TODO Is a event for blobby himself
	#blobby.get_node("BubbleShieldViewport/IridescenceBall").visible = true
	signal_manager.emit_signal("power_up_collected", "shield")
	$AudioStreamPlayer.play()
	#TODO dis importante
	activatorArea.set_deferred("monitoring", false)


func _on_ActivatorArea_area_entered(area:Area2D) -> void:
		$Label.visible = true
		$Label.text = "Orbs in Wallet: " + str(level_state.check_balance())
		$Highlight.visible = true
		activatable = true

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

func _on_Timer_timeout():
	if($ActivatorArea.get_overlapping_bodies().size() == 0):
		$Highlight.visible = false
		$Label.visible = false
		activatable = false
		$Sprite.frame = 1
