extends Area2D

var goose_talking: bool = false

func _ready() -> void:
  EventBus.goose_talk.connect(_on_goose_talk)


func _on_goose_talk(frame_index: int, quack_wait_time: float = 3.0):
  
  #var quacking = get_tree().create_timer(3).timeout
  
  $DapperGoose/Quacking.wait_time = quack_wait_time
  
  if goose_talking == false:    
    print("QUACK")
    self.show()
    $DapperGoose/Bubble.set_frame(frame_index)
    var tween = create_tween()
    tween.tween_property(self, "position:x",  position.x - 50, .5).set_ease(tween.EASE_OUT)
    await tween.finished
    $DapperGoose/Bubble.show()
    goose_talking = true 
    $DapperGoose/Quacking.start()
    await $DapperGoose/Quacking.timeout
    $DapperGoose/Bubble.hide()
    goose_talking = false
    tween = create_tween()
    tween.tween_property(self, "position:x",  position.x + 50, .5).set_ease(tween.EASE_IN)
    await tween.finished
    self.hide()
    
    if goose_talking == true:
      print("I'm interrupting")
      $DapperGoose/Quacking.stop()
      $DapperGosse/Bubble.set_frame(frame_index)
      $DapperGoose/Quacking.start()
      await $DapperGoose/Quacking.timeout
      $DapperGoose/Bubble.hide()
      tween = create_tween()
      tween.tween_property(self, "position:x",  position.x + 50, .5).set_ease(tween.EASE_IN)
      await tween.finished
      goose_talking = false
      self.hide()
