// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;

namespace SampleTypeSpec
{
    internal partial class SampleTypeSpecClientListWithNextLinkAsyncCollectionResult : AsyncCollectionResult
    {
        private readonly SampleTypeSpecClient _client;
        private readonly Uri _nextPage;
        private readonly RequestOptions _options;

        /// <summary> Initializes a new instance of SampleTypeSpecClientListWithNextLinkAsyncCollectionResult, which is used to iterate over the pages of a collection. </summary>
        /// <param name="client"> The SampleTypeSpecClient client used to send requests. </param>
        /// <param name="nextPage"> The url of the next page of responses. </param>
        /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
        public SampleTypeSpecClientListWithNextLinkAsyncCollectionResult(SampleTypeSpecClient client, Uri nextPage, RequestOptions options)
        {
            _client = client;
            _nextPage = nextPage;
            _options = options;
        }

        /// <summary> Gets the raw pages of the collection. </summary>
        /// <returns> The raw pages of the collection. </returns>
        public override async IAsyncEnumerable<ClientResult> GetRawPagesAsync()
        {
            PipelineMessage message = _client.CreateListWithNextLinkRequest(_nextPage, _options);
            Uri nextPageUri = null;
            while (true)
            {
                ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false));
                yield return result;

                nextPageUri = ((ListWithNextLinkResponse)result).Next;
                if (nextPageUri == null)
                {
                    yield break;
                }
                message = _client.CreateListWithNextLinkRequest(nextPageUri, _options);
            }
        }

        /// <summary> Gets the continuation token from the specified page. </summary>
        /// <param name="page"></param>
        /// <returns> The continuation token for the specified page. </returns>
        public override ContinuationToken GetContinuationToken(ClientResult page)
        {
            Uri nextPage = ((ListWithNextLinkResponse)page).Next;
            if (nextPage != null)
            {
                return ContinuationToken.FromBytes(BinaryData.FromString(nextPage.AbsoluteUri));
            }
            else
            {
                return null;
            }
        }
    }
}
