extends Area2D

var enemy_type: String = "boss"

var bullet_scene = preload("res://scenes/enemy_bullet.tscn")
var start_pos = Vector2.ZERO
var speed = 0
var tween_speed: float = 1.4
var pause: bool = false


var horse_counter: int = 0
var horse_phase: bool = true
var exploding:bool = false 
var heart_taken: bool = false
var heart_broken: bool = false

@onready var boss = $AnimationPlayer
@onready var heart = $Boss/Heart/AnimationPlayer
@onready var screensize  = get_viewport_rect().size

func _ready():
  EventBus.ending.connect(_on_ending)
  boss_intro()
  $Heart.monitoring = false
  $Heart.monitorable = false
  
func _process(_delta: float) -> void:
  if pause == true: return

    
func boss_intro():
  # Move boss into screen
  position = Vector2(screensize.x / 2, -screensize.y)
  var tween = create_tween().set_ease(Tween.EASE_OUT)
  tween.tween_property(self, "position:y",  100, 2)
  await tween.finished
  
  # Introduce Lady of Whispers
  print("Boss intro!")
  await get_tree().create_timer(2).timeout
  boss.play("align")
  await get_tree().create_timer(2).timeout
  boss.play("rise")
  await get_tree().create_timer(4).timeout
  boss.play("whisper")
  await get_tree().create_timer(1).timeout
  
  # Check to see if horse phase is available 
  horse_phase_check()
  
 
func horse_phase_check():
  if horse_phase == true: boss_horse()
  if horse_phase == false: 
    print("No more horse!")
    $Sprite2D.frame = 0
  if horse_phase == false and heart_broken == true:
    $Sprite2D.frame = 4
  
func boss_horse():
  
  if heart_taken == false:
    print("Boss horse!")
    await get_tree().create_timer(2).timeout
    EventBus.flash_screen.emit(.15)
    #if heart_taken == true: return
    #$Heart.hide()
    #heart.stop()
    #$Heart.monitoring = false
    $Sprite2D.frame = 7
    $Horse.show()
    $Horse.start()
    var horse_player = $Horse/AnimationPlayer
    horse_player.play("run")
    await get_tree().create_timer(10).timeout
    EventBus.flash_screen.emit(.15)
    $Horse.hide()
    $Horse.end()
    await get_tree().create_timer(5).timeout
    horse_counter += 1
    lady_of_whispers()
    
  if heart_taken == true or heart_broken == true:
    horse_phase = false
    horse_phase_check()
  

func lady_of_whispers():
  
  if horse_counter == 2:
    offer_heart()
    
  
  if horse_counter <= 1:
    print("Lady of Whispers!")
    boss.play("rise")
    await get_tree().create_timer(4).timeout
    boss.play("whisper")
    await boss.animation_finished
    await get_tree().create_timer(1).timeout
    horse_phase_check()
  
func hit_detection():
  return
  
func offer_heart():
  print("Lady of Whispers offers you a heart!")
  boss.play("rise")
  await get_tree().create_timer(4).timeout
  $Heart/AnimationPlayer.play("beat")
  $Heart.show()
  $Heart.monitoring = true
  $Heart.monitorable = true
  heart_check()
  
func heart_check():
  print("Heart check!")
  await get_tree().create_timer(5).timeout
  if heart_taken == false:
    await get_tree().create_timer(1).timeout
    horse_counter = 0
    print("Horse Counter:",horse_counter)
    $Heart/AnimationPlayer.stop()
    $Heart.hide()
    $Heart.monitoring = false
    $Heart.monitorable = false
    
  if heart_taken == true or heart_broken == true:
    #$Heart.monitoring = false
    #$Heart.monitorable = false
    horse_phase = false
    
  horse_phase_check()
    
  
  
func _on_ending(value: int): 
  if value == 1:
    heart_taken = true
    horse_phase = false
    #horse_phase_check()
    print("Fix your broken heart or die")
    $Sprite2D.frame = 0
    EventBus.goose_talk.emit(3,6)
    
    
    
    
    
    #var tween = create_tween()
    #tween.tween_property($Player, "position:y",  180, 2)#.set_ease(Tween.EASE_OUT)
    #tween.tween_property($Player, "position:y",  60 * -1, .25)#.set_ease(Tween.EASE_IN)
    #await get_tree().create_timer(8).timeout
    #EventBus.goose_talk.emit(3)
    #self.monitoring = false
    #self.monitorable = false 
    
    
  if value == 2: 
    heart_broken = true
    horse_phase = false
    horse_phase_check()
    $Heart.monitoring = false
    $Heart.monitorable = false
    print("Some hearts are too wild.")
    EventBus.heart_beat.emit(0)
    await get_tree().create_timer(2).timeout 
    EventBus.flash_screen.emit(.25)
    #$Heart.queue_free()
    $Sprite2D.frame = 4
    await get_tree().create_timer(4).timeout
    EventBus.goose_talk.emit(4,6)
    self.monitoring = false
    self.monitorable = false 
    EventBus.win_game.emit(2)
    
  
 
