extends AnimatableBody2D

enum Reward { COIN, APPLE, PEAR, GRAPES }
@export var reward: Reward = Reward.COIN

const REWARD_SCENES = {
	Reward.COIN: "res://scenes/coin.tscn",
	Reward.APPLE: "res://scenes/apple.tscn",
	Reward.PEAR: "res://scenes/pear.tscn",
	Reward.GRAPES: "res://scenes/grapes.tscn",
}

@onready var sound_player: AnimationPlayer = $SoundPlayer
@onready var reward_collision: CollisionShape2D = $RewardZone/RewardCollision
@onready var reward_zone: Area2D = $RewardZone

func _ready() -> void:
	reward_zone.body_entered.connect(_on_rewardzone_body_entered)

func _on_rewardzone_body_entered(body: Node) -> void:
	if body.is_in_group("player"):
		sound_player.play("reward")
		reward_collision.set_deferred("disabled", true)
		call_deferred("_spawn_reward")

func _spawn_reward() -> void:
	var reward_scene = load(REWARD_SCENES[reward])
	var reward_node = reward_scene.instantiate()
	get_parent().add_child(reward_node)
	reward_node.global_position = global_position + Vector2(0, -16)
	if "game_manager" in reward_node:
		reward_node.game_manager = get_tree().get_first_node_in_group("game_manager")
	if reward_node.has_method("popup"):
		reward_node.popup()
