using System.Threading;
using Cysharp.Threading.Tasks;

namespace QuizPleaseTest.Boot.Flow
{
    public class MenuRestartSignal : IMenuRestartSignal
    {
        private UniTaskCompletionSource _restartCompletionSource;

        public UniTask WaitAsync(CancellationToken ct)
        {
            ct.ThrowIfCancellationRequested();

            _restartCompletionSource = new UniTaskCompletionSource();
            return _restartCompletionSource.Task.AttachExternalCancellation(ct);
        }

        public void RequestRestart()
        {
            UniTaskCompletionSource restartCompletionSource = _restartCompletionSource;
            _restartCompletionSource = null;
            restartCompletionSource?.TrySetResult();
        }
    }
}
