using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using BITKit.Core.Entites;
// ReSharper disable All

namespace BITKit;
/// <summary>
/// 基于Godot.Node的IEntitiesService实现
/// </summary>
public partial class GodotEntitiesService : Node,IEntitiesService
{
    public GodotEntitiesService()
    {
        DI.Register<IEntitiesService>(this);
    }
    private readonly Dictionary<ulong,IEntity> _entities=new ();
    private CancellationTokenSource _cancellationTokenSource;
    public IEntity[] Entities => _entities.Values.ToArray();
    public bool Register(IEntity entity)
    {
        return _entities.TryAdd(entity.Id, entity);
    }
    public bool UnRegister(IEntity entity)
    {
        return _entities.TryRemove(entity.Id);
    }

    public override void _Ready()
    {
        _cancellationTokenSource = new();
    }

    protected override void Dispose(bool disposing)
    {
        if(disposing)_cancellationTokenSource.Cancel();
    }

    public CancellationToken CancellationToken => _cancellationTokenSource.Token;
}
