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

package com.azure.resourcemanager.reservations.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.reservations.fluent.ReservationOrdersClient;
import com.azure.resourcemanager.reservations.fluent.models.CalculatePriceResponseInner;
import com.azure.resourcemanager.reservations.fluent.models.ChangeDirectoryResponseInner;
import com.azure.resourcemanager.reservations.fluent.models.ReservationOrderResponseInner;
import com.azure.resourcemanager.reservations.models.ChangeDirectoryRequest;
import com.azure.resourcemanager.reservations.models.PurchaseRequest;
import com.azure.resourcemanager.reservations.models.ReservationOrderList;
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 ReservationOrdersClient. */
public final class ReservationOrdersClientImpl implements ReservationOrdersClient {
    /** The proxy service used to perform REST calls. */
    private final ReservationOrdersService service;

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

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

    /**
     * The interface defining all the services for AzureReservationApiReservationOrders to be used by the proxy service
     * to perform REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "AzureReservationApiR")
    public interface ReservationOrdersService {
        @Headers({"Content-Type: application/json"})
        @Post("/providers/Microsoft.Capacity/calculatePrice")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<CalculatePriceResponseInner>> calculate(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @BodyParam("application/json") PurchaseRequest body,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get("/providers/Microsoft.Capacity/reservationOrders")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<ReservationOrderList>> list(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Put("/providers/Microsoft.Capacity/reservationOrders/{reservationOrderId}")
        @ExpectedResponses({200, 202})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<Flux<ByteBuffer>>> purchase(
            @HostParam("$host") String endpoint,
            @PathParam("reservationOrderId") String reservationOrderId,
            @QueryParam("api-version") String apiVersion,
            @BodyParam("application/json") PurchaseRequest body,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get("/providers/Microsoft.Capacity/reservationOrders/{reservationOrderId}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<ReservationOrderResponseInner>> get(
            @HostParam("$host") String endpoint,
            @PathParam("reservationOrderId") String reservationOrderId,
            @QueryParam("api-version") String apiVersion,
            @QueryParam("$expand") String expand,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Post("/providers/Microsoft.Capacity/reservationOrders/{reservationOrderId}/changeDirectory")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<ChangeDirectoryResponseInner>> changeDirectory(
            @HostParam("$host") String endpoint,
            @PathParam("reservationOrderId") String reservationOrderId,
            @QueryParam("api-version") String apiVersion,
            @BodyParam("application/json") ChangeDirectoryRequest body,
            @HeaderParam("Accept") String accept,
            Context context);

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

    /**
     * Calculate price for a `ReservationOrder`.
     *
     * <p>Calculate price for placing a `ReservationOrder`.
     *
     * @param body Information needed for calculate or purchase reservation.
     * @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 response of calculate price for reservation along with {@link Response} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<CalculatePriceResponseInner>> calculateWithResponseAsync(PurchaseRequest body) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (body == null) {
            return Mono.error(new IllegalArgumentException("Parameter body is required and cannot be null."));
        } else {
            body.validate();
        }
        final String apiVersion = "2022-11-01";
        final String accept = "application/json";
        return FluxUtil
            .withContext(context -> service.calculate(this.client.getEndpoint(), apiVersion, body, accept, context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Calculate price for a `ReservationOrder`.
     *
     * <p>Calculate price for placing a `ReservationOrder`.
     *
     * @param body Information needed for calculate or purchase reservation.
     * @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 response of calculate price for reservation along with {@link Response} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<CalculatePriceResponseInner>> calculateWithResponseAsync(
        PurchaseRequest body, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (body == null) {
            return Mono.error(new IllegalArgumentException("Parameter body is required and cannot be null."));
        } else {
            body.validate();
        }
        final String apiVersion = "2022-11-01";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service.calculate(this.client.getEndpoint(), apiVersion, body, accept, context);
    }

    /**
     * Calculate price for a `ReservationOrder`.
     *
     * <p>Calculate price for placing a `ReservationOrder`.
     *
     * @param body Information needed for calculate or purchase reservation.
     * @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 response of calculate price for reservation on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<CalculatePriceResponseInner> calculateAsync(PurchaseRequest body) {
        return calculateWithResponseAsync(body).flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Calculate price for a `ReservationOrder`.
     *
     * <p>Calculate price for placing a `ReservationOrder`.
     *
     * @param body Information needed for calculate or purchase reservation.
     * @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 response of calculate price for reservation along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<CalculatePriceResponseInner> calculateWithResponse(PurchaseRequest body, Context context) {
        return calculateWithResponseAsync(body, context).block();
    }

    /**
     * Calculate price for a `ReservationOrder`.
     *
     * <p>Calculate price for placing a `ReservationOrder`.
     *
     * @param body Information needed for calculate or purchase reservation.
     * @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 response of calculate price for reservation.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public CalculatePriceResponseInner calculate(PurchaseRequest body) {
        return calculateWithResponse(body, Context.NONE).getValue();
    }

    /**
     * Get all `ReservationOrder`s.
     *
     * <p>List of all the `ReservationOrder`s that the user has access to in the current tenant.
     *
     * @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 list of `ReservationOrder`s along with {@link PagedResponse} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<ReservationOrderResponseInner>> listSinglePageAsync() {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        final String apiVersion = "2022-11-01";
        final String accept = "application/json";
        return FluxUtil
            .withContext(context -> service.list(this.client.getEndpoint(), apiVersion, accept, context))
            .<PagedResponse<ReservationOrderResponseInner>>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 all `ReservationOrder`s.
     *
     * <p>List of all the `ReservationOrder`s that the user has access to in the current tenant.
     *
     * @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 list of `ReservationOrder`s along with {@link PagedResponse} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<ReservationOrderResponseInner>> listSinglePageAsync(Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        final String apiVersion = "2022-11-01";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .list(this.client.getEndpoint(), apiVersion, accept, context)
            .map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null));
    }

    /**
     * Get all `ReservationOrder`s.
     *
     * <p>List of all the `ReservationOrder`s that the user has access to in the current tenant.
     *
     * @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 list of `ReservationOrder`s as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<ReservationOrderResponseInner> listAsync() {
        return new PagedFlux<>(() -> listSinglePageAsync(), nextLink -> listNextSinglePageAsync(nextLink));
    }

    /**
     * Get all `ReservationOrder`s.
     *
     * <p>List of all the `ReservationOrder`s that the user has access to in the current tenant.
     *
     * @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 list of `ReservationOrder`s as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<ReservationOrderResponseInner> listAsync(Context context) {
        return new PagedFlux<>(
            () -> listSinglePageAsync(context), nextLink -> listNextSinglePageAsync(nextLink, context));
    }

    /**
     * Get all `ReservationOrder`s.
     *
     * <p>List of all the `ReservationOrder`s that the user has access to in the current tenant.
     *
     * @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 list of `ReservationOrder`s as paginated response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<ReservationOrderResponseInner> list() {
        return new PagedIterable<>(listAsync());
    }

    /**
     * Get all `ReservationOrder`s.
     *
     * <p>List of all the `ReservationOrder`s that the user has access to in the current tenant.
     *
     * @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 list of `ReservationOrder`s as paginated response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<ReservationOrderResponseInner> list(Context context) {
        return new PagedIterable<>(listAsync(context));
    }

    /**
     * Purchase `ReservationOrder`
     *
     * <p>Purchase `ReservationOrder` and create resource under the specified URI.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for calculate or purchase reservation.
     * @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 details of a reservation order being returned along with {@link Response} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> purchaseWithResponseAsync(
        String reservationOrderId, PurchaseRequest body) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (reservationOrderId == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter reservationOrderId is required and cannot be null."));
        }
        if (body == null) {
            return Mono.error(new IllegalArgumentException("Parameter body is required and cannot be null."));
        } else {
            body.validate();
        }
        final String apiVersion = "2022-11-01";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service.purchase(this.client.getEndpoint(), reservationOrderId, apiVersion, body, accept, context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Purchase `ReservationOrder`
     *
     * <p>Purchase `ReservationOrder` and create resource under the specified URI.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for calculate or purchase reservation.
     * @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 details of a reservation order being returned along with {@link Response} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> purchaseWithResponseAsync(
        String reservationOrderId, PurchaseRequest body, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (reservationOrderId == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter reservationOrderId is required and cannot be null."));
        }
        if (body == null) {
            return Mono.error(new IllegalArgumentException("Parameter body is required and cannot be null."));
        } else {
            body.validate();
        }
        final String apiVersion = "2022-11-01";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service.purchase(this.client.getEndpoint(), reservationOrderId, apiVersion, body, accept, context);
    }

    /**
     * Purchase `ReservationOrder`
     *
     * <p>Purchase `ReservationOrder` and create resource under the specified URI.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for calculate or purchase reservation.
     * @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 details of a reservation order being returned.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<ReservationOrderResponseInner>, ReservationOrderResponseInner> beginPurchaseAsync(
        String reservationOrderId, PurchaseRequest body) {
        Mono<Response<Flux<ByteBuffer>>> mono = purchaseWithResponseAsync(reservationOrderId, body);
        return this
            .client
            .<ReservationOrderResponseInner, ReservationOrderResponseInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                ReservationOrderResponseInner.class,
                ReservationOrderResponseInner.class,
                this.client.getContext());
    }

    /**
     * Purchase `ReservationOrder`
     *
     * <p>Purchase `ReservationOrder` and create resource under the specified URI.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for calculate or purchase reservation.
     * @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 details of a reservation order being returned.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<ReservationOrderResponseInner>, ReservationOrderResponseInner> beginPurchaseAsync(
        String reservationOrderId, PurchaseRequest body, Context context) {
        context = this.client.mergeContext(context);
        Mono<Response<Flux<ByteBuffer>>> mono = purchaseWithResponseAsync(reservationOrderId, body, context);
        return this
            .client
            .<ReservationOrderResponseInner, ReservationOrderResponseInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                ReservationOrderResponseInner.class,
                ReservationOrderResponseInner.class,
                context);
    }

    /**
     * Purchase `ReservationOrder`
     *
     * <p>Purchase `ReservationOrder` and create resource under the specified URI.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for calculate or purchase reservation.
     * @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 details of a reservation order being returned.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<ReservationOrderResponseInner>, ReservationOrderResponseInner> beginPurchase(
        String reservationOrderId, PurchaseRequest body) {
        return this.beginPurchaseAsync(reservationOrderId, body).getSyncPoller();
    }

    /**
     * Purchase `ReservationOrder`
     *
     * <p>Purchase `ReservationOrder` and create resource under the specified URI.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for calculate or purchase reservation.
     * @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 details of a reservation order being returned.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<ReservationOrderResponseInner>, ReservationOrderResponseInner> beginPurchase(
        String reservationOrderId, PurchaseRequest body, Context context) {
        return this.beginPurchaseAsync(reservationOrderId, body, context).getSyncPoller();
    }

    /**
     * Purchase `ReservationOrder`
     *
     * <p>Purchase `ReservationOrder` and create resource under the specified URI.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for calculate or purchase reservation.
     * @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 details of a reservation order being returned on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ReservationOrderResponseInner> purchaseAsync(String reservationOrderId, PurchaseRequest body) {
        return beginPurchaseAsync(reservationOrderId, body).last().flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Purchase `ReservationOrder`
     *
     * <p>Purchase `ReservationOrder` and create resource under the specified URI.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for calculate or purchase reservation.
     * @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 details of a reservation order being returned on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ReservationOrderResponseInner> purchaseAsync(
        String reservationOrderId, PurchaseRequest body, Context context) {
        return beginPurchaseAsync(reservationOrderId, body, context)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Purchase `ReservationOrder`
     *
     * <p>Purchase `ReservationOrder` and create resource under the specified URI.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for calculate or purchase reservation.
     * @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 details of a reservation order being returned.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ReservationOrderResponseInner purchase(String reservationOrderId, PurchaseRequest body) {
        return purchaseAsync(reservationOrderId, body).block();
    }

    /**
     * Purchase `ReservationOrder`
     *
     * <p>Purchase `ReservationOrder` and create resource under the specified URI.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for calculate or purchase reservation.
     * @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 details of a reservation order being returned.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ReservationOrderResponseInner purchase(String reservationOrderId, PurchaseRequest body, Context context) {
        return purchaseAsync(reservationOrderId, body, context).block();
    }

    /**
     * Get a specific `ReservationOrder`.
     *
     * <p>Get the details of the `ReservationOrder`.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param expand May be used to expand the planInformation.
     * @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 the `ReservationOrder` along with {@link Response} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ReservationOrderResponseInner>> getWithResponseAsync(
        String reservationOrderId, String expand) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (reservationOrderId == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter reservationOrderId is required and cannot be null."));
        }
        final String apiVersion = "2022-11-01";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service.get(this.client.getEndpoint(), reservationOrderId, apiVersion, expand, accept, context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Get a specific `ReservationOrder`.
     *
     * <p>Get the details of the `ReservationOrder`.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param expand May be used to expand the planInformation.
     * @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 the `ReservationOrder` along with {@link Response} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ReservationOrderResponseInner>> getWithResponseAsync(
        String reservationOrderId, String expand, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (reservationOrderId == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter reservationOrderId is required and cannot be null."));
        }
        final String apiVersion = "2022-11-01";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service.get(this.client.getEndpoint(), reservationOrderId, apiVersion, expand, accept, context);
    }

    /**
     * Get a specific `ReservationOrder`.
     *
     * <p>Get the details of the `ReservationOrder`.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @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 the `ReservationOrder` on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ReservationOrderResponseInner> getAsync(String reservationOrderId) {
        final String expand = null;
        return getWithResponseAsync(reservationOrderId, expand).flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Get a specific `ReservationOrder`.
     *
     * <p>Get the details of the `ReservationOrder`.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param expand May be used to expand the planInformation.
     * @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 the `ReservationOrder` along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<ReservationOrderResponseInner> getWithResponse(
        String reservationOrderId, String expand, Context context) {
        return getWithResponseAsync(reservationOrderId, expand, context).block();
    }

    /**
     * Get a specific `ReservationOrder`.
     *
     * <p>Get the details of the `ReservationOrder`.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @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 the `ReservationOrder`.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ReservationOrderResponseInner get(String reservationOrderId) {
        final String expand = null;
        return getWithResponse(reservationOrderId, expand, Context.NONE).getValue();
    }

    /**
     * Change directory of `ReservationOrder`.
     *
     * <p>Change directory (tenant) of `ReservationOrder` and all `Reservation` under it to specified tenant id.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed to change directory of reservation order.
     * @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 change directory response along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ChangeDirectoryResponseInner>> changeDirectoryWithResponseAsync(
        String reservationOrderId, ChangeDirectoryRequest body) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (reservationOrderId == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter reservationOrderId is required and cannot be null."));
        }
        if (body == null) {
            return Mono.error(new IllegalArgumentException("Parameter body is required and cannot be null."));
        } else {
            body.validate();
        }
        final String apiVersion = "2022-11-01";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .changeDirectory(
                            this.client.getEndpoint(), reservationOrderId, apiVersion, body, accept, context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Change directory of `ReservationOrder`.
     *
     * <p>Change directory (tenant) of `ReservationOrder` and all `Reservation` under it to specified tenant id.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed to change directory of reservation order.
     * @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 change directory response along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ChangeDirectoryResponseInner>> changeDirectoryWithResponseAsync(
        String reservationOrderId, ChangeDirectoryRequest body, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (reservationOrderId == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter reservationOrderId is required and cannot be null."));
        }
        if (body == null) {
            return Mono.error(new IllegalArgumentException("Parameter body is required and cannot be null."));
        } else {
            body.validate();
        }
        final String apiVersion = "2022-11-01";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .changeDirectory(this.client.getEndpoint(), reservationOrderId, apiVersion, body, accept, context);
    }

    /**
     * Change directory of `ReservationOrder`.
     *
     * <p>Change directory (tenant) of `ReservationOrder` and all `Reservation` under it to specified tenant id.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed to change directory of reservation order.
     * @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 change directory response on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ChangeDirectoryResponseInner> changeDirectoryAsync(
        String reservationOrderId, ChangeDirectoryRequest body) {
        return changeDirectoryWithResponseAsync(reservationOrderId, body)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Change directory of `ReservationOrder`.
     *
     * <p>Change directory (tenant) of `ReservationOrder` and all `Reservation` under it to specified tenant id.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed to change directory of reservation order.
     * @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 change directory response along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<ChangeDirectoryResponseInner> changeDirectoryWithResponse(
        String reservationOrderId, ChangeDirectoryRequest body, Context context) {
        return changeDirectoryWithResponseAsync(reservationOrderId, body, context).block();
    }

    /**
     * Change directory of `ReservationOrder`.
     *
     * <p>Change directory (tenant) of `ReservationOrder` and all `Reservation` under it to specified tenant id.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed to change directory of reservation order.
     * @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 change directory response.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ChangeDirectoryResponseInner changeDirectory(String reservationOrderId, ChangeDirectoryRequest body) {
        return changeDirectoryWithResponse(reservationOrderId, body, Context.NONE).getValue();
    }

    /**
     * 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 list of `ReservationOrder`s along with {@link PagedResponse} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<ReservationOrderResponseInner>> 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<ReservationOrderResponseInner>>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 list of `ReservationOrder`s along with {@link PagedResponse} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<ReservationOrderResponseInner>> 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));
    }
}
