// 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.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.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.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.ReturnsClient;
import com.azure.resourcemanager.reservations.fluent.models.ReservationOrderResponseInner;
import com.azure.resourcemanager.reservations.models.RefundRequest;
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 ReturnsClient. */
public final class ReturnsClientImpl implements ReturnsClient {
    /** The proxy service used to perform REST calls. */
    private final ReturnsService service;

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

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

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

    /**
     * Return a reservation.
     *
     * <p>Return a reservation and get refund information.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for returning 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>>> postWithResponseAsync(String reservationOrderId, RefundRequest 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.post(this.client.getEndpoint(), reservationOrderId, apiVersion, body, accept, context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Return a reservation.
     *
     * <p>Return a reservation and get refund information.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for returning 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>>> postWithResponseAsync(
        String reservationOrderId, RefundRequest 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.post(this.client.getEndpoint(), reservationOrderId, apiVersion, body, accept, context);
    }

    /**
     * Return a reservation.
     *
     * <p>Return a reservation and get refund information.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for returning 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> beginPostAsync(
        String reservationOrderId, RefundRequest body) {
        Mono<Response<Flux<ByteBuffer>>> mono = postWithResponseAsync(reservationOrderId, body);
        return this
            .client
            .<ReservationOrderResponseInner, ReservationOrderResponseInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                ReservationOrderResponseInner.class,
                ReservationOrderResponseInner.class,
                this.client.getContext());
    }

    /**
     * Return a reservation.
     *
     * <p>Return a reservation and get refund information.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for returning 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> beginPostAsync(
        String reservationOrderId, RefundRequest body, Context context) {
        context = this.client.mergeContext(context);
        Mono<Response<Flux<ByteBuffer>>> mono = postWithResponseAsync(reservationOrderId, body, context);
        return this
            .client
            .<ReservationOrderResponseInner, ReservationOrderResponseInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                ReservationOrderResponseInner.class,
                ReservationOrderResponseInner.class,
                context);
    }

    /**
     * Return a reservation.
     *
     * <p>Return a reservation and get refund information.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for returning 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> beginPost(
        String reservationOrderId, RefundRequest body) {
        return this.beginPostAsync(reservationOrderId, body).getSyncPoller();
    }

    /**
     * Return a reservation.
     *
     * <p>Return a reservation and get refund information.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for returning 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> beginPost(
        String reservationOrderId, RefundRequest body, Context context) {
        return this.beginPostAsync(reservationOrderId, body, context).getSyncPoller();
    }

    /**
     * Return a reservation.
     *
     * <p>Return a reservation and get refund information.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for returning 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> postAsync(String reservationOrderId, RefundRequest body) {
        return beginPostAsync(reservationOrderId, body).last().flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Return a reservation.
     *
     * <p>Return a reservation and get refund information.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for returning 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> postAsync(
        String reservationOrderId, RefundRequest body, Context context) {
        return beginPostAsync(reservationOrderId, body, context).last().flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Return a reservation.
     *
     * <p>Return a reservation and get refund information.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for returning 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 post(String reservationOrderId, RefundRequest body) {
        return postAsync(reservationOrderId, body).block();
    }

    /**
     * Return a reservation.
     *
     * <p>Return a reservation and get refund information.
     *
     * @param reservationOrderId Order Id of the reservation.
     * @param body Information needed for returning 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 post(String reservationOrderId, RefundRequest body, Context context) {
        return postAsync(reservationOrderId, body, context).block();
    }
}
