extends State
class_name BeeIdle

@onready var bee : Bee = get_parent().get_parent() as Bee # I think this is bad but I dont care it works

var idle_time : float = 0.0

var target : Vector2 = Vector2.ZERO

var acquire_new_target : bool = false


func enter(_msg : Dictionary = {}) -> void:
	if acquire_new_target:
		target = bee.get_global_position() + Vector2(randi_range(-60, 60), randi_range(-60, 60))

func exit() -> void: 
	acquire_new_target = true
	
func update(delta : float) -> void:

	if target == Vector2.ZERO:
		randomize()
		target = bee.get_global_position() + Vector2(randi_range(-60, 60), randi_range(-60, 60))


	idle_time += delta

	if idle_time > 2.0:
		find_something_to_do()
		idle_time = 0.0
	pass

func physics_update(delta : float) -> void:
	if target:
		if bee.position.distance_to(target) > 3:

			bee.velocity = (target - bee.position).normalized() * bee.speed / 2 * delta
			bee.move_and_collide(bee.velocity)
			bee.bee_body.look_at(target)

func find_something_to_do() -> void:
	if bee.nectar > 0:
		Log.pr("I have pollen, time to move..")
		## Bee has pollen - head home
		state_transition.emit(self, "Travelling")
	else:
		## Bee has no pollen - they should move to a flower
		state_transition.emit(self, "Travelling")
	pass
