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

package com.azure.resourcemanager.hybridconnectivity.implementation;

import com.azure.core.annotation.BodyParam;
import com.azure.core.annotation.Delete;
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.Patch;
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.PagedFlux;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.PagedResponse;
import com.azure.core.http.rest.PagedResponseBase;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.RestProxy;
import com.azure.core.management.exception.ManagementException;
import com.azure.core.util.Context;
import com.azure.core.util.FluxUtil;
import com.azure.resourcemanager.hybridconnectivity.fluent.ServiceConfigurationsClient;
import com.azure.resourcemanager.hybridconnectivity.fluent.models.ServiceConfigurationResourceInner;
import com.azure.resourcemanager.hybridconnectivity.models.ServiceConfigurationList;
import com.azure.resourcemanager.hybridconnectivity.models.ServiceConfigurationResourcePatch;
import reactor.core.publisher.Mono;

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

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

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

    /**
     * The interface defining all the services for HybridConnectivityManagementApiServiceConfigurations to be used by
     * the proxy service to perform REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "HybridConnectivityMa")
    public interface ServiceConfigurationsService {
        @Headers({"Content-Type: application/json"})
        @Get("/{resourceUri}/providers/Microsoft.HybridConnectivity/endpoints/{endpointName}/serviceConfigurations")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<ServiceConfigurationList>> listByEndpointResource(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam(value = "resourceUri", encoded = true) String resourceUri,
            @PathParam(value = "endpointName", encoded = true) String endpointName,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get(
            "/{resourceUri}/providers/Microsoft.HybridConnectivity/endpoints/{endpointName}/serviceConfigurations/{serviceConfigurationName}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<ServiceConfigurationResourceInner>> get(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam(value = "resourceUri", encoded = true) String resourceUri,
            @PathParam(value = "endpointName", encoded = true) String endpointName,
            @PathParam(value = "serviceConfigurationName", encoded = true) String serviceConfigurationName,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Put(
            "/{resourceUri}/providers/Microsoft.HybridConnectivity/endpoints/{endpointName}/serviceConfigurations/{serviceConfigurationName}")
        @ExpectedResponses({200, 201})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<ServiceConfigurationResourceInner>> createOrupdate(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam(value = "resourceUri", encoded = true) String resourceUri,
            @PathParam(value = "endpointName", encoded = true) String endpointName,
            @PathParam(value = "serviceConfigurationName", encoded = true) String serviceConfigurationName,
            @BodyParam("application/json") ServiceConfigurationResourceInner serviceConfigurationResource,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Patch(
            "/{resourceUri}/providers/Microsoft.HybridConnectivity/endpoints/{endpointName}/serviceConfigurations/{serviceConfigurationName}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<ServiceConfigurationResourceInner>> update(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam(value = "resourceUri", encoded = true) String resourceUri,
            @PathParam(value = "endpointName", encoded = true) String endpointName,
            @PathParam(value = "serviceConfigurationName", encoded = true) String serviceConfigurationName,
            @BodyParam("application/json") ServiceConfigurationResourcePatch serviceConfigurationResource,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Delete(
            "/{resourceUri}/providers/Microsoft.HybridConnectivity/endpoints/{endpointName}/serviceConfigurations/{serviceConfigurationName}")
        @ExpectedResponses({200, 204})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<Void>> delete(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam(value = "resourceUri", encoded = true) String resourceUri,
            @PathParam(value = "endpointName", encoded = true) String endpointName,
            @PathParam(value = "serviceConfigurationName", encoded = true) String serviceConfigurationName,
            @HeaderParam("Accept") String accept,
            Context context);

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

    /**
     * Lists of all the services associated with endpoint resource.
     *
     * <p>API to enumerate registered services in service configurations under a Endpoint Resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @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 paginated list of serviceConfigurations along with {@link PagedResponse} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<ServiceConfigurationResourceInner>> listByEndpointResourceSinglePageAsync(
        String resourceUri, String endpointName) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (resourceUri == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceUri is required and cannot be null."));
        }
        if (endpointName == null) {
            return Mono.error(new IllegalArgumentException("Parameter endpointName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .listByEndpointResource(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            resourceUri,
                            endpointName,
                            accept,
                            context))
            .<PagedResponse<ServiceConfigurationResourceInner>>map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Lists of all the services associated with endpoint resource.
     *
     * <p>API to enumerate registered services in service configurations under a Endpoint Resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @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 paginated list of serviceConfigurations along with {@link PagedResponse} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<ServiceConfigurationResourceInner>> listByEndpointResourceSinglePageAsync(
        String resourceUri, String endpointName, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (resourceUri == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceUri is required and cannot be null."));
        }
        if (endpointName == null) {
            return Mono.error(new IllegalArgumentException("Parameter endpointName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .listByEndpointResource(
                this.client.getEndpoint(), this.client.getApiVersion(), resourceUri, endpointName, accept, context)
            .map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null));
    }

    /**
     * Lists of all the services associated with endpoint resource.
     *
     * <p>API to enumerate registered services in service configurations under a Endpoint Resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @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 paginated list of serviceConfigurations as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<ServiceConfigurationResourceInner> listByEndpointResourceAsync(
        String resourceUri, String endpointName) {
        return new PagedFlux<>(
            () -> listByEndpointResourceSinglePageAsync(resourceUri, endpointName),
            nextLink -> listByEndpointResourceNextSinglePageAsync(nextLink));
    }

    /**
     * Lists of all the services associated with endpoint resource.
     *
     * <p>API to enumerate registered services in service configurations under a Endpoint Resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @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 paginated list of serviceConfigurations as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<ServiceConfigurationResourceInner> listByEndpointResourceAsync(
        String resourceUri, String endpointName, Context context) {
        return new PagedFlux<>(
            () -> listByEndpointResourceSinglePageAsync(resourceUri, endpointName, context),
            nextLink -> listByEndpointResourceNextSinglePageAsync(nextLink, context));
    }

    /**
     * Lists of all the services associated with endpoint resource.
     *
     * <p>API to enumerate registered services in service configurations under a Endpoint Resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @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 paginated list of serviceConfigurations as paginated response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<ServiceConfigurationResourceInner> listByEndpointResource(
        String resourceUri, String endpointName) {
        return new PagedIterable<>(listByEndpointResourceAsync(resourceUri, endpointName));
    }

    /**
     * Lists of all the services associated with endpoint resource.
     *
     * <p>API to enumerate registered services in service configurations under a Endpoint Resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @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 paginated list of serviceConfigurations as paginated response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<ServiceConfigurationResourceInner> listByEndpointResource(
        String resourceUri, String endpointName, Context context) {
        return new PagedIterable<>(listByEndpointResourceAsync(resourceUri, endpointName, context));
    }

    /**
     * Gets the details about the service to the resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the details about the service to the resource along with {@link Response} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ServiceConfigurationResourceInner>> getWithResponseAsync(
        String resourceUri, String endpointName, String serviceConfigurationName) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (resourceUri == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceUri is required and cannot be null."));
        }
        if (endpointName == null) {
            return Mono.error(new IllegalArgumentException("Parameter endpointName is required and cannot be null."));
        }
        if (serviceConfigurationName == null) {
            return Mono
                .error(
                    new IllegalArgumentException("Parameter serviceConfigurationName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .get(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            resourceUri,
                            endpointName,
                            serviceConfigurationName,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Gets the details about the service to the resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the details about the service to the resource along with {@link Response} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ServiceConfigurationResourceInner>> getWithResponseAsync(
        String resourceUri, String endpointName, String serviceConfigurationName, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (resourceUri == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceUri is required and cannot be null."));
        }
        if (endpointName == null) {
            return Mono.error(new IllegalArgumentException("Parameter endpointName is required and cannot be null."));
        }
        if (serviceConfigurationName == null) {
            return Mono
                .error(
                    new IllegalArgumentException("Parameter serviceConfigurationName 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(),
                resourceUri,
                endpointName,
                serviceConfigurationName,
                accept,
                context);
    }

    /**
     * Gets the details about the service to the resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the details about the service to the resource on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ServiceConfigurationResourceInner> getAsync(
        String resourceUri, String endpointName, String serviceConfigurationName) {
        return getWithResponseAsync(resourceUri, endpointName, serviceConfigurationName)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Gets the details about the service to the resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the details about the service to the resource along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<ServiceConfigurationResourceInner> getWithResponse(
        String resourceUri, String endpointName, String serviceConfigurationName, Context context) {
        return getWithResponseAsync(resourceUri, endpointName, serviceConfigurationName, context).block();
    }

    /**
     * Gets the details about the service to the resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the details about the service to the resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ServiceConfigurationResourceInner get(
        String resourceUri, String endpointName, String serviceConfigurationName) {
        return getWithResponse(resourceUri, endpointName, serviceConfigurationName, Context.NONE).getValue();
    }

    /**
     * Create or update a service in serviceConfiguration for the endpoint resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @param serviceConfigurationResource Service details.
     * @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 service configuration details associated with the target resource along with {@link Response} on
     *     successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ServiceConfigurationResourceInner>> createOrupdateWithResponseAsync(
        String resourceUri,
        String endpointName,
        String serviceConfigurationName,
        ServiceConfigurationResourceInner serviceConfigurationResource) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (resourceUri == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceUri is required and cannot be null."));
        }
        if (endpointName == null) {
            return Mono.error(new IllegalArgumentException("Parameter endpointName is required and cannot be null."));
        }
        if (serviceConfigurationName == null) {
            return Mono
                .error(
                    new IllegalArgumentException("Parameter serviceConfigurationName is required and cannot be null."));
        }
        if (serviceConfigurationResource == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter serviceConfigurationResource is required and cannot be null."));
        } else {
            serviceConfigurationResource.validate();
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .createOrupdate(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            resourceUri,
                            endpointName,
                            serviceConfigurationName,
                            serviceConfigurationResource,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Create or update a service in serviceConfiguration for the endpoint resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @param serviceConfigurationResource Service details.
     * @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 service configuration details associated with the target resource along with {@link Response} on
     *     successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ServiceConfigurationResourceInner>> createOrupdateWithResponseAsync(
        String resourceUri,
        String endpointName,
        String serviceConfigurationName,
        ServiceConfigurationResourceInner serviceConfigurationResource,
        Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (resourceUri == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceUri is required and cannot be null."));
        }
        if (endpointName == null) {
            return Mono.error(new IllegalArgumentException("Parameter endpointName is required and cannot be null."));
        }
        if (serviceConfigurationName == null) {
            return Mono
                .error(
                    new IllegalArgumentException("Parameter serviceConfigurationName is required and cannot be null."));
        }
        if (serviceConfigurationResource == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter serviceConfigurationResource is required and cannot be null."));
        } else {
            serviceConfigurationResource.validate();
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .createOrupdate(
                this.client.getEndpoint(),
                this.client.getApiVersion(),
                resourceUri,
                endpointName,
                serviceConfigurationName,
                serviceConfigurationResource,
                accept,
                context);
    }

    /**
     * Create or update a service in serviceConfiguration for the endpoint resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @param serviceConfigurationResource Service details.
     * @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 service configuration details associated with the target resource on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ServiceConfigurationResourceInner> createOrupdateAsync(
        String resourceUri,
        String endpointName,
        String serviceConfigurationName,
        ServiceConfigurationResourceInner serviceConfigurationResource) {
        return createOrupdateWithResponseAsync(
                resourceUri, endpointName, serviceConfigurationName, serviceConfigurationResource)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Create or update a service in serviceConfiguration for the endpoint resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @param serviceConfigurationResource Service details.
     * @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 service configuration details associated with the target resource along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<ServiceConfigurationResourceInner> createOrupdateWithResponse(
        String resourceUri,
        String endpointName,
        String serviceConfigurationName,
        ServiceConfigurationResourceInner serviceConfigurationResource,
        Context context) {
        return createOrupdateWithResponseAsync(
                resourceUri, endpointName, serviceConfigurationName, serviceConfigurationResource, context)
            .block();
    }

    /**
     * Create or update a service in serviceConfiguration for the endpoint resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @param serviceConfigurationResource Service details.
     * @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 service configuration details associated with the target resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ServiceConfigurationResourceInner createOrupdate(
        String resourceUri,
        String endpointName,
        String serviceConfigurationName,
        ServiceConfigurationResourceInner serviceConfigurationResource) {
        return createOrupdateWithResponse(
                resourceUri, endpointName, serviceConfigurationName, serviceConfigurationResource, Context.NONE)
            .getValue();
    }

    /**
     * Update the service details in the service configurations of the target resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @param serviceConfigurationResource Service details.
     * @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 service configuration details associated with the target resource along with {@link Response} on
     *     successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ServiceConfigurationResourceInner>> updateWithResponseAsync(
        String resourceUri,
        String endpointName,
        String serviceConfigurationName,
        ServiceConfigurationResourcePatch serviceConfigurationResource) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (resourceUri == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceUri is required and cannot be null."));
        }
        if (endpointName == null) {
            return Mono.error(new IllegalArgumentException("Parameter endpointName is required and cannot be null."));
        }
        if (serviceConfigurationName == null) {
            return Mono
                .error(
                    new IllegalArgumentException("Parameter serviceConfigurationName is required and cannot be null."));
        }
        if (serviceConfigurationResource == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter serviceConfigurationResource is required and cannot be null."));
        } else {
            serviceConfigurationResource.validate();
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .update(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            resourceUri,
                            endpointName,
                            serviceConfigurationName,
                            serviceConfigurationResource,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Update the service details in the service configurations of the target resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @param serviceConfigurationResource Service details.
     * @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 service configuration details associated with the target resource along with {@link Response} on
     *     successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ServiceConfigurationResourceInner>> updateWithResponseAsync(
        String resourceUri,
        String endpointName,
        String serviceConfigurationName,
        ServiceConfigurationResourcePatch serviceConfigurationResource,
        Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (resourceUri == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceUri is required and cannot be null."));
        }
        if (endpointName == null) {
            return Mono.error(new IllegalArgumentException("Parameter endpointName is required and cannot be null."));
        }
        if (serviceConfigurationName == null) {
            return Mono
                .error(
                    new IllegalArgumentException("Parameter serviceConfigurationName is required and cannot be null."));
        }
        if (serviceConfigurationResource == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter serviceConfigurationResource is required and cannot be null."));
        } else {
            serviceConfigurationResource.validate();
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .update(
                this.client.getEndpoint(),
                this.client.getApiVersion(),
                resourceUri,
                endpointName,
                serviceConfigurationName,
                serviceConfigurationResource,
                accept,
                context);
    }

    /**
     * Update the service details in the service configurations of the target resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @param serviceConfigurationResource Service details.
     * @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 service configuration details associated with the target resource on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ServiceConfigurationResourceInner> updateAsync(
        String resourceUri,
        String endpointName,
        String serviceConfigurationName,
        ServiceConfigurationResourcePatch serviceConfigurationResource) {
        return updateWithResponseAsync(
                resourceUri, endpointName, serviceConfigurationName, serviceConfigurationResource)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Update the service details in the service configurations of the target resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @param serviceConfigurationResource Service details.
     * @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 service configuration details associated with the target resource along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<ServiceConfigurationResourceInner> updateWithResponse(
        String resourceUri,
        String endpointName,
        String serviceConfigurationName,
        ServiceConfigurationResourcePatch serviceConfigurationResource,
        Context context) {
        return updateWithResponseAsync(
                resourceUri, endpointName, serviceConfigurationName, serviceConfigurationResource, context)
            .block();
    }

    /**
     * Update the service details in the service configurations of the target resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @param serviceConfigurationResource Service details.
     * @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 service configuration details associated with the target resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ServiceConfigurationResourceInner update(
        String resourceUri,
        String endpointName,
        String serviceConfigurationName,
        ServiceConfigurationResourcePatch serviceConfigurationResource) {
        return updateWithResponse(
                resourceUri, endpointName, serviceConfigurationName, serviceConfigurationResource, Context.NONE)
            .getValue();
    }

    /**
     * Deletes the service details to the target resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @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 Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Void>> deleteWithResponseAsync(
        String resourceUri, String endpointName, String serviceConfigurationName) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (resourceUri == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceUri is required and cannot be null."));
        }
        if (endpointName == null) {
            return Mono.error(new IllegalArgumentException("Parameter endpointName is required and cannot be null."));
        }
        if (serviceConfigurationName == null) {
            return Mono
                .error(
                    new IllegalArgumentException("Parameter serviceConfigurationName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .delete(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            resourceUri,
                            endpointName,
                            serviceConfigurationName,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Deletes the service details to the target resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @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 Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Void>> deleteWithResponseAsync(
        String resourceUri, String endpointName, String serviceConfigurationName, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (resourceUri == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceUri is required and cannot be null."));
        }
        if (endpointName == null) {
            return Mono.error(new IllegalArgumentException("Parameter endpointName is required and cannot be null."));
        }
        if (serviceConfigurationName == null) {
            return Mono
                .error(
                    new IllegalArgumentException("Parameter serviceConfigurationName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .delete(
                this.client.getEndpoint(),
                this.client.getApiVersion(),
                resourceUri,
                endpointName,
                serviceConfigurationName,
                accept,
                context);
    }

    /**
     * Deletes the service details to the target resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @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 {@link Mono} that completes when a successful response is received.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Void> deleteAsync(String resourceUri, String endpointName, String serviceConfigurationName) {
        return deleteWithResponseAsync(resourceUri, endpointName, serviceConfigurationName)
            .flatMap(ignored -> Mono.empty());
    }

    /**
     * Deletes the service details to the target resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @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 Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<Void> deleteWithResponse(
        String resourceUri, String endpointName, String serviceConfigurationName, Context context) {
        return deleteWithResponseAsync(resourceUri, endpointName, serviceConfigurationName, context).block();
    }

    /**
     * Deletes the service details to the target resource.
     *
     * @param resourceUri The fully qualified Azure Resource manager identifier of the resource to be connected.
     * @param endpointName The endpoint name.
     * @param serviceConfigurationName The service name.
     * @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.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public void delete(String resourceUri, String endpointName, String serviceConfigurationName) {
        deleteWithResponse(resourceUri, endpointName, serviceConfigurationName, Context.NONE);
    }

    /**
     * Get the next page of items.
     *
     * @param nextLink The URL to get the next list of items
     *     <p>The nextLink parameter.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the paginated list of serviceConfigurations along with {@link PagedResponse} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<ServiceConfigurationResourceInner>> listByEndpointResourceNextSinglePageAsync(
        String nextLink) {
        if (nextLink == null) {
            return Mono.error(new IllegalArgumentException("Parameter nextLink is required and cannot be null."));
        }
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context -> service.listByEndpointResourceNext(nextLink, this.client.getEndpoint(), accept, context))
            .<PagedResponse<ServiceConfigurationResourceInner>>map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Get the next page of items.
     *
     * @param nextLink The URL to get the next list of items
     *     <p>The nextLink parameter.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the paginated list of serviceConfigurations along with {@link PagedResponse} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<ServiceConfigurationResourceInner>> listByEndpointResourceNextSinglePageAsync(
        String nextLink, Context context) {
        if (nextLink == null) {
            return Mono.error(new IllegalArgumentException("Parameter nextLink is required and cannot be null."));
        }
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .listByEndpointResourceNext(nextLink, this.client.getEndpoint(), accept, context)
            .map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null));
    }
}
