// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.support.implementation;

import com.azure.core.annotation.BodyParam;
import com.azure.core.annotation.ExpectedResponses;
import com.azure.core.annotation.Get;
import com.azure.core.annotation.HeaderParam;
import com.azure.core.annotation.Headers;
import com.azure.core.annotation.Host;
import com.azure.core.annotation.HostParam;
import com.azure.core.annotation.PathParam;
import com.azure.core.annotation.Post;
import com.azure.core.annotation.Put;
import com.azure.core.annotation.QueryParam;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceInterface;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.annotation.UnexpectedResponseExceptionType;
import com.azure.core.http.rest.PagedFlux;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.PagedResponse;
import com.azure.core.http.rest.PagedResponseBase;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.RestProxy;
import com.azure.core.management.exception.ManagementException;
import com.azure.core.management.polling.PollResult;
import com.azure.core.util.Context;
import com.azure.core.util.FluxUtil;
import com.azure.core.util.polling.PollerFlux;
import com.azure.core.util.polling.SyncPoller;
import com.azure.resourcemanager.support.fluent.CommunicationsClient;
import com.azure.resourcemanager.support.fluent.models.CheckNameAvailabilityOutputInner;
import com.azure.resourcemanager.support.fluent.models.CommunicationDetailsInner;
import com.azure.resourcemanager.support.models.CheckNameAvailabilityInput;
import com.azure.resourcemanager.support.models.CommunicationsListResult;
import java.nio.ByteBuffer;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/** An instance of this class provides access to all the operations defined in CommunicationsClient. */
public final class CommunicationsClientImpl implements CommunicationsClient {
    /** The proxy service used to perform REST calls. */
    private final CommunicationsService service;

    /** The service client containing this operation class. */
    private final MicrosoftSupportImpl client;

    /**
     * Initializes an instance of CommunicationsClientImpl.
     *
     * @param client the instance of the service client containing this operation class.
     */
    CommunicationsClientImpl(MicrosoftSupportImpl client) {
        this.service =
            RestProxy.create(CommunicationsService.class, client.getHttpPipeline(), client.getSerializerAdapter());
        this.client = client;
    }

    /**
     * The interface defining all the services for MicrosoftSupportCommunications to be used by the proxy service to
     * perform REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "MicrosoftSupportComm")
    public interface CommunicationsService {
        @Headers({"Content-Type: application/json"})
        @Post(
            "/subscriptions/{subscriptionId}/providers/Microsoft.Support/supportTickets/{supportTicketName}"
                + "/checkNameAvailability")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<CheckNameAvailabilityOutputInner>> checkNameAvailability(
            @HostParam("$host") String endpoint,
            @PathParam("supportTicketName") String supportTicketName,
            @PathParam("subscriptionId") String subscriptionId,
            @QueryParam("api-version") String apiVersion,
            @BodyParam("application/json") CheckNameAvailabilityInput checkNameAvailabilityInput,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get(
            "/subscriptions/{subscriptionId}/providers/Microsoft.Support/supportTickets/{supportTicketName}"
                + "/communications")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<CommunicationsListResult>> list(
            @HostParam("$host") String endpoint,
            @PathParam("supportTicketName") String supportTicketName,
            @QueryParam("$top") Integer top,
            @QueryParam("$filter") String filter,
            @PathParam("subscriptionId") String subscriptionId,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get(
            "/subscriptions/{subscriptionId}/providers/Microsoft.Support/supportTickets/{supportTicketName}"
                + "/communications/{communicationName}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<CommunicationDetailsInner>> get(
            @HostParam("$host") String endpoint,
            @PathParam("supportTicketName") String supportTicketName,
            @PathParam("communicationName") String communicationName,
            @PathParam("subscriptionId") String subscriptionId,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Put(
            "/subscriptions/{subscriptionId}/providers/Microsoft.Support/supportTickets/{supportTicketName}"
                + "/communications/{communicationName}")
        @ExpectedResponses({200, 202})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<Flux<ByteBuffer>>> create(
            @HostParam("$host") String endpoint,
            @PathParam("supportTicketName") String supportTicketName,
            @PathParam("communicationName") String communicationName,
            @PathParam("subscriptionId") String subscriptionId,
            @QueryParam("api-version") String apiVersion,
            @BodyParam("application/json") CommunicationDetailsInner createCommunicationParameters,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get("{nextLink}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<CommunicationsListResult>> listNext(
            @PathParam(value = "nextLink", encoded = true) String nextLink,
            @HostParam("$host") String endpoint,
            @HeaderParam("Accept") String accept,
            Context context);
    }

    /**
     * Check the availability of a resource name. This API should be used to check the uniqueness of the name for adding
     * a new communication to the support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param checkNameAvailabilityInput Input to check.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return output of check name availability API along with {@link Response} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<CheckNameAvailabilityOutputInner>> checkNameAvailabilityWithResponseAsync(
        String supportTicketName, CheckNameAvailabilityInput checkNameAvailabilityInput) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (supportTicketName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter supportTicketName is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (checkNameAvailabilityInput == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter checkNameAvailabilityInput is required and cannot be null."));
        } else {
            checkNameAvailabilityInput.validate();
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .checkNameAvailability(
                            this.client.getEndpoint(),
                            supportTicketName,
                            this.client.getSubscriptionId(),
                            this.client.getApiVersion(),
                            checkNameAvailabilityInput,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Check the availability of a resource name. This API should be used to check the uniqueness of the name for adding
     * a new communication to the support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param checkNameAvailabilityInput Input to check.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return output of check name availability API along with {@link Response} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<CheckNameAvailabilityOutputInner>> checkNameAvailabilityWithResponseAsync(
        String supportTicketName, CheckNameAvailabilityInput checkNameAvailabilityInput, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (supportTicketName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter supportTicketName is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (checkNameAvailabilityInput == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter checkNameAvailabilityInput is required and cannot be null."));
        } else {
            checkNameAvailabilityInput.validate();
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .checkNameAvailability(
                this.client.getEndpoint(),
                supportTicketName,
                this.client.getSubscriptionId(),
                this.client.getApiVersion(),
                checkNameAvailabilityInput,
                accept,
                context);
    }

    /**
     * Check the availability of a resource name. This API should be used to check the uniqueness of the name for adding
     * a new communication to the support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param checkNameAvailabilityInput Input to check.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return output of check name availability API on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<CheckNameAvailabilityOutputInner> checkNameAvailabilityAsync(
        String supportTicketName, CheckNameAvailabilityInput checkNameAvailabilityInput) {
        return checkNameAvailabilityWithResponseAsync(supportTicketName, checkNameAvailabilityInput)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Check the availability of a resource name. This API should be used to check the uniqueness of the name for adding
     * a new communication to the support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param checkNameAvailabilityInput Input to check.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return output of check name availability API along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<CheckNameAvailabilityOutputInner> checkNameAvailabilityWithResponse(
        String supportTicketName, CheckNameAvailabilityInput checkNameAvailabilityInput, Context context) {
        return checkNameAvailabilityWithResponseAsync(supportTicketName, checkNameAvailabilityInput, context).block();
    }

    /**
     * Check the availability of a resource name. This API should be used to check the uniqueness of the name for adding
     * a new communication to the support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param checkNameAvailabilityInput Input to check.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return output of check name availability API.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public CheckNameAvailabilityOutputInner checkNameAvailability(
        String supportTicketName, CheckNameAvailabilityInput checkNameAvailabilityInput) {
        return checkNameAvailabilityWithResponse(supportTicketName, checkNameAvailabilityInput, Context.NONE)
            .getValue();
    }

    /**
     * Lists all communications (attachments not included) for a support ticket. &lt;br/&gt;&lt;/br&gt; You can also
     * filter support ticket communications by _CreatedDate_ or _CommunicationType_ using the $filter parameter. The
     * only type of communication supported today is _Web_. Output will be a paged result with _nextLink_, using which
     * you can retrieve the next set of Communication results. &lt;br/&gt;&lt;br/&gt;Support ticket data is available
     * for 18 months after ticket creation. If a ticket was created more than 18 months ago, a request for data might
     * cause an error.
     *
     * @param supportTicketName Support ticket name.
     * @param top The number of values to return in the collection. Default is 10 and max is 10.
     * @param filter The filter to apply on the operation. You can filter by communicationType and createdDate
     *     properties. CommunicationType supports Equals ('eq') operator and createdDate supports Greater Than ('gt')
     *     and Greater Than or Equals ('ge') operators. You may combine the CommunicationType and CreatedDate filters by
     *     Logical And ('and') operator.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return collection of Communication resources along with {@link PagedResponse} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<CommunicationDetailsInner>> listSinglePageAsync(
        String supportTicketName, Integer top, String filter) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (supportTicketName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter supportTicketName is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .list(
                            this.client.getEndpoint(),
                            supportTicketName,
                            top,
                            filter,
                            this.client.getSubscriptionId(),
                            this.client.getApiVersion(),
                            accept,
                            context))
            .<PagedResponse<CommunicationDetailsInner>>map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Lists all communications (attachments not included) for a support ticket. &lt;br/&gt;&lt;/br&gt; You can also
     * filter support ticket communications by _CreatedDate_ or _CommunicationType_ using the $filter parameter. The
     * only type of communication supported today is _Web_. Output will be a paged result with _nextLink_, using which
     * you can retrieve the next set of Communication results. &lt;br/&gt;&lt;br/&gt;Support ticket data is available
     * for 18 months after ticket creation. If a ticket was created more than 18 months ago, a request for data might
     * cause an error.
     *
     * @param supportTicketName Support ticket name.
     * @param top The number of values to return in the collection. Default is 10 and max is 10.
     * @param filter The filter to apply on the operation. You can filter by communicationType and createdDate
     *     properties. CommunicationType supports Equals ('eq') operator and createdDate supports Greater Than ('gt')
     *     and Greater Than or Equals ('ge') operators. You may combine the CommunicationType and CreatedDate filters by
     *     Logical And ('and') operator.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return collection of Communication resources along with {@link PagedResponse} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<CommunicationDetailsInner>> listSinglePageAsync(
        String supportTicketName, Integer top, String filter, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (supportTicketName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter supportTicketName is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .list(
                this.client.getEndpoint(),
                supportTicketName,
                top,
                filter,
                this.client.getSubscriptionId(),
                this.client.getApiVersion(),
                accept,
                context)
            .map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null));
    }

    /**
     * Lists all communications (attachments not included) for a support ticket. &lt;br/&gt;&lt;/br&gt; You can also
     * filter support ticket communications by _CreatedDate_ or _CommunicationType_ using the $filter parameter. The
     * only type of communication supported today is _Web_. Output will be a paged result with _nextLink_, using which
     * you can retrieve the next set of Communication results. &lt;br/&gt;&lt;br/&gt;Support ticket data is available
     * for 18 months after ticket creation. If a ticket was created more than 18 months ago, a request for data might
     * cause an error.
     *
     * @param supportTicketName Support ticket name.
     * @param top The number of values to return in the collection. Default is 10 and max is 10.
     * @param filter The filter to apply on the operation. You can filter by communicationType and createdDate
     *     properties. CommunicationType supports Equals ('eq') operator and createdDate supports Greater Than ('gt')
     *     and Greater Than or Equals ('ge') operators. You may combine the CommunicationType and CreatedDate filters by
     *     Logical And ('and') operator.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return collection of Communication resources as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<CommunicationDetailsInner> listAsync(String supportTicketName, Integer top, String filter) {
        return new PagedFlux<>(
            () -> listSinglePageAsync(supportTicketName, top, filter), nextLink -> listNextSinglePageAsync(nextLink));
    }

    /**
     * Lists all communications (attachments not included) for a support ticket. &lt;br/&gt;&lt;/br&gt; You can also
     * filter support ticket communications by _CreatedDate_ or _CommunicationType_ using the $filter parameter. The
     * only type of communication supported today is _Web_. Output will be a paged result with _nextLink_, using which
     * you can retrieve the next set of Communication results. &lt;br/&gt;&lt;br/&gt;Support ticket data is available
     * for 18 months after ticket creation. If a ticket was created more than 18 months ago, a request for data might
     * cause an error.
     *
     * @param supportTicketName Support ticket name.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return collection of Communication resources as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<CommunicationDetailsInner> listAsync(String supportTicketName) {
        final Integer top = null;
        final String filter = null;
        return new PagedFlux<>(
            () -> listSinglePageAsync(supportTicketName, top, filter), nextLink -> listNextSinglePageAsync(nextLink));
    }

    /**
     * Lists all communications (attachments not included) for a support ticket. &lt;br/&gt;&lt;/br&gt; You can also
     * filter support ticket communications by _CreatedDate_ or _CommunicationType_ using the $filter parameter. The
     * only type of communication supported today is _Web_. Output will be a paged result with _nextLink_, using which
     * you can retrieve the next set of Communication results. &lt;br/&gt;&lt;br/&gt;Support ticket data is available
     * for 18 months after ticket creation. If a ticket was created more than 18 months ago, a request for data might
     * cause an error.
     *
     * @param supportTicketName Support ticket name.
     * @param top The number of values to return in the collection. Default is 10 and max is 10.
     * @param filter The filter to apply on the operation. You can filter by communicationType and createdDate
     *     properties. CommunicationType supports Equals ('eq') operator and createdDate supports Greater Than ('gt')
     *     and Greater Than or Equals ('ge') operators. You may combine the CommunicationType and CreatedDate filters by
     *     Logical And ('and') operator.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return collection of Communication resources as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<CommunicationDetailsInner> listAsync(
        String supportTicketName, Integer top, String filter, Context context) {
        return new PagedFlux<>(
            () -> listSinglePageAsync(supportTicketName, top, filter, context),
            nextLink -> listNextSinglePageAsync(nextLink, context));
    }

    /**
     * Lists all communications (attachments not included) for a support ticket. &lt;br/&gt;&lt;/br&gt; You can also
     * filter support ticket communications by _CreatedDate_ or _CommunicationType_ using the $filter parameter. The
     * only type of communication supported today is _Web_. Output will be a paged result with _nextLink_, using which
     * you can retrieve the next set of Communication results. &lt;br/&gt;&lt;br/&gt;Support ticket data is available
     * for 18 months after ticket creation. If a ticket was created more than 18 months ago, a request for data might
     * cause an error.
     *
     * @param supportTicketName Support ticket name.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return collection of Communication resources as paginated response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<CommunicationDetailsInner> list(String supportTicketName) {
        final Integer top = null;
        final String filter = null;
        return new PagedIterable<>(listAsync(supportTicketName, top, filter));
    }

    /**
     * Lists all communications (attachments not included) for a support ticket. &lt;br/&gt;&lt;/br&gt; You can also
     * filter support ticket communications by _CreatedDate_ or _CommunicationType_ using the $filter parameter. The
     * only type of communication supported today is _Web_. Output will be a paged result with _nextLink_, using which
     * you can retrieve the next set of Communication results. &lt;br/&gt;&lt;br/&gt;Support ticket data is available
     * for 18 months after ticket creation. If a ticket was created more than 18 months ago, a request for data might
     * cause an error.
     *
     * @param supportTicketName Support ticket name.
     * @param top The number of values to return in the collection. Default is 10 and max is 10.
     * @param filter The filter to apply on the operation. You can filter by communicationType and createdDate
     *     properties. CommunicationType supports Equals ('eq') operator and createdDate supports Greater Than ('gt')
     *     and Greater Than or Equals ('ge') operators. You may combine the CommunicationType and CreatedDate filters by
     *     Logical And ('and') operator.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return collection of Communication resources as paginated response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<CommunicationDetailsInner> list(
        String supportTicketName, Integer top, String filter, Context context) {
        return new PagedIterable<>(listAsync(supportTicketName, top, filter, context));
    }

    /**
     * Returns communication details for a support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param communicationName Communication name.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return object that represents a Communication resource along with {@link Response} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<CommunicationDetailsInner>> getWithResponseAsync(
        String supportTicketName, String communicationName) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (supportTicketName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter supportTicketName is required and cannot be null."));
        }
        if (communicationName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter communicationName is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .get(
                            this.client.getEndpoint(),
                            supportTicketName,
                            communicationName,
                            this.client.getSubscriptionId(),
                            this.client.getApiVersion(),
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Returns communication details for a support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param communicationName Communication name.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return object that represents a Communication resource along with {@link Response} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<CommunicationDetailsInner>> getWithResponseAsync(
        String supportTicketName, String communicationName, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (supportTicketName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter supportTicketName is required and cannot be null."));
        }
        if (communicationName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter communicationName is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .get(
                this.client.getEndpoint(),
                supportTicketName,
                communicationName,
                this.client.getSubscriptionId(),
                this.client.getApiVersion(),
                accept,
                context);
    }

    /**
     * Returns communication details for a support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param communicationName Communication name.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return object that represents a Communication resource on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<CommunicationDetailsInner> getAsync(String supportTicketName, String communicationName) {
        return getWithResponseAsync(supportTicketName, communicationName)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Returns communication details for a support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param communicationName Communication name.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return object that represents a Communication resource along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<CommunicationDetailsInner> getWithResponse(
        String supportTicketName, String communicationName, Context context) {
        return getWithResponseAsync(supportTicketName, communicationName, context).block();
    }

    /**
     * Returns communication details for a support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param communicationName Communication name.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return object that represents a Communication resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public CommunicationDetailsInner get(String supportTicketName, String communicationName) {
        return getWithResponse(supportTicketName, communicationName, Context.NONE).getValue();
    }

    /**
     * Adds a new customer communication to an Azure support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param communicationName Communication name.
     * @param createCommunicationParameters Communication object.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return object that represents a Communication resource along with {@link Response} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> createWithResponseAsync(
        String supportTicketName, String communicationName, CommunicationDetailsInner createCommunicationParameters) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (supportTicketName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter supportTicketName is required and cannot be null."));
        }
        if (communicationName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter communicationName is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (createCommunicationParameters == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter createCommunicationParameters is required and cannot be null."));
        } else {
            createCommunicationParameters.validate();
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .create(
                            this.client.getEndpoint(),
                            supportTicketName,
                            communicationName,
                            this.client.getSubscriptionId(),
                            this.client.getApiVersion(),
                            createCommunicationParameters,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Adds a new customer communication to an Azure support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param communicationName Communication name.
     * @param createCommunicationParameters Communication object.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return object that represents a Communication resource along with {@link Response} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> createWithResponseAsync(
        String supportTicketName,
        String communicationName,
        CommunicationDetailsInner createCommunicationParameters,
        Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (supportTicketName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter supportTicketName is required and cannot be null."));
        }
        if (communicationName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter communicationName is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (createCommunicationParameters == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter createCommunicationParameters is required and cannot be null."));
        } else {
            createCommunicationParameters.validate();
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .create(
                this.client.getEndpoint(),
                supportTicketName,
                communicationName,
                this.client.getSubscriptionId(),
                this.client.getApiVersion(),
                createCommunicationParameters,
                accept,
                context);
    }

    /**
     * Adds a new customer communication to an Azure support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param communicationName Communication name.
     * @param createCommunicationParameters Communication object.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link PollerFlux} for polling of object that represents a Communication resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<CommunicationDetailsInner>, CommunicationDetailsInner> beginCreateAsync(
        String supportTicketName, String communicationName, CommunicationDetailsInner createCommunicationParameters) {
        Mono<Response<Flux<ByteBuffer>>> mono =
            createWithResponseAsync(supportTicketName, communicationName, createCommunicationParameters);
        return this
            .client
            .<CommunicationDetailsInner, CommunicationDetailsInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                CommunicationDetailsInner.class,
                CommunicationDetailsInner.class,
                this.client.getContext());
    }

    /**
     * Adds a new customer communication to an Azure support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param communicationName Communication name.
     * @param createCommunicationParameters Communication object.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link PollerFlux} for polling of object that represents a Communication resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<CommunicationDetailsInner>, CommunicationDetailsInner> beginCreateAsync(
        String supportTicketName,
        String communicationName,
        CommunicationDetailsInner createCommunicationParameters,
        Context context) {
        context = this.client.mergeContext(context);
        Mono<Response<Flux<ByteBuffer>>> mono =
            createWithResponseAsync(supportTicketName, communicationName, createCommunicationParameters, context);
        return this
            .client
            .<CommunicationDetailsInner, CommunicationDetailsInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                CommunicationDetailsInner.class,
                CommunicationDetailsInner.class,
                context);
    }

    /**
     * Adds a new customer communication to an Azure support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param communicationName Communication name.
     * @param createCommunicationParameters Communication object.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link SyncPoller} for polling of object that represents a Communication resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<CommunicationDetailsInner>, CommunicationDetailsInner> beginCreate(
        String supportTicketName, String communicationName, CommunicationDetailsInner createCommunicationParameters) {
        return this
            .beginCreateAsync(supportTicketName, communicationName, createCommunicationParameters)
            .getSyncPoller();
    }

    /**
     * Adds a new customer communication to an Azure support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param communicationName Communication name.
     * @param createCommunicationParameters Communication object.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link SyncPoller} for polling of object that represents a Communication resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<CommunicationDetailsInner>, CommunicationDetailsInner> beginCreate(
        String supportTicketName,
        String communicationName,
        CommunicationDetailsInner createCommunicationParameters,
        Context context) {
        return this
            .beginCreateAsync(supportTicketName, communicationName, createCommunicationParameters, context)
            .getSyncPoller();
    }

    /**
     * Adds a new customer communication to an Azure support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param communicationName Communication name.
     * @param createCommunicationParameters Communication object.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return object that represents a Communication resource on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<CommunicationDetailsInner> createAsync(
        String supportTicketName, String communicationName, CommunicationDetailsInner createCommunicationParameters) {
        return beginCreateAsync(supportTicketName, communicationName, createCommunicationParameters)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Adds a new customer communication to an Azure support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param communicationName Communication name.
     * @param createCommunicationParameters Communication object.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return object that represents a Communication resource on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<CommunicationDetailsInner> createAsync(
        String supportTicketName,
        String communicationName,
        CommunicationDetailsInner createCommunicationParameters,
        Context context) {
        return beginCreateAsync(supportTicketName, communicationName, createCommunicationParameters, context)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Adds a new customer communication to an Azure support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param communicationName Communication name.
     * @param createCommunicationParameters Communication object.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return object that represents a Communication resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public CommunicationDetailsInner create(
        String supportTicketName, String communicationName, CommunicationDetailsInner createCommunicationParameters) {
        return createAsync(supportTicketName, communicationName, createCommunicationParameters).block();
    }

    /**
     * Adds a new customer communication to an Azure support ticket.
     *
     * @param supportTicketName Support ticket name.
     * @param communicationName Communication name.
     * @param createCommunicationParameters Communication object.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return object that represents a Communication resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public CommunicationDetailsInner create(
        String supportTicketName,
        String communicationName,
        CommunicationDetailsInner createCommunicationParameters,
        Context context) {
        return createAsync(supportTicketName, communicationName, createCommunicationParameters, context).block();
    }

    /**
     * Get the next page of items.
     *
     * @param nextLink The URL to get the next list of items
     *     <p>The nextLink parameter.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return collection of Communication resources along with {@link PagedResponse} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<CommunicationDetailsInner>> listNextSinglePageAsync(String nextLink) {
        if (nextLink == null) {
            return Mono.error(new IllegalArgumentException("Parameter nextLink is required and cannot be null."));
        }
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(context -> service.listNext(nextLink, this.client.getEndpoint(), accept, context))
            .<PagedResponse<CommunicationDetailsInner>>map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Get the next page of items.
     *
     * @param nextLink The URL to get the next list of items
     *     <p>The nextLink parameter.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return collection of Communication resources along with {@link PagedResponse} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<CommunicationDetailsInner>> listNextSinglePageAsync(String nextLink, Context context) {
        if (nextLink == null) {
            return Mono.error(new IllegalArgumentException("Parameter nextLink is required and cannot be null."));
        }
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .listNext(nextLink, this.client.getEndpoint(), accept, context)
            .map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null));
    }
}
