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

package com.azure.resourcemanager.marketplaceordering.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.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.marketplaceordering.fluent.MarketplaceAgreementsClient;
import com.azure.resourcemanager.marketplaceordering.fluent.models.AgreementTermsInner;
import com.azure.resourcemanager.marketplaceordering.models.OfferType;
import java.util.List;
import reactor.core.publisher.Mono;

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

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

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

    /**
     * The interface defining all the services for MarketplaceOrderingAgreementsMarketplaceAgreements to be used by the
     * proxy service to perform REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "MarketplaceOrderingA")
    private interface MarketplaceAgreementsService {
        @Headers({"Content-Type: application/json"})
        @Get(
            "/subscriptions/{subscriptionId}/providers/Microsoft.MarketplaceOrdering/offerTypes/{offerType}/publishers"
                + "/{publisherId}/offers/{offerId}/plans/{planId}/agreements/current")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<AgreementTermsInner>> get(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("offerType") OfferType offerType,
            @PathParam("publisherId") String publisherId,
            @PathParam("offerId") String offerId,
            @PathParam("planId") String planId,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Put(
            "/subscriptions/{subscriptionId}/providers/Microsoft.MarketplaceOrdering/offerTypes/{offerType}/publishers"
                + "/{publisherId}/offers/{offerId}/plans/{planId}/agreements/current")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<AgreementTermsInner>> create(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("offerType") OfferType offerType,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("publisherId") String publisherId,
            @PathParam("offerId") String offerId,
            @PathParam("planId") String planId,
            @BodyParam("application/json") AgreementTermsInner parameters,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Post(
            "/subscriptions/{subscriptionId}/providers/Microsoft.MarketplaceOrdering/agreements/{publisherId}/offers"
                + "/{offerId}/plans/{planId}/sign")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<AgreementTermsInner>> sign(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("publisherId") String publisherId,
            @PathParam("offerId") String offerId,
            @PathParam("planId") String planId,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Post(
            "/subscriptions/{subscriptionId}/providers/Microsoft.MarketplaceOrdering/agreements/{publisherId}/offers"
                + "/{offerId}/plans/{planId}/cancel")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<AgreementTermsInner>> cancel(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("publisherId") String publisherId,
            @PathParam("offerId") String offerId,
            @PathParam("planId") String planId,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get(
            "/subscriptions/{subscriptionId}/providers/Microsoft.MarketplaceOrdering/agreements/{publisherId}/offers"
                + "/{offerId}/plans/{planId}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<AgreementTermsInner>> getAgreement(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("publisherId") String publisherId,
            @PathParam("offerId") String offerId,
            @PathParam("planId") String planId,
            @HeaderParam("Accept") String accept,
            Context context);

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

    /**
     * Get marketplace terms.
     *
     * @param offerType Offer Type, currently only virtualmachine type is supported.
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 marketplace terms along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgreementTermsInner>> getWithResponseAsync(
        OfferType offerType, String publisherId, String offerId, String planId) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (offerType == null) {
            return Mono.error(new IllegalArgumentException("Parameter offerType is required and cannot be null."));
        }
        if (publisherId == null) {
            return Mono.error(new IllegalArgumentException("Parameter publisherId is required and cannot be null."));
        }
        if (offerId == null) {
            return Mono.error(new IllegalArgumentException("Parameter offerId is required and cannot be null."));
        }
        if (planId == null) {
            return Mono.error(new IllegalArgumentException("Parameter planId is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .get(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            this.client.getSubscriptionId(),
                            offerType,
                            publisherId,
                            offerId,
                            planId,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Get marketplace terms.
     *
     * @param offerType Offer Type, currently only virtualmachine type is supported.
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 marketplace terms along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgreementTermsInner>> getWithResponseAsync(
        OfferType offerType, String publisherId, String offerId, String planId, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (offerType == null) {
            return Mono.error(new IllegalArgumentException("Parameter offerType is required and cannot be null."));
        }
        if (publisherId == null) {
            return Mono.error(new IllegalArgumentException("Parameter publisherId is required and cannot be null."));
        }
        if (offerId == null) {
            return Mono.error(new IllegalArgumentException("Parameter offerId is required and cannot be null."));
        }
        if (planId == null) {
            return Mono.error(new IllegalArgumentException("Parameter planId is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .get(
                this.client.getEndpoint(),
                this.client.getApiVersion(),
                this.client.getSubscriptionId(),
                offerType,
                publisherId,
                offerId,
                planId,
                accept,
                context);
    }

    /**
     * Get marketplace terms.
     *
     * @param offerType Offer Type, currently only virtualmachine type is supported.
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 marketplace terms on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<AgreementTermsInner> getAsync(OfferType offerType, String publisherId, String offerId, String planId) {
        return getWithResponseAsync(offerType, publisherId, offerId, planId)
            .flatMap(
                (Response<AgreementTermsInner> res) -> {
                    if (res.getValue() != null) {
                        return Mono.just(res.getValue());
                    } else {
                        return Mono.empty();
                    }
                });
    }

    /**
     * Get marketplace terms.
     *
     * @param offerType Offer Type, currently only virtualmachine type is supported.
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 marketplace terms.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public AgreementTermsInner get(OfferType offerType, String publisherId, String offerId, String planId) {
        return getAsync(offerType, publisherId, offerId, planId).block();
    }

    /**
     * Get marketplace terms.
     *
     * @param offerType Offer Type, currently only virtualmachine type is supported.
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 marketplace terms along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<AgreementTermsInner> getWithResponse(
        OfferType offerType, String publisherId, String offerId, String planId, Context context) {
        return getWithResponseAsync(offerType, publisherId, offerId, planId, context).block();
    }

    /**
     * Save marketplace terms.
     *
     * @param offerType Offer Type, currently only virtualmachine type is supported.
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @param parameters Parameters supplied to the Create Marketplace Terms 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 terms properties for provided Publisher/Offer/Plan tuple along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgreementTermsInner>> createWithResponseAsync(
        OfferType offerType, String publisherId, String offerId, String planId, AgreementTermsInner parameters) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (offerType == null) {
            return Mono.error(new IllegalArgumentException("Parameter offerType is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (publisherId == null) {
            return Mono.error(new IllegalArgumentException("Parameter publisherId is required and cannot be null."));
        }
        if (offerId == null) {
            return Mono.error(new IllegalArgumentException("Parameter offerId is required and cannot be null."));
        }
        if (planId == null) {
            return Mono.error(new IllegalArgumentException("Parameter planId is required and cannot be null."));
        }
        if (parameters == null) {
            return Mono.error(new IllegalArgumentException("Parameter parameters is required and cannot be null."));
        } else {
            parameters.validate();
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .create(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            offerType,
                            this.client.getSubscriptionId(),
                            publisherId,
                            offerId,
                            planId,
                            parameters,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Save marketplace terms.
     *
     * @param offerType Offer Type, currently only virtualmachine type is supported.
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @param parameters Parameters supplied to the Create Marketplace Terms operation.
     * @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 terms properties for provided Publisher/Offer/Plan tuple along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgreementTermsInner>> createWithResponseAsync(
        OfferType offerType,
        String publisherId,
        String offerId,
        String planId,
        AgreementTermsInner parameters,
        Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (offerType == null) {
            return Mono.error(new IllegalArgumentException("Parameter offerType is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (publisherId == null) {
            return Mono.error(new IllegalArgumentException("Parameter publisherId is required and cannot be null."));
        }
        if (offerId == null) {
            return Mono.error(new IllegalArgumentException("Parameter offerId is required and cannot be null."));
        }
        if (planId == null) {
            return Mono.error(new IllegalArgumentException("Parameter planId is required and cannot be null."));
        }
        if (parameters == null) {
            return Mono.error(new IllegalArgumentException("Parameter parameters is required and cannot be null."));
        } else {
            parameters.validate();
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .create(
                this.client.getEndpoint(),
                this.client.getApiVersion(),
                offerType,
                this.client.getSubscriptionId(),
                publisherId,
                offerId,
                planId,
                parameters,
                accept,
                context);
    }

    /**
     * Save marketplace terms.
     *
     * @param offerType Offer Type, currently only virtualmachine type is supported.
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @param parameters Parameters supplied to the Create Marketplace Terms 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 terms properties for provided Publisher/Offer/Plan tuple on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<AgreementTermsInner> createAsync(
        OfferType offerType, String publisherId, String offerId, String planId, AgreementTermsInner parameters) {
        return createWithResponseAsync(offerType, publisherId, offerId, planId, parameters)
            .flatMap(
                (Response<AgreementTermsInner> res) -> {
                    if (res.getValue() != null) {
                        return Mono.just(res.getValue());
                    } else {
                        return Mono.empty();
                    }
                });
    }

    /**
     * Save marketplace terms.
     *
     * @param offerType Offer Type, currently only virtualmachine type is supported.
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @param parameters Parameters supplied to the Create Marketplace Terms 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 terms properties for provided Publisher/Offer/Plan tuple.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public AgreementTermsInner create(
        OfferType offerType, String publisherId, String offerId, String planId, AgreementTermsInner parameters) {
        return createAsync(offerType, publisherId, offerId, planId, parameters).block();
    }

    /**
     * Save marketplace terms.
     *
     * @param offerType Offer Type, currently only virtualmachine type is supported.
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @param parameters Parameters supplied to the Create Marketplace Terms operation.
     * @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 terms properties for provided Publisher/Offer/Plan tuple along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<AgreementTermsInner> createWithResponse(
        OfferType offerType,
        String publisherId,
        String offerId,
        String planId,
        AgreementTermsInner parameters,
        Context context) {
        return createWithResponseAsync(offerType, publisherId, offerId, planId, parameters, context).block();
    }

    /**
     * Sign marketplace terms.
     *
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 terms properties for provided Publisher/Offer/Plan tuple along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgreementTermsInner>> signWithResponseAsync(
        String publisherId, String offerId, String planId) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (publisherId == null) {
            return Mono.error(new IllegalArgumentException("Parameter publisherId is required and cannot be null."));
        }
        if (offerId == null) {
            return Mono.error(new IllegalArgumentException("Parameter offerId is required and cannot be null."));
        }
        if (planId == null) {
            return Mono.error(new IllegalArgumentException("Parameter planId is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .sign(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            this.client.getSubscriptionId(),
                            publisherId,
                            offerId,
                            planId,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Sign marketplace terms.
     *
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 terms properties for provided Publisher/Offer/Plan tuple along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgreementTermsInner>> signWithResponseAsync(
        String publisherId, String offerId, String planId, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (publisherId == null) {
            return Mono.error(new IllegalArgumentException("Parameter publisherId is required and cannot be null."));
        }
        if (offerId == null) {
            return Mono.error(new IllegalArgumentException("Parameter offerId is required and cannot be null."));
        }
        if (planId == null) {
            return Mono.error(new IllegalArgumentException("Parameter planId is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .sign(
                this.client.getEndpoint(),
                this.client.getApiVersion(),
                this.client.getSubscriptionId(),
                publisherId,
                offerId,
                planId,
                accept,
                context);
    }

    /**
     * Sign marketplace terms.
     *
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 terms properties for provided Publisher/Offer/Plan tuple on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<AgreementTermsInner> signAsync(String publisherId, String offerId, String planId) {
        return signWithResponseAsync(publisherId, offerId, planId)
            .flatMap(
                (Response<AgreementTermsInner> res) -> {
                    if (res.getValue() != null) {
                        return Mono.just(res.getValue());
                    } else {
                        return Mono.empty();
                    }
                });
    }

    /**
     * Sign marketplace terms.
     *
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 terms properties for provided Publisher/Offer/Plan tuple.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public AgreementTermsInner sign(String publisherId, String offerId, String planId) {
        return signAsync(publisherId, offerId, planId).block();
    }

    /**
     * Sign marketplace terms.
     *
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 terms properties for provided Publisher/Offer/Plan tuple along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<AgreementTermsInner> signWithResponse(
        String publisherId, String offerId, String planId, Context context) {
        return signWithResponseAsync(publisherId, offerId, planId, context).block();
    }

    /**
     * Cancel marketplace terms.
     *
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 terms properties for provided Publisher/Offer/Plan tuple along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgreementTermsInner>> cancelWithResponseAsync(
        String publisherId, String offerId, String planId) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (publisherId == null) {
            return Mono.error(new IllegalArgumentException("Parameter publisherId is required and cannot be null."));
        }
        if (offerId == null) {
            return Mono.error(new IllegalArgumentException("Parameter offerId is required and cannot be null."));
        }
        if (planId == null) {
            return Mono.error(new IllegalArgumentException("Parameter planId is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .cancel(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            this.client.getSubscriptionId(),
                            publisherId,
                            offerId,
                            planId,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Cancel marketplace terms.
     *
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 terms properties for provided Publisher/Offer/Plan tuple along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgreementTermsInner>> cancelWithResponseAsync(
        String publisherId, String offerId, String planId, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (publisherId == null) {
            return Mono.error(new IllegalArgumentException("Parameter publisherId is required and cannot be null."));
        }
        if (offerId == null) {
            return Mono.error(new IllegalArgumentException("Parameter offerId is required and cannot be null."));
        }
        if (planId == null) {
            return Mono.error(new IllegalArgumentException("Parameter planId is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .cancel(
                this.client.getEndpoint(),
                this.client.getApiVersion(),
                this.client.getSubscriptionId(),
                publisherId,
                offerId,
                planId,
                accept,
                context);
    }

    /**
     * Cancel marketplace terms.
     *
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 terms properties for provided Publisher/Offer/Plan tuple on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<AgreementTermsInner> cancelAsync(String publisherId, String offerId, String planId) {
        return cancelWithResponseAsync(publisherId, offerId, planId)
            .flatMap(
                (Response<AgreementTermsInner> res) -> {
                    if (res.getValue() != null) {
                        return Mono.just(res.getValue());
                    } else {
                        return Mono.empty();
                    }
                });
    }

    /**
     * Cancel marketplace terms.
     *
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 terms properties for provided Publisher/Offer/Plan tuple.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public AgreementTermsInner cancel(String publisherId, String offerId, String planId) {
        return cancelAsync(publisherId, offerId, planId).block();
    }

    /**
     * Cancel marketplace terms.
     *
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 terms properties for provided Publisher/Offer/Plan tuple along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<AgreementTermsInner> cancelWithResponse(
        String publisherId, String offerId, String planId, Context context) {
        return cancelWithResponseAsync(publisherId, offerId, planId, context).block();
    }

    /**
     * Get marketplace agreement.
     *
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 marketplace agreement along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgreementTermsInner>> getAgreementWithResponseAsync(
        String publisherId, String offerId, String planId) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (publisherId == null) {
            return Mono.error(new IllegalArgumentException("Parameter publisherId is required and cannot be null."));
        }
        if (offerId == null) {
            return Mono.error(new IllegalArgumentException("Parameter offerId is required and cannot be null."));
        }
        if (planId == null) {
            return Mono.error(new IllegalArgumentException("Parameter planId is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .getAgreement(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            this.client.getSubscriptionId(),
                            publisherId,
                            offerId,
                            planId,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Get marketplace agreement.
     *
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 marketplace agreement along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgreementTermsInner>> getAgreementWithResponseAsync(
        String publisherId, String offerId, String planId, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        if (publisherId == null) {
            return Mono.error(new IllegalArgumentException("Parameter publisherId is required and cannot be null."));
        }
        if (offerId == null) {
            return Mono.error(new IllegalArgumentException("Parameter offerId is required and cannot be null."));
        }
        if (planId == null) {
            return Mono.error(new IllegalArgumentException("Parameter planId is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .getAgreement(
                this.client.getEndpoint(),
                this.client.getApiVersion(),
                this.client.getSubscriptionId(),
                publisherId,
                offerId,
                planId,
                accept,
                context);
    }

    /**
     * Get marketplace agreement.
     *
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 marketplace agreement on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<AgreementTermsInner> getAgreementAsync(String publisherId, String offerId, String planId) {
        return getAgreementWithResponseAsync(publisherId, offerId, planId)
            .flatMap(
                (Response<AgreementTermsInner> res) -> {
                    if (res.getValue() != null) {
                        return Mono.just(res.getValue());
                    } else {
                        return Mono.empty();
                    }
                });
    }

    /**
     * Get marketplace agreement.
     *
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 marketplace agreement.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public AgreementTermsInner getAgreement(String publisherId, String offerId, String planId) {
        return getAgreementAsync(publisherId, offerId, planId).block();
    }

    /**
     * Get marketplace agreement.
     *
     * @param publisherId Publisher identifier string of image being deployed.
     * @param offerId Offer identifier string of image being deployed.
     * @param planId Plan identifier string of image being deployed.
     * @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 marketplace agreement along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<AgreementTermsInner> getAgreementWithResponse(
        String publisherId, String offerId, String planId, Context context) {
        return getAgreementWithResponseAsync(publisherId, offerId, planId, context).block();
    }

    /**
     * List marketplace agreements in the subscription.
     *
     * @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 array of AgreementTerms along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<List<AgreementTermsInner>>> listWithResponseAsync() {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .list(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            this.client.getSubscriptionId(),
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * List marketplace agreements in the subscription.
     *
     * @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 array of AgreementTerms along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<List<AgreementTermsInner>>> listWithResponseAsync(Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (this.client.getSubscriptionId() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getSubscriptionId() is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .list(
                this.client.getEndpoint(),
                this.client.getApiVersion(),
                this.client.getSubscriptionId(),
                accept,
                context);
    }

    /**
     * List marketplace agreements in the subscription.
     *
     * @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 array of AgreementTerms on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<List<AgreementTermsInner>> listAsync() {
        return listWithResponseAsync()
            .flatMap(
                (Response<List<AgreementTermsInner>> res) -> {
                    if (res.getValue() != null) {
                        return Mono.just(res.getValue());
                    } else {
                        return Mono.empty();
                    }
                });
    }

    /**
     * List marketplace agreements in the subscription.
     *
     * @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 array of AgreementTerms.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public List<AgreementTermsInner> list() {
        return listAsync().block();
    }

    /**
     * List marketplace agreements in the subscription.
     *
     * @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 array of AgreementTerms along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<List<AgreementTermsInner>> listWithResponse(Context context) {
        return listWithResponseAsync(context).block();
    }
}
