// 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 SampleTypeSpecClientGetWithStringNextLinkCollectionResult : CollectionResult
    {
        private readonly SampleTypeSpecClient _client;
        private readonly RequestOptions _options;

        /// <summary> Initializes a new instance of SampleTypeSpecClientGetWithStringNextLinkCollectionResult, 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="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
        public SampleTypeSpecClientGetWithStringNextLinkCollectionResult(SampleTypeSpecClient client, RequestOptions options)
        {
            _client = client;
            _options = options;
        }

        /// <summary> Gets the raw pages of the collection. </summary>
        /// <returns> The raw pages of the collection. </returns>
        public override IEnumerable<ClientResult> GetRawPages()
        {
            PipelineMessage message = _client.CreateGetWithStringNextLinkRequest(_options);
            Uri nextPageUri = null;
            while (true)
            {
                ClientResult result = GetNextResponse(message);
                yield return result;

                string nextPageString = ((ListWithStringNextLinkResponse)result).Next;
                if (string.IsNullOrEmpty(nextPageString))
                {
                    yield break;
                }
                nextPageUri = new Uri(nextPageString, UriKind.RelativeOrAbsolute);
                message = _client.CreateNextGetWithStringNextLinkRequest(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)
        {
            string nextPage = ((ListWithStringNextLinkResponse)page).Next;
            if (!string.IsNullOrEmpty(nextPage))
            {
                return ContinuationToken.FromBytes(BinaryData.FromString(nextPage));
            }
            else
            {
                return null;
            }
        }

        /// <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 ClientResult GetNextResponse(PipelineMessage message)
        {
            return ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options));
        }
    }
}
