using Godot;
using System;

public partial class Interactable : Node2D
{
	[Signal]
	public delegate void PlayerNearByEventHandler(Player player);
	[Signal]
	public delegate void PlayerLeftEventHandler(Player player);
	
	[Export] public Vector2 SpriteOffset;
	[Export] public float AreaRadius;

	private AnimatedSprite2D _sprite;

	private CollisionShape2D _areaMesh;

	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		_sprite = (AnimatedSprite2D)FindChild("AnimatedSprite2D");
		_areaMesh = (CollisionShape2D)FindChild("CollisionShape2D");
		_sprite.Position += SpriteOffset;
		((CircleShape2D)_areaMesh.Shape).Radius = AreaRadius;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}

	private void _on_area_2d_body_entered(Node2D body)
	{
		if (body is Player player)
		{
			_sprite.Visible = true;
			EmitSignal(SignalName.PlayerNearBy, player);
		}
	}

	private void _on_area_2d_body_exited(Node2D body)
	{
		if (body is Player player)
		{
			_sprite.Visible = false;
			EmitSignal(SignalName.PlayerLeft, player);
		}
	}
}
