using System.Threading.Tasks;
using Cirno.Scripts.Activables;
using Godot;

namespace Cirno.Scripts.Actors;

public partial class AlarmTeleporterActorSpawner : ActorSpawner
{
    private AlarmManager _alarmManager;
    
    [Export]
    public Teleporter Teleporter { get; private set; }
    
    [Export]
    public float ActivationRange { get; private set; }

    [Export] public float ActivationDelay { get; private set; } = 2f;

    public override void _Ready()
    {
        _alarmManager = this.GetAlarmManager();
        _alarmManager.AlarmEnabled += AlarmManagerOnAlarmEnabled;
    }

    private void AlarmManagerOnAlarmEnabled(Vector2 location)
    {
        if (!(location.DistanceTo(this.GlobalPosition) <= ActivationRange)) return;

        _ = DelaySpawn();
    }

    private async Task DelaySpawn()
    {
        await Task.Delay((int)(1000 * ActivationDelay));
        Teleporter?.FireParticles();
        Spawn();
    }
}