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

package com.azure.resourcemanager.security.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.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.security.fluent.PricingsClient;
import com.azure.resourcemanager.security.fluent.models.PricingInner;
import com.azure.resourcemanager.security.fluent.models.PricingListInner;
import reactor.core.publisher.Mono;

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

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

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

    /**
     * The interface defining all the services for SecurityCenterPricings to be used by the proxy service to perform
     * REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "SecurityCenterPricin")
    public interface PricingsService {
        @Headers({"Content-Type: application/json"})
        @Get("/subscriptions/{subscriptionId}/providers/Microsoft.Security/pricings")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<PricingListInner>> list(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("subscriptionId") String subscriptionId,
            @HeaderParam("Accept") String accept,
            Context context);

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

        @Headers({"Content-Type: application/json"})
        @Put("/subscriptions/{subscriptionId}/providers/Microsoft.Security/pricings/{pricingName}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<PricingInner>> update(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("pricingName") String pricingName,
            @BodyParam("application/json") PricingInner pricing,
            @HeaderParam("Accept") String accept,
            Context context);
    }

    /**
     * Lists Microsoft Defender for Cloud pricing configurations 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 list of pricing configurations response along with {@link Response} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<PricingListInner>> 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 apiVersion = "2023-01-01";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .list(this.client.getEndpoint(), apiVersion, this.client.getSubscriptionId(), accept, context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Lists Microsoft Defender for Cloud pricing configurations 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 list of pricing configurations response along with {@link Response} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<PricingListInner>> 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 apiVersion = "2023-01-01";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service.list(this.client.getEndpoint(), apiVersion, this.client.getSubscriptionId(), accept, context);
    }

    /**
     * Lists Microsoft Defender for Cloud pricing configurations 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 list of pricing configurations response on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PricingListInner> listAsync() {
        return listWithResponseAsync().flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Lists Microsoft Defender for Cloud pricing configurations 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 list of pricing configurations response along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<PricingListInner> listWithResponse(Context context) {
        return listWithResponseAsync(context).block();
    }

    /**
     * Lists Microsoft Defender for Cloud pricing configurations 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 list of pricing configurations response.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public PricingListInner list() {
        return listWithResponse(Context.NONE).getValue();
    }

    /**
     * Gets a provided Microsoft Defender for Cloud pricing configuration in the subscription.
     *
     * @param pricingName name of the pricing configuration.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return a provided Microsoft Defender for Cloud pricing configuration in the subscription along with {@link
     *     Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<PricingInner>> getWithResponseAsync(String pricingName) {
        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 (pricingName == null) {
            return Mono.error(new IllegalArgumentException("Parameter pricingName is required and cannot be null."));
        }
        final String apiVersion = "2023-01-01";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .get(
                            this.client.getEndpoint(),
                            apiVersion,
                            this.client.getSubscriptionId(),
                            pricingName,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Gets a provided Microsoft Defender for Cloud pricing configuration in the subscription.
     *
     * @param pricingName name of the pricing configuration.
     * @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 a provided Microsoft Defender for Cloud pricing configuration in the subscription along with {@link
     *     Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<PricingInner>> getWithResponseAsync(String pricingName, 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 (pricingName == null) {
            return Mono.error(new IllegalArgumentException("Parameter pricingName is required and cannot be null."));
        }
        final String apiVersion = "2023-01-01";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .get(this.client.getEndpoint(), apiVersion, this.client.getSubscriptionId(), pricingName, accept, context);
    }

    /**
     * Gets a provided Microsoft Defender for Cloud pricing configuration in the subscription.
     *
     * @param pricingName name of the pricing configuration.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return a provided Microsoft Defender for Cloud pricing configuration in the subscription on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PricingInner> getAsync(String pricingName) {
        return getWithResponseAsync(pricingName).flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Gets a provided Microsoft Defender for Cloud pricing configuration in the subscription.
     *
     * @param pricingName name of the pricing configuration.
     * @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 a provided Microsoft Defender for Cloud pricing configuration in the subscription along with {@link
     *     Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<PricingInner> getWithResponse(String pricingName, Context context) {
        return getWithResponseAsync(pricingName, context).block();
    }

    /**
     * Gets a provided Microsoft Defender for Cloud pricing configuration in the subscription.
     *
     * @param pricingName name of the pricing configuration.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return a provided Microsoft Defender for Cloud pricing configuration in the subscription.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public PricingInner get(String pricingName) {
        return getWithResponse(pricingName, Context.NONE).getValue();
    }

    /**
     * Updates a provided Microsoft Defender for Cloud pricing configuration in the subscription.
     *
     * @param pricingName name of the pricing configuration.
     * @param pricing Pricing object.
     * @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 microsoft Defender for Cloud is provided in two pricing tiers: free and standard along with {@link
     *     Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<PricingInner>> updateWithResponseAsync(String pricingName, PricingInner pricing) {
        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 (pricingName == null) {
            return Mono.error(new IllegalArgumentException("Parameter pricingName is required and cannot be null."));
        }
        if (pricing == null) {
            return Mono.error(new IllegalArgumentException("Parameter pricing is required and cannot be null."));
        } else {
            pricing.validate();
        }
        final String apiVersion = "2023-01-01";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .update(
                            this.client.getEndpoint(),
                            apiVersion,
                            this.client.getSubscriptionId(),
                            pricingName,
                            pricing,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Updates a provided Microsoft Defender for Cloud pricing configuration in the subscription.
     *
     * @param pricingName name of the pricing configuration.
     * @param pricing Pricing object.
     * @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 microsoft Defender for Cloud is provided in two pricing tiers: free and standard along with {@link
     *     Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<PricingInner>> updateWithResponseAsync(
        String pricingName, PricingInner pricing, 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 (pricingName == null) {
            return Mono.error(new IllegalArgumentException("Parameter pricingName is required and cannot be null."));
        }
        if (pricing == null) {
            return Mono.error(new IllegalArgumentException("Parameter pricing is required and cannot be null."));
        } else {
            pricing.validate();
        }
        final String apiVersion = "2023-01-01";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .update(
                this.client.getEndpoint(),
                apiVersion,
                this.client.getSubscriptionId(),
                pricingName,
                pricing,
                accept,
                context);
    }

    /**
     * Updates a provided Microsoft Defender for Cloud pricing configuration in the subscription.
     *
     * @param pricingName name of the pricing configuration.
     * @param pricing Pricing object.
     * @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 microsoft Defender for Cloud is provided in two pricing tiers: free and standard on successful completion
     *     of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PricingInner> updateAsync(String pricingName, PricingInner pricing) {
        return updateWithResponseAsync(pricingName, pricing).flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Updates a provided Microsoft Defender for Cloud pricing configuration in the subscription.
     *
     * @param pricingName name of the pricing configuration.
     * @param pricing Pricing object.
     * @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 microsoft Defender for Cloud is provided in two pricing tiers: free and standard along with {@link
     *     Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<PricingInner> updateWithResponse(String pricingName, PricingInner pricing, Context context) {
        return updateWithResponseAsync(pricingName, pricing, context).block();
    }

    /**
     * Updates a provided Microsoft Defender for Cloud pricing configuration in the subscription.
     *
     * @param pricingName name of the pricing configuration.
     * @param pricing Pricing object.
     * @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 microsoft Defender for Cloud is provided in two pricing tiers: free and standard.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public PricingInner update(String pricingName, PricingInner pricing) {
        return updateWithResponse(pricingName, pricing, Context.NONE).getValue();
    }
}
