using NB.Authentication;
using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas;
using Fantasy.Network;
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract

namespace NB;

public static class EntityHelper
{
    public static bool CheckInterval(this Entity entity, int interval)
    {
        var sessionTimeOutComponent = entity.GetComponent<EntityTimeOutComponent>();

        if (sessionTimeOutComponent == null)
        {
            sessionTimeOutComponent = entity.AddComponent<EntityTimeOutComponent>();
            sessionTimeOutComponent.SetInterval(interval);
            return true;
        }

        return sessionTimeOutComponent.CheckInterval();
    }

    public static void SetTimeout(this Entity entity, int timeout = 3000, Func<FTask>? task = null)
    {
        var sessionTimeOutComponent = entity.GetComponent<EntityTimeOutComponent>();
        
        if (sessionTimeOutComponent == null)
        {
            sessionTimeOutComponent = entity.AddComponent<EntityTimeOutComponent>();
        }

        sessionTimeOutComponent.TimeOut(timeout, task);
    }

    public static bool IsTimeOutComponent(this Entity entity)
    {
        return entity.GetComponent<EntityTimeOutComponent>() != null;
    }

    public static void CancelTimeout(this Entity entity)
    {
        entity.RemoveComponent<EntityTimeOutComponent>();
    }
}