extends Area2D

var enemy_type: String = "crt"

var bullet_scene = preload("res://scenes/enemy_bullet.tscn")
var start_pos = Vector2.ZERO
var move_break
var speed: int = 0
var direction = Vector2(0,1)
var tween_speed: float = 1

var exploding = false
var can_shoot = true
var vulnerable = true
var stunned = false
var blocking = false
var travel: bool = false

var shader_active = false

var enemy_move_aggression = randf_range(5, 20)
var enemy_shoot_aggression = randf_range(4, 20)

@onready var screensize  = get_viewport_rect().size
@onready var move_timer = $MoveTimer
@onready var crt = $CRT

func _ready():
  move_timer.start()

func start(pos):
    vulnerable = true
    speed = 4
    crt.frame = 0
    position = Vector2(pos.x, -pos.y)
    start_pos = pos
    await get_tree().create_timer(randf_range(0.25, 0.35)).timeout
    var tween = create_tween().set_trans(Tween.TRANS_BACK)
    tween.tween_property(self, "position:y", start_pos.y, tween_speed)
    await tween.finished
    travel = true
    # move_timer.wait_time = enemy_move_aggression
    move_timer.start()
    # $ShootTimer.wait_time = enemy_shoot_aggression
    $ShootTimer.start()

func _on_timer_timeout():
  speed = randf_range(75, 100)
  $DirectionTimer.start()
  
func _on_direction_timer_timeout():
  var move_direction = randi_range(0,2)
  match move_direction:
        0:
          crt.frame = 0
          direction = Vector2(0,1)
          can_shoot = true
          vulnerable = true
          #$DirectionTimer.start() 
        1:
          crt.frame = 1
          direction = Vector2(1,0)
          can_shoot = false
          vulnerable = false
          #$DirectionTimer.start() 
        2:
          crt.frame = 2
          direction = Vector2(-1,0)
          can_shoot = false
          vulnerable = false
          #$DirectionTimer.start() 
  


func _on_shoot_timer_timeout():
  if can_shoot == true:
      var b = bullet_scene.instantiate()
      $Pew.play()
      get_tree().root.add_child(b)
      b.start(position)
      #$ShootTimer.wait_time = randf_range(4, 20)
      $ShootTimer.start()
      
  else: 
      $ShootTimer.start()

func _process(delta):
    
    if stunned == true:
      return
    
    if stunned == false:  
      position += direction * speed * delta
          
    #if position.x > screensize.x + 32:
      #start(start_pos)
       
    if travel == true and position.y > screensize.y + 32 or position.x > screensize.x or position.x < -screensize.x:
        print("I'm off screen!")
        start(start_pos)
      

func hit_detection():
    if exploding: return
    
    if vulnerable == true:
      exploding = true
      EventBus.enemy_hit.emit(5)
      speed = 0
      $ImpactCRT.play()
      $AnimationPlayer.play("explode")
      #set_deferred("monitoring", false)
      await $AnimationPlayer.animation_finished
      queue_free()
      EventBus.enemy_died.emit()
      
    if vulnerable == false:
      block()
    
func block():
    #if blocking: return
    #blocking = true
    stunned = true
    $BlockCRT.play()
    print("CRT Stunned!")
    $DirectionTimer.stop()
    shader_active = true
    crt.material.set_shader_parameter("toggle", 1.0)
    await get_tree().create_timer(2).timeout
    crt.material.set_shader_parameter("toggle", 0.0)
    shader_active = false
    stunned = false
    $DirectionTimer.start()
    print("CRT Not Stunned!")
  
      
 
