// ---------------------------------------------------------------------------
// BT.Framework
// CoroutineHandle.cs — CoroutineRunner 반환 핸들
// ---------------------------------------------------------------------------
using System;

namespace BurningTimes.Core.Coroutine
{
    /// <summary>
    /// <see cref="CoroutineRunner"/>에서 발급하는 코루틴 핸들.
    /// </summary>
    /// <remarks>
    /// ID 기반 참조값이며 값 타입이다. <see cref="None"/>은 유효하지 않은 핸들을 의미한다.
    /// 핸들을 보관한 쪽에서 중단·일시정지·재개 등을 요청할 때 사용한다.
    /// </remarks>
    public readonly struct CoroutineHandle : IEquatable<CoroutineHandle>
    {
        /// <summary>유효하지 않은 핸들.</summary>
        public static readonly CoroutineHandle None = new CoroutineHandle(0);

        /// <summary>내부 식별 ID. 0은 유효하지 않음.</summary>
        public readonly ulong Id;

        internal CoroutineHandle(ulong id) { Id = id; }

        /// <summary>핸들이 유효한지 여부(ID가 0이 아닌지).</summary>
        public bool IsValid => Id != 0;

        public bool Equals(CoroutineHandle other) => Id == other.Id;
        public override bool Equals(object obj) => obj is CoroutineHandle h && Equals(h);
        public override int GetHashCode() => Id.GetHashCode();
        public static bool operator ==(CoroutineHandle a, CoroutineHandle b) => a.Id == b.Id;
        public static bool operator !=(CoroutineHandle a, CoroutineHandle b) => a.Id != b.Id;
        public override string ToString() => IsValid ? $"Coroutine#{Id}" : "Coroutine#None";
    }
}
