extends State
class_name BeeGathering

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

var time_at_patch : float = 0.0

var target : Vector2 = Vector2.ZERO

func enter(_msg := {}):
	bee.just_gathering = true

	Log.pr("Gathering now...")

	randomize()
	target = bee.get_global_position() + Vector2(randi_range(-100, 100), randi_range(-100, 100))

func update(_delta : float):	

	if bee.in_range_of_flowers:
		#animator.play("Gathering")
		time_at_patch += _delta
		if time_at_patch > 5.0:
			Log.pr("Gathered nectar!")
			bee.nectar += GameState.flower_nectar_level # Add nectar to the bee based on current flower nectar level
			state_transition.emit(self, "Idle")
	else:
		state_transition.emit(self, "Idle")

func physics_update(delta : float) -> void:
	if target:
		if bee.position.distance_to(target) > 2:
			bee.velocity = (target - bee.position).normalized() * bee.speed / 2 * delta
			bee.move_and_collide(bee.velocity)
			bee.bee_body.look_at(target)
