// 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;
using System.Threading.Tasks;

namespace SampleTypeSpec
{
    internal partial class SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT : AsyncCollectionResult<Thing>
    {
        private readonly SampleTypeSpecClient _client;
        private readonly string _token;
        private readonly RequestOptions _options;

        /// <summary> Initializes a new instance of SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT, 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="token"></param>
        /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
        public SampleTypeSpecClientGetWithContinuationTokenHeaderResponseAsyncCollectionResultOfT(SampleTypeSpecClient client, string token, RequestOptions options)
        {
            _client = client;
            _token = token;
            _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.CreateGetWithContinuationTokenHeaderResponseRequest(_token, _options);
            string nextToken = null;
            while (true)
            {
                ClientResult result = await GetNextResponseAsync(message).ConfigureAwait(false);
                yield return result;

                if (result.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value))
                {
                    nextToken = value;
                }
                else
                {
                    yield break;
                }
                message = _client.CreateGetWithContinuationTokenHeaderResponseRequest(nextToken, _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)
        {
            if (page.GetRawResponse().Headers.TryGetValue("next-token", out string value) && !string.IsNullOrEmpty(value))
            {
                return ContinuationToken.FromBytes(BinaryData.FromString(value));
            }
            else
            {
                return null;
            }
        }

        /// <summary> Gets the values from the specified page. </summary>
        /// <param name="page"></param>
        /// <returns> The values from the specified page. </returns>
        protected override async IAsyncEnumerable<Thing> GetValuesFromPageAsync(ClientResult page)
        {
            foreach (Thing item in ((ListWithContinuationTokenHeaderResponseResponse)page).Things)
            {
                yield return item;
                await Task.Yield();
            }
        }

        /// <summary> Sends the request in the pipeline message and returns the response. </summary>
        /// <param name="message"> The pipeline message containing the request to send. </param>
        private async ValueTask<ClientResult> GetNextResponseAsync(PipelineMessage message)
        {
            return ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false));
        }
    }
}
