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

package com.azure.resourcemanager.managementgroups.implementation;

import com.azure.core.annotation.Delete;
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.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.util.Context;
import com.azure.core.util.FluxUtil;
import com.azure.resourcemanager.managementgroups.fluent.ManagementGroupSubscriptionsClient;
import com.azure.resourcemanager.managementgroups.fluent.models.SubscriptionUnderManagementGroupInner;
import com.azure.resourcemanager.managementgroups.models.ListSubscriptionUnderManagementGroup;
import reactor.core.publisher.Mono;

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

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

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

    /**
     * The interface defining all the services for ManagementGroupsApiManagementGroupSubscriptions to be used by the
     * proxy service to perform REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "ManagementGroupsApiM")
    public interface ManagementGroupSubscriptionsService {
        @Headers({"Content-Type: application/json"})
        @Put("/providers/Microsoft.Management/managementGroups/{groupId}/subscriptions/{subscriptionId}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<SubscriptionUnderManagementGroupInner>> create(
            @HostParam("$host") String endpoint,
            @PathParam("groupId") String groupId,
            @PathParam("subscriptionId") String subscriptionId,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Cache-Control") String cacheControl,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Delete("/providers/Microsoft.Management/managementGroups/{groupId}/subscriptions/{subscriptionId}")
        @ExpectedResponses({200, 204})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<Void>> delete(
            @HostParam("$host") String endpoint,
            @PathParam("groupId") String groupId,
            @PathParam("subscriptionId") String subscriptionId,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Cache-Control") String cacheControl,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get("/providers/Microsoft.Management/managementGroups/{groupId}/subscriptions/{subscriptionId}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<SubscriptionUnderManagementGroupInner>> getSubscription(
            @HostParam("$host") String endpoint,
            @PathParam("groupId") String groupId,
            @PathParam("subscriptionId") String subscriptionId,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Cache-Control") String cacheControl,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get("/providers/Microsoft.Management/managementGroups/{groupId}/subscriptions")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<ListSubscriptionUnderManagementGroup>> getSubscriptionsUnderManagementGroup(
            @HostParam("$host") String endpoint,
            @PathParam("groupId") String groupId,
            @QueryParam("api-version") String apiVersion,
            @QueryParam("$skiptoken") String skiptoken,
            @HeaderParam("Accept") String accept,
            Context context);

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

    /**
     * Associates existing subscription with the management group.
     *
     * @param groupId Management Group ID.
     * @param subscriptionId Subscription ID.
     * @param cacheControl Indicates whether the request should utilize any caches. Populate the header with 'no-cache'
     *     value to bypass existing caches.
     * @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 details of subscription under management group along with {@link Response} on successful completion
     *     of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<SubscriptionUnderManagementGroupInner>> createWithResponseAsync(
        String groupId, String subscriptionId, String cacheControl) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (groupId == null) {
            return Mono.error(new IllegalArgumentException("Parameter groupId is required and cannot be null."));
        }
        if (subscriptionId == null) {
            return Mono.error(new IllegalArgumentException("Parameter subscriptionId is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .create(
                            this.client.getEndpoint(),
                            groupId,
                            subscriptionId,
                            this.client.getApiVersion(),
                            cacheControl,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Associates existing subscription with the management group.
     *
     * @param groupId Management Group ID.
     * @param subscriptionId Subscription ID.
     * @param cacheControl Indicates whether the request should utilize any caches. Populate the header with 'no-cache'
     *     value to bypass existing caches.
     * @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 details of subscription under management group along with {@link Response} on successful completion
     *     of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<SubscriptionUnderManagementGroupInner>> createWithResponseAsync(
        String groupId, String subscriptionId, String cacheControl, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (groupId == null) {
            return Mono.error(new IllegalArgumentException("Parameter groupId is required and cannot be null."));
        }
        if (subscriptionId == null) {
            return Mono.error(new IllegalArgumentException("Parameter subscriptionId is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .create(
                this.client.getEndpoint(),
                groupId,
                subscriptionId,
                this.client.getApiVersion(),
                cacheControl,
                accept,
                context);
    }

    /**
     * Associates existing subscription with the management group.
     *
     * @param groupId Management Group ID.
     * @param subscriptionId Subscription ID.
     * @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 details of subscription under management group on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<SubscriptionUnderManagementGroupInner> createAsync(String groupId, String subscriptionId) {
        final String cacheControl = null;
        return createWithResponseAsync(groupId, subscriptionId, cacheControl)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Associates existing subscription with the management group.
     *
     * @param groupId Management Group ID.
     * @param subscriptionId Subscription ID.
     * @param cacheControl Indicates whether the request should utilize any caches. Populate the header with 'no-cache'
     *     value to bypass existing caches.
     * @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 details of subscription under management group along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<SubscriptionUnderManagementGroupInner> createWithResponse(
        String groupId, String subscriptionId, String cacheControl, Context context) {
        return createWithResponseAsync(groupId, subscriptionId, cacheControl, context).block();
    }

    /**
     * Associates existing subscription with the management group.
     *
     * @param groupId Management Group ID.
     * @param subscriptionId Subscription ID.
     * @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 details of subscription under management group.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public SubscriptionUnderManagementGroupInner create(String groupId, String subscriptionId) {
        final String cacheControl = null;
        return createWithResponse(groupId, subscriptionId, cacheControl, Context.NONE).getValue();
    }

    /**
     * De-associates subscription from the management group.
     *
     * @param groupId Management Group ID.
     * @param subscriptionId Subscription ID.
     * @param cacheControl Indicates whether the request should utilize any caches. Populate the header with 'no-cache'
     *     value to bypass existing caches.
     * @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 Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Void>> deleteWithResponseAsync(String groupId, String subscriptionId, String cacheControl) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (groupId == null) {
            return Mono.error(new IllegalArgumentException("Parameter groupId is required and cannot be null."));
        }
        if (subscriptionId == null) {
            return Mono.error(new IllegalArgumentException("Parameter subscriptionId is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .delete(
                            this.client.getEndpoint(),
                            groupId,
                            subscriptionId,
                            this.client.getApiVersion(),
                            cacheControl,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * De-associates subscription from the management group.
     *
     * @param groupId Management Group ID.
     * @param subscriptionId Subscription ID.
     * @param cacheControl Indicates whether the request should utilize any caches. Populate the header with 'no-cache'
     *     value to bypass existing caches.
     * @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 Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Void>> deleteWithResponseAsync(
        String groupId, String subscriptionId, String cacheControl, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (groupId == null) {
            return Mono.error(new IllegalArgumentException("Parameter groupId is required and cannot be null."));
        }
        if (subscriptionId == null) {
            return Mono.error(new IllegalArgumentException("Parameter subscriptionId is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .delete(
                this.client.getEndpoint(),
                groupId,
                subscriptionId,
                this.client.getApiVersion(),
                cacheControl,
                accept,
                context);
    }

    /**
     * De-associates subscription from the management group.
     *
     * @param groupId Management Group ID.
     * @param subscriptionId Subscription ID.
     * @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 A {@link Mono} that completes when a successful response is received.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Void> deleteAsync(String groupId, String subscriptionId) {
        final String cacheControl = null;
        return deleteWithResponseAsync(groupId, subscriptionId, cacheControl).flatMap(ignored -> Mono.empty());
    }

    /**
     * De-associates subscription from the management group.
     *
     * @param groupId Management Group ID.
     * @param subscriptionId Subscription ID.
     * @param cacheControl Indicates whether the request should utilize any caches. Populate the header with 'no-cache'
     *     value to bypass existing caches.
     * @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 Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<Void> deleteWithResponse(
        String groupId, String subscriptionId, String cacheControl, Context context) {
        return deleteWithResponseAsync(groupId, subscriptionId, cacheControl, context).block();
    }

    /**
     * De-associates subscription from the management group.
     *
     * @param groupId Management Group ID.
     * @param subscriptionId Subscription ID.
     * @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.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public void delete(String groupId, String subscriptionId) {
        final String cacheControl = null;
        deleteWithResponse(groupId, subscriptionId, cacheControl, Context.NONE);
    }

    /**
     * Retrieves details about given subscription which is associated with the management group.
     *
     * @param groupId Management Group ID.
     * @param subscriptionId Subscription ID.
     * @param cacheControl Indicates whether the request should utilize any caches. Populate the header with 'no-cache'
     *     value to bypass existing caches.
     * @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 details of subscription under management group along with {@link Response} on successful completion
     *     of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<SubscriptionUnderManagementGroupInner>> getSubscriptionWithResponseAsync(
        String groupId, String subscriptionId, String cacheControl) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (groupId == null) {
            return Mono.error(new IllegalArgumentException("Parameter groupId is required and cannot be null."));
        }
        if (subscriptionId == null) {
            return Mono.error(new IllegalArgumentException("Parameter subscriptionId is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .getSubscription(
                            this.client.getEndpoint(),
                            groupId,
                            subscriptionId,
                            this.client.getApiVersion(),
                            cacheControl,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Retrieves details about given subscription which is associated with the management group.
     *
     * @param groupId Management Group ID.
     * @param subscriptionId Subscription ID.
     * @param cacheControl Indicates whether the request should utilize any caches. Populate the header with 'no-cache'
     *     value to bypass existing caches.
     * @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 details of subscription under management group along with {@link Response} on successful completion
     *     of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<SubscriptionUnderManagementGroupInner>> getSubscriptionWithResponseAsync(
        String groupId, String subscriptionId, String cacheControl, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (groupId == null) {
            return Mono.error(new IllegalArgumentException("Parameter groupId is required and cannot be null."));
        }
        if (subscriptionId == null) {
            return Mono.error(new IllegalArgumentException("Parameter subscriptionId is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .getSubscription(
                this.client.getEndpoint(),
                groupId,
                subscriptionId,
                this.client.getApiVersion(),
                cacheControl,
                accept,
                context);
    }

    /**
     * Retrieves details about given subscription which is associated with the management group.
     *
     * @param groupId Management Group ID.
     * @param subscriptionId Subscription ID.
     * @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 details of subscription under management group on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<SubscriptionUnderManagementGroupInner> getSubscriptionAsync(String groupId, String subscriptionId) {
        final String cacheControl = null;
        return getSubscriptionWithResponseAsync(groupId, subscriptionId, cacheControl)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Retrieves details about given subscription which is associated with the management group.
     *
     * @param groupId Management Group ID.
     * @param subscriptionId Subscription ID.
     * @param cacheControl Indicates whether the request should utilize any caches. Populate the header with 'no-cache'
     *     value to bypass existing caches.
     * @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 details of subscription under management group along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<SubscriptionUnderManagementGroupInner> getSubscriptionWithResponse(
        String groupId, String subscriptionId, String cacheControl, Context context) {
        return getSubscriptionWithResponseAsync(groupId, subscriptionId, cacheControl, context).block();
    }

    /**
     * Retrieves details about given subscription which is associated with the management group.
     *
     * @param groupId Management Group ID.
     * @param subscriptionId Subscription ID.
     * @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 details of subscription under management group.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public SubscriptionUnderManagementGroupInner getSubscription(String groupId, String subscriptionId) {
        final String cacheControl = null;
        return getSubscriptionWithResponse(groupId, subscriptionId, cacheControl, Context.NONE).getValue();
    }

    /**
     * Retrieves details about all subscriptions which are associated with the management group.
     *
     * @param groupId Management Group ID.
     * @param skiptoken Page continuation token is only used if a previous operation returned a partial result. If a
     *     previous response contains a nextLink element, the value of the nextLink element will include a token
     *     parameter that specifies a starting point to use for subsequent calls.
     * @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 details of all subscriptions under management group along with {@link PagedResponse} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<SubscriptionUnderManagementGroupInner>>
        getSubscriptionsUnderManagementGroupSinglePageAsync(String groupId, String skiptoken) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (groupId == null) {
            return Mono.error(new IllegalArgumentException("Parameter groupId is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .getSubscriptionsUnderManagementGroup(
                            this.client.getEndpoint(),
                            groupId,
                            this.client.getApiVersion(),
                            skiptoken,
                            accept,
                            context))
            .<PagedResponse<SubscriptionUnderManagementGroupInner>>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()));
    }

    /**
     * Retrieves details about all subscriptions which are associated with the management group.
     *
     * @param groupId Management Group ID.
     * @param skiptoken Page continuation token is only used if a previous operation returned a partial result. If a
     *     previous response contains a nextLink element, the value of the nextLink element will include a token
     *     parameter that specifies a starting point to use for subsequent calls.
     * @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 details of all subscriptions under management group along with {@link PagedResponse} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<SubscriptionUnderManagementGroupInner>>
        getSubscriptionsUnderManagementGroupSinglePageAsync(String groupId, String skiptoken, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (groupId == null) {
            return Mono.error(new IllegalArgumentException("Parameter groupId is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .getSubscriptionsUnderManagementGroup(
                this.client.getEndpoint(), groupId, this.client.getApiVersion(), skiptoken, accept, context)
            .map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null));
    }

    /**
     * Retrieves details about all subscriptions which are associated with the management group.
     *
     * @param groupId Management Group ID.
     * @param skiptoken Page continuation token is only used if a previous operation returned a partial result. If a
     *     previous response contains a nextLink element, the value of the nextLink element will include a token
     *     parameter that specifies a starting point to use for subsequent calls.
     * @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 details of all subscriptions under management group as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<SubscriptionUnderManagementGroupInner> getSubscriptionsUnderManagementGroupAsync(
        String groupId, String skiptoken) {
        return new PagedFlux<>(
            () -> getSubscriptionsUnderManagementGroupSinglePageAsync(groupId, skiptoken),
            nextLink -> getSubscriptionsUnderManagementGroupNextSinglePageAsync(nextLink));
    }

    /**
     * Retrieves details about all subscriptions which are associated with the management group.
     *
     * @param groupId Management Group ID.
     * @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 details of all subscriptions under management group as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<SubscriptionUnderManagementGroupInner> getSubscriptionsUnderManagementGroupAsync(String groupId) {
        final String skiptoken = null;
        return new PagedFlux<>(
            () -> getSubscriptionsUnderManagementGroupSinglePageAsync(groupId, skiptoken),
            nextLink -> getSubscriptionsUnderManagementGroupNextSinglePageAsync(nextLink));
    }

    /**
     * Retrieves details about all subscriptions which are associated with the management group.
     *
     * @param groupId Management Group ID.
     * @param skiptoken Page continuation token is only used if a previous operation returned a partial result. If a
     *     previous response contains a nextLink element, the value of the nextLink element will include a token
     *     parameter that specifies a starting point to use for subsequent calls.
     * @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 details of all subscriptions under management group as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<SubscriptionUnderManagementGroupInner> getSubscriptionsUnderManagementGroupAsync(
        String groupId, String skiptoken, Context context) {
        return new PagedFlux<>(
            () -> getSubscriptionsUnderManagementGroupSinglePageAsync(groupId, skiptoken, context),
            nextLink -> getSubscriptionsUnderManagementGroupNextSinglePageAsync(nextLink, context));
    }

    /**
     * Retrieves details about all subscriptions which are associated with the management group.
     *
     * @param groupId Management Group ID.
     * @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 details of all subscriptions under management group as paginated response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<SubscriptionUnderManagementGroupInner> getSubscriptionsUnderManagementGroup(String groupId) {
        final String skiptoken = null;
        return new PagedIterable<>(getSubscriptionsUnderManagementGroupAsync(groupId, skiptoken));
    }

    /**
     * Retrieves details about all subscriptions which are associated with the management group.
     *
     * @param groupId Management Group ID.
     * @param skiptoken Page continuation token is only used if a previous operation returned a partial result. If a
     *     previous response contains a nextLink element, the value of the nextLink element will include a token
     *     parameter that specifies a starting point to use for subsequent calls.
     * @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 details of all subscriptions under management group as paginated response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<SubscriptionUnderManagementGroupInner> getSubscriptionsUnderManagementGroup(
        String groupId, String skiptoken, Context context) {
        return new PagedIterable<>(getSubscriptionsUnderManagementGroupAsync(groupId, skiptoken, context));
    }

    /**
     * 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 the details of all subscriptions under management group along with {@link PagedResponse} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<SubscriptionUnderManagementGroupInner>>
        getSubscriptionsUnderManagementGroupNextSinglePageAsync(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
                        .getSubscriptionsUnderManagementGroupNext(nextLink, this.client.getEndpoint(), accept, context))
            .<PagedResponse<SubscriptionUnderManagementGroupInner>>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 the details of all subscriptions under management group along with {@link PagedResponse} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<SubscriptionUnderManagementGroupInner>>
        getSubscriptionsUnderManagementGroupNextSinglePageAsync(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
            .getSubscriptionsUnderManagementGroupNext(nextLink, this.client.getEndpoint(), accept, context)
            .map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null));
    }
}
