using Godot;
using System.Collections.Generic;

namespace Newlon.Components.Plants;

public partial class Eyesight : Area2D
{
    private bool _enemyDetected;
    public bool EnemyDetected => _enemyDetected;
    private readonly List<Entity> _detectedEntities = new List<Entity>();
    private RuntimePlantData _plantData;

    public override void _Ready()
    {
        _plantData = GetParent<RuntimePlantData>();
        AreaEntered += OnAreaEntered;
        AreaExited += OnAreaExited;
    }

    public void OnAreaEntered(Area2D area)
    {
        var entity = area.GetParent<Entity>();
        if (entity != null)
        {
            _detectedEntities.Add(entity);
        }

        _enemyDetected = _detectedEntities.Count > 0;
    }

    public void OnAreaExited(Area2D area)
    {
        var entity = area.GetParent<Entity>();
        if (entity != null)
        {
            if (_detectedEntities.Contains(entity))
            {
                _detectedEntities.Remove(entity);
            }
        }

        _enemyDetected = _detectedEntities.Count > 0;
    }
}
