extends MarginContainer

@onready var shield_bar = $HBoxContainer/ShieldBar
@onready var score_counter = $HBoxContainer/ScoreCounter

var shader_active = false

func _ready():
    # get_node("Player.Ship").node_ready.connect("shield_ui", self, "_on_shield_ui")
    EventBus.shield_changed.connect(_on_shield_changed)
    EventBus.initialize_shieldbar.connect(_on_initialize_shieldbar)

func update_score(value):
    score_counter.display_digits(value)
    # var digits = score_counter.get_children()
    # var tween = create_tween()
    # tween.tween_property(digits,"scale",2,.15)


func _on_shield_changed(max_value: int, old_value: int, new_value: int) -> void:
    shield_bar.max_value = max_value
    shield_bar.value = old_value
    var tween = create_tween()
    print_debug("Tweening shield bar")
    tween.tween_property(shield_bar, "value", new_value, .25).set_trans(Tween.TRANS_LINEAR)
    _on_fx_shield_ui()


func _on_fx_shield_ui():
    shader_active = true
    shield_bar.material.set_shader_parameter("toggle", 1.0)
    await get_tree().create_timer(1).timeout
    shader_active = false
    shield_bar.material.set_shader_parameter("toggle", 0.0)

func _on_initialize_shieldbar():
    print("Shieldbar Initializing!")
    var tween = create_tween()
    tween.tween_property(shield_bar, "value", 100, .25).set_trans(Tween.TRANS_LINEAR)
    await tween.finished
    print_debug("Tweens played!")
