// 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.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.CalculateExchangesClient;
import com.azure.resourcemanager.reservations.fluent.models.CalculateExchangeOperationResultResponseInner;
import com.azure.resourcemanager.reservations.models.CalculateExchangeRequest;
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 CalculateExchangesClient. */
public final class CalculateExchangesClientImpl implements CalculateExchangesClient {
    /** The proxy service used to perform REST calls. */
    private final CalculateExchangesService service;

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

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

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

    /**
     * Calculates the refund amounts and price of the new purchases.
     *
     * <p>Calculates price for exchanging `Reservations` if there are no policy errors.
     *
     * @param body Request containing purchases and refunds that need to be executed.
     * @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 calculateExchange operation result along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> postWithResponseAsync(CalculateExchangeRequest 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.post(this.client.getEndpoint(), apiVersion, body, accept, context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Calculates the refund amounts and price of the new purchases.
     *
     * <p>Calculates price for exchanging `Reservations` if there are no policy errors.
     *
     * @param body Request containing purchases and refunds that need to be executed.
     * @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 calculateExchange operation result along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> postWithResponseAsync(CalculateExchangeRequest 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.post(this.client.getEndpoint(), apiVersion, body, accept, context);
    }

    /**
     * Calculates the refund amounts and price of the new purchases.
     *
     * <p>Calculates price for exchanging `Reservations` if there are no policy errors.
     *
     * @param body Request containing purchases and refunds that need to be executed.
     * @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 calculateExchange operation result.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<
            PollResult<CalculateExchangeOperationResultResponseInner>, CalculateExchangeOperationResultResponseInner>
        beginPostAsync(CalculateExchangeRequest body) {
        Mono<Response<Flux<ByteBuffer>>> mono = postWithResponseAsync(body);
        return this
            .client
            .<CalculateExchangeOperationResultResponseInner, CalculateExchangeOperationResultResponseInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                CalculateExchangeOperationResultResponseInner.class,
                CalculateExchangeOperationResultResponseInner.class,
                this.client.getContext());
    }

    /**
     * Calculates the refund amounts and price of the new purchases.
     *
     * <p>Calculates price for exchanging `Reservations` if there are no policy errors.
     *
     * @param body Request containing purchases and refunds that need to be executed.
     * @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 calculateExchange operation result.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<
            PollResult<CalculateExchangeOperationResultResponseInner>, CalculateExchangeOperationResultResponseInner>
        beginPostAsync(CalculateExchangeRequest body, Context context) {
        context = this.client.mergeContext(context);
        Mono<Response<Flux<ByteBuffer>>> mono = postWithResponseAsync(body, context);
        return this
            .client
            .<CalculateExchangeOperationResultResponseInner, CalculateExchangeOperationResultResponseInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                CalculateExchangeOperationResultResponseInner.class,
                CalculateExchangeOperationResultResponseInner.class,
                context);
    }

    /**
     * Calculates the refund amounts and price of the new purchases.
     *
     * <p>Calculates price for exchanging `Reservations` if there are no policy errors.
     *
     * @param body Request containing purchases and refunds that need to be executed.
     * @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 calculateExchange operation result.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<
            PollResult<CalculateExchangeOperationResultResponseInner>, CalculateExchangeOperationResultResponseInner>
        beginPost(CalculateExchangeRequest body) {
        return this.beginPostAsync(body).getSyncPoller();
    }

    /**
     * Calculates the refund amounts and price of the new purchases.
     *
     * <p>Calculates price for exchanging `Reservations` if there are no policy errors.
     *
     * @param body Request containing purchases and refunds that need to be executed.
     * @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 calculateExchange operation result.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<
            PollResult<CalculateExchangeOperationResultResponseInner>, CalculateExchangeOperationResultResponseInner>
        beginPost(CalculateExchangeRequest body, Context context) {
        return this.beginPostAsync(body, context).getSyncPoller();
    }

    /**
     * Calculates the refund amounts and price of the new purchases.
     *
     * <p>Calculates price for exchanging `Reservations` if there are no policy errors.
     *
     * @param body Request containing purchases and refunds that need to be executed.
     * @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 calculateExchange operation result on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<CalculateExchangeOperationResultResponseInner> postAsync(CalculateExchangeRequest body) {
        return beginPostAsync(body).last().flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Calculates the refund amounts and price of the new purchases.
     *
     * <p>Calculates price for exchanging `Reservations` if there are no policy errors.
     *
     * @param body Request containing purchases and refunds that need to be executed.
     * @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 calculateExchange operation result on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<CalculateExchangeOperationResultResponseInner> postAsync(
        CalculateExchangeRequest body, Context context) {
        return beginPostAsync(body, context).last().flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Calculates the refund amounts and price of the new purchases.
     *
     * <p>Calculates price for exchanging `Reservations` if there are no policy errors.
     *
     * @param body Request containing purchases and refunds that need to be executed.
     * @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 calculateExchange operation result.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public CalculateExchangeOperationResultResponseInner post(CalculateExchangeRequest body) {
        return postAsync(body).block();
    }

    /**
     * Calculates the refund amounts and price of the new purchases.
     *
     * <p>Calculates price for exchanging `Reservations` if there are no policy errors.
     *
     * @param body Request containing purchases and refunds that need to be executed.
     * @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 calculateExchange operation result.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public CalculateExchangeOperationResultResponseInner post(CalculateExchangeRequest body, Context context) {
        return postAsync(body, context).block();
    }
}
