extends Node2D

var wave_count: int = 0
var enemy_count: int = 0
var enemy = null

@export var wave_complete: bool = false

@onready var screensize = get_viewport_rect().size

const COLS: int = 5
const ROWS: int = 3

const CHICKEN = preload("res://scenes/enemy_chicken.tscn")
const CRT = preload("res://scenes/enemy_crt.tscn")
const MIRROR = preload("res://scenes/enemy_mirror.tscn")
const SHARDS = preload("res://scenes/enemy_shards.tscn")
const TABLE = preload("res://scenes/enemy_table.tscn")
const BOSS = preload("res://scenes/boss.tscn")

func _ready():
  wave_count = 0
  EventBus.enemy_died.connect(_on_enemy_died)


func spawn_enemies():
  
    if wave_count == 0: #This is for CHICKENS
      print("Enemy Wave: ", wave_count)
      enemy = CHICKEN
      for x in range(COLS):
        for y in range(ROWS):
              var e = enemy.instantiate()  
              var pos = Vector2(x * (28 + 4) + 56, 28 * 2 + y * 38)
              add_child(e)
              e.start(pos)
              enemy_count = ROWS * COLS
              await get_tree().process_frame
              print_debug("Remaining enemies: ", enemy_count, ", ", get_tree().get_nodes_in_group("enemies").size())
              
    if wave_count == 1: #This is for CRTs
      print("Enemy Wave: ", wave_count)
      enemy = CRT
      var cols_mod = COLS - 1
      var rows_mod = ROWS - 1 
      for x in range(cols_mod):
        for y in range(rows_mod):
              var pos_mod = int (randf_range(-42,32))
              var e = enemy.instantiate()  
              var pos = Vector2(x * (36 + 12) + 56, 28 * 2 + y * 38)
              add_child(e)
              e.start(pos + Vector2(pos_mod, y))
              enemy_count = rows_mod * cols_mod
              await get_tree().process_frame
              
    if wave_count == 2: #This if for Mirror
      print("Enemy Wave: ", wave_count)
      
      # Spawn Tablea
      EventBus.flash_screen.emit(.15)
      var t = TABLE.instantiate()
      add_child(t)
      t.position = Vector2(screensize.x / 2, position.y + 125)
      #await get_tree().create_timer(1.5).timeout
      var ts = t.get_child(2)
      await get_tree().create_timer(.9).timeout
      ts.play()
      ts = t.get_child(0)
      ts.frame = 1
      EventBus.flash_screen.emit(.25)
      await get_tree().create_timer(1.5).timeout
      
      # Spawn Mirror
      enemy = MIRROR
      enemy_count = 1
      var e = enemy.instantiate()
      add_child(e)
      e.position = Vector2(screensize.x / 2, position.y + 48)
      
      # Spawn Shards
      t.queue_free()
      var s = SHARDS.instantiate()
      add_child(s)
      s.position = Vector2(screensize.x / 2, position.y + 125)
      s.show()
      
      # Start Shards
      get_tree().create_timer(2)
      s.start_all_animations()
  
      
    if wave_count == 3   : #This is for Boss
     
    # Hide the player 
     
    # Speed up parallax
      EventBus.cloud_speed.emit(true)
    
     # Spawn Boss
      await get_tree().create_timer(3).timeout
      #EventBus.flash_screen.emit(.25)
      var b = BOSS.instantiate()
      add_child(b)
      #b.position = Vector2(screensize.x / 2, position.y + 100)
      
      
            
    

func _on_enemy_died():
    enemy_count -= 1
    wave_status()
    
       
func update_enemy_aggression(low,high):
  var nodes = get_tree().get_nodes_in_group("enemies")
  for node in nodes:
   if node is Area2D:
     node.enemy_move_aggression = randf_range(low,high)
     node.enemy_shoot_aggression = randf_range(low,high)
     node.tween_speed = min(low,high) - .75
    
    
func wave_status():
      if enemy_count > 0:
        #enemy_aggression_check()
        wave_complete = false
      
      else:
        await get_tree().create_timer(3).timeout
        EventBus.goose_talk.emit(wave_count, 4)
        wave_initialize()
        wave_complete = true
        
func wave_initialize():
    await get_tree().create_timer(2).timeout
    wave_count += 1
    spawn_enemies()
    
func enemy_aggression_check():
  if enemy_count == ROWS * COLS / 2:
        print_debug("Enemy count is 50%")
        update_enemy_aggression(2,10)
        EventBus.goose_talk.emit(0)
  if enemy_count == ROWS * COLS / 4:
        print_debug("Enemy count is 25%")
        update_enemy_aggression(1,1)
  
  
 
  
      
        
