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

package com.azure.resourcemanager.servicefabric.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.Response;
import com.azure.core.http.rest.RestProxy;
import com.azure.core.management.exception.ManagementException;
import com.azure.core.management.polling.PollResult;
import com.azure.core.util.Context;
import com.azure.core.util.FluxUtil;
import com.azure.core.util.polling.PollerFlux;
import com.azure.core.util.polling.SyncPoller;
import com.azure.resourcemanager.servicefabric.fluent.ServicesClient;
import com.azure.resourcemanager.servicefabric.fluent.models.ServiceResourceInner;
import com.azure.resourcemanager.servicefabric.fluent.models.ServiceResourceListInner;
import com.azure.resourcemanager.servicefabric.models.ServiceResourceUpdate;
import java.nio.ByteBuffer;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

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

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

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

    /**
     * The interface defining all the services for ServiceFabricManagementClientServices to be used by the proxy service
     * to perform REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "ServiceFabricManagem")
    public interface ServicesService {
        @Headers({"Content-Type: application/json"})
        @Get(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ServiceFabric"
                + "/clusters/{clusterName}/applications/{applicationName}/services/{serviceName}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<ServiceResourceInner>> get(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("clusterName") String clusterName,
            @PathParam("applicationName") String applicationName,
            @PathParam("serviceName") String serviceName,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Put(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ServiceFabric"
                + "/clusters/{clusterName}/applications/{applicationName}/services/{serviceName}")
        @ExpectedResponses({202})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<Flux<ByteBuffer>>> createOrUpdate(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("clusterName") String clusterName,
            @PathParam("applicationName") String applicationName,
            @PathParam("serviceName") String serviceName,
            @QueryParam("api-version") String apiVersion,
            @BodyParam("application/json") ServiceResourceInner parameters,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Patch(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ServiceFabric"
                + "/clusters/{clusterName}/applications/{applicationName}/services/{serviceName}")
        @ExpectedResponses({202})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<Flux<ByteBuffer>>> update(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("clusterName") String clusterName,
            @PathParam("applicationName") String applicationName,
            @PathParam("serviceName") String serviceName,
            @QueryParam("api-version") String apiVersion,
            @BodyParam("application/json") ServiceResourceUpdate parameters,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Delete(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ServiceFabric"
                + "/clusters/{clusterName}/applications/{applicationName}/services/{serviceName}")
        @ExpectedResponses({202, 204})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<Flux<ByteBuffer>>> delete(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("clusterName") String clusterName,
            @PathParam("applicationName") String applicationName,
            @PathParam("serviceName") String serviceName,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ServiceFabric"
                + "/clusters/{clusterName}/applications/{applicationName}/services")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<ServiceResourceListInner>> list(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("clusterName") String clusterName,
            @PathParam("applicationName") String applicationName,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);
    }

    /**
     * Gets a Service Fabric service resource.
     *
     * <p>Get a Service Fabric service resource created or in the process of being created in the Service Fabric
     * application resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @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 Service Fabric service resource created or in the process of being created in the Service Fabric
     *     application resource along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ServiceResourceInner>> getWithResponseAsync(
        String resourceGroupName, String clusterName, String applicationName, String serviceName) {
        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 (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (clusterName == null) {
            return Mono.error(new IllegalArgumentException("Parameter clusterName is required and cannot be null."));
        }
        if (applicationName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter applicationName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .get(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            clusterName,
                            applicationName,
                            serviceName,
                            this.client.getApiVersion(),
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Gets a Service Fabric service resource.
     *
     * <p>Get a Service Fabric service resource created or in the process of being created in the Service Fabric
     * application resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @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 Service Fabric service resource created or in the process of being created in the Service Fabric
     *     application resource along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ServiceResourceInner>> getWithResponseAsync(
        String resourceGroupName, String clusterName, String applicationName, String serviceName, 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 (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (clusterName == null) {
            return Mono.error(new IllegalArgumentException("Parameter clusterName is required and cannot be null."));
        }
        if (applicationName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter applicationName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .get(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                clusterName,
                applicationName,
                serviceName,
                this.client.getApiVersion(),
                accept,
                context);
    }

    /**
     * Gets a Service Fabric service resource.
     *
     * <p>Get a Service Fabric service resource created or in the process of being created in the Service Fabric
     * application resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @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 Service Fabric service resource created or in the process of being created in the Service Fabric
     *     application resource on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ServiceResourceInner> getAsync(
        String resourceGroupName, String clusterName, String applicationName, String serviceName) {
        return getWithResponseAsync(resourceGroupName, clusterName, applicationName, serviceName)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Gets a Service Fabric service resource.
     *
     * <p>Get a Service Fabric service resource created or in the process of being created in the Service Fabric
     * application resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @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 Service Fabric service resource created or in the process of being created in the Service Fabric
     *     application resource along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<ServiceResourceInner> getWithResponse(
        String resourceGroupName, String clusterName, String applicationName, String serviceName, Context context) {
        return getWithResponseAsync(resourceGroupName, clusterName, applicationName, serviceName, context).block();
    }

    /**
     * Gets a Service Fabric service resource.
     *
     * <p>Get a Service Fabric service resource created or in the process of being created in the Service Fabric
     * application resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @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 Service Fabric service resource created or in the process of being created in the Service Fabric
     *     application resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ServiceResourceInner get(
        String resourceGroupName, String clusterName, String applicationName, String serviceName) {
        return getWithResponse(resourceGroupName, clusterName, applicationName, serviceName, Context.NONE).getValue();
    }

    /**
     * Creates or updates a Service Fabric service resource.
     *
     * <p>Create or update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource.
     * @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 resource along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> createOrUpdateWithResponseAsync(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceInner parameters) {
        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 (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (clusterName == null) {
            return Mono.error(new IllegalArgumentException("Parameter clusterName is required and cannot be null."));
        }
        if (applicationName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter applicationName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName 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
                        .createOrUpdate(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            clusterName,
                            applicationName,
                            serviceName,
                            this.client.getApiVersion(),
                            parameters,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Creates or updates a Service Fabric service resource.
     *
     * <p>Create or update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource.
     * @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 resource along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> createOrUpdateWithResponseAsync(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceInner parameters,
        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 (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (clusterName == null) {
            return Mono.error(new IllegalArgumentException("Parameter clusterName is required and cannot be null."));
        }
        if (applicationName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter applicationName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName 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
            .createOrUpdate(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                clusterName,
                applicationName,
                serviceName,
                this.client.getApiVersion(),
                parameters,
                accept,
                context);
    }

    /**
     * Creates or updates a Service Fabric service resource.
     *
     * <p>Create or update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link PollerFlux} for polling of the service resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<ServiceResourceInner>, ServiceResourceInner> beginCreateOrUpdateAsync(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceInner parameters) {
        Mono<Response<Flux<ByteBuffer>>> mono =
            createOrUpdateWithResponseAsync(resourceGroupName, clusterName, applicationName, serviceName, parameters);
        return this
            .client
            .<ServiceResourceInner, ServiceResourceInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                ServiceResourceInner.class,
                ServiceResourceInner.class,
                this.client.getContext());
    }

    /**
     * Creates or updates a Service Fabric service resource.
     *
     * <p>Create or update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link PollerFlux} for polling of the service resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<ServiceResourceInner>, ServiceResourceInner> beginCreateOrUpdateAsync(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceInner parameters,
        Context context) {
        context = this.client.mergeContext(context);
        Mono<Response<Flux<ByteBuffer>>> mono =
            createOrUpdateWithResponseAsync(
                resourceGroupName, clusterName, applicationName, serviceName, parameters, context);
        return this
            .client
            .<ServiceResourceInner, ServiceResourceInner>getLroResult(
                mono, this.client.getHttpPipeline(), ServiceResourceInner.class, ServiceResourceInner.class, context);
    }

    /**
     * Creates or updates a Service Fabric service resource.
     *
     * <p>Create or update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link SyncPoller} for polling of the service resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<ServiceResourceInner>, ServiceResourceInner> beginCreateOrUpdate(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceInner parameters) {
        return this
            .beginCreateOrUpdateAsync(resourceGroupName, clusterName, applicationName, serviceName, parameters)
            .getSyncPoller();
    }

    /**
     * Creates or updates a Service Fabric service resource.
     *
     * <p>Create or update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link SyncPoller} for polling of the service resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<ServiceResourceInner>, ServiceResourceInner> beginCreateOrUpdate(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceInner parameters,
        Context context) {
        return this
            .beginCreateOrUpdateAsync(resourceGroupName, clusterName, applicationName, serviceName, parameters, context)
            .getSyncPoller();
    }

    /**
     * Creates or updates a Service Fabric service resource.
     *
     * <p>Create or update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource.
     * @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 resource on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ServiceResourceInner> createOrUpdateAsync(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceInner parameters) {
        return beginCreateOrUpdateAsync(resourceGroupName, clusterName, applicationName, serviceName, parameters)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Creates or updates a Service Fabric service resource.
     *
     * <p>Create or update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource.
     * @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 resource on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ServiceResourceInner> createOrUpdateAsync(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceInner parameters,
        Context context) {
        return beginCreateOrUpdateAsync(
                resourceGroupName, clusterName, applicationName, serviceName, parameters, context)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Creates or updates a Service Fabric service resource.
     *
     * <p>Create or update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource.
     * @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 resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ServiceResourceInner createOrUpdate(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceInner parameters) {
        return createOrUpdateAsync(resourceGroupName, clusterName, applicationName, serviceName, parameters).block();
    }

    /**
     * Creates or updates a Service Fabric service resource.
     *
     * <p>Create or update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource.
     * @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 resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ServiceResourceInner createOrUpdate(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceInner parameters,
        Context context) {
        return createOrUpdateAsync(resourceGroupName, clusterName, applicationName, serviceName, parameters, context)
            .block();
    }

    /**
     * Updates a Service Fabric service resource.
     *
     * <p>Update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource for patch operations.
     * @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 resource along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> updateWithResponseAsync(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceUpdate parameters) {
        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 (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (clusterName == null) {
            return Mono.error(new IllegalArgumentException("Parameter clusterName is required and cannot be null."));
        }
        if (applicationName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter applicationName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName 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
                        .update(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            clusterName,
                            applicationName,
                            serviceName,
                            this.client.getApiVersion(),
                            parameters,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Updates a Service Fabric service resource.
     *
     * <p>Update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource for patch operations.
     * @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 resource along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> updateWithResponseAsync(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceUpdate parameters,
        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 (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (clusterName == null) {
            return Mono.error(new IllegalArgumentException("Parameter clusterName is required and cannot be null."));
        }
        if (applicationName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter applicationName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName 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
            .update(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                clusterName,
                applicationName,
                serviceName,
                this.client.getApiVersion(),
                parameters,
                accept,
                context);
    }

    /**
     * Updates a Service Fabric service resource.
     *
     * <p>Update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource for patch operations.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link PollerFlux} for polling of the service resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<ServiceResourceInner>, ServiceResourceInner> beginUpdateAsync(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceUpdate parameters) {
        Mono<Response<Flux<ByteBuffer>>> mono =
            updateWithResponseAsync(resourceGroupName, clusterName, applicationName, serviceName, parameters);
        return this
            .client
            .<ServiceResourceInner, ServiceResourceInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                ServiceResourceInner.class,
                ServiceResourceInner.class,
                this.client.getContext());
    }

    /**
     * Updates a Service Fabric service resource.
     *
     * <p>Update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource for patch operations.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link PollerFlux} for polling of the service resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<ServiceResourceInner>, ServiceResourceInner> beginUpdateAsync(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceUpdate parameters,
        Context context) {
        context = this.client.mergeContext(context);
        Mono<Response<Flux<ByteBuffer>>> mono =
            updateWithResponseAsync(resourceGroupName, clusterName, applicationName, serviceName, parameters, context);
        return this
            .client
            .<ServiceResourceInner, ServiceResourceInner>getLroResult(
                mono, this.client.getHttpPipeline(), ServiceResourceInner.class, ServiceResourceInner.class, context);
    }

    /**
     * Updates a Service Fabric service resource.
     *
     * <p>Update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource for patch operations.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link SyncPoller} for polling of the service resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<ServiceResourceInner>, ServiceResourceInner> beginUpdate(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceUpdate parameters) {
        return this
            .beginUpdateAsync(resourceGroupName, clusterName, applicationName, serviceName, parameters)
            .getSyncPoller();
    }

    /**
     * Updates a Service Fabric service resource.
     *
     * <p>Update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource for patch operations.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link SyncPoller} for polling of the service resource.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<ServiceResourceInner>, ServiceResourceInner> beginUpdate(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceUpdate parameters,
        Context context) {
        return this
            .beginUpdateAsync(resourceGroupName, clusterName, applicationName, serviceName, parameters, context)
            .getSyncPoller();
    }

    /**
     * Updates a Service Fabric service resource.
     *
     * <p>Update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource for patch operations.
     * @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 resource on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ServiceResourceInner> updateAsync(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceUpdate parameters) {
        return beginUpdateAsync(resourceGroupName, clusterName, applicationName, serviceName, parameters)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Updates a Service Fabric service resource.
     *
     * <p>Update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource for patch operations.
     * @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 resource on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ServiceResourceInner> updateAsync(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceUpdate parameters,
        Context context) {
        return beginUpdateAsync(resourceGroupName, clusterName, applicationName, serviceName, parameters, context)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Updates a Service Fabric service resource.
     *
     * <p>Update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource for patch operations.
     * @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 resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ServiceResourceInner update(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceUpdate parameters) {
        return updateAsync(resourceGroupName, clusterName, applicationName, serviceName, parameters).block();
    }

    /**
     * Updates a Service Fabric service resource.
     *
     * <p>Update a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param parameters The service resource for patch operations.
     * @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 resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ServiceResourceInner update(
        String resourceGroupName,
        String clusterName,
        String applicationName,
        String serviceName,
        ServiceResourceUpdate parameters,
        Context context) {
        return updateAsync(resourceGroupName, clusterName, applicationName, serviceName, parameters, context).block();
    }

    /**
     * Deletes a Service Fabric service resource.
     *
     * <p>Delete a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @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<Flux<ByteBuffer>>> deleteWithResponseAsync(
        String resourceGroupName, String clusterName, String applicationName, String serviceName) {
        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 (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (clusterName == null) {
            return Mono.error(new IllegalArgumentException("Parameter clusterName is required and cannot be null."));
        }
        if (applicationName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter applicationName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .delete(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            clusterName,
                            applicationName,
                            serviceName,
                            this.client.getApiVersion(),
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Deletes a Service Fabric service resource.
     *
     * <p>Delete a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @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<Flux<ByteBuffer>>> deleteWithResponseAsync(
        String resourceGroupName, String clusterName, String applicationName, String serviceName, 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 (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (clusterName == null) {
            return Mono.error(new IllegalArgumentException("Parameter clusterName is required and cannot be null."));
        }
        if (applicationName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter applicationName is required and cannot be null."));
        }
        if (serviceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter serviceName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .delete(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                clusterName,
                applicationName,
                serviceName,
                this.client.getApiVersion(),
                accept,
                context);
    }

    /**
     * Deletes a Service Fabric service resource.
     *
     * <p>Delete a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link PollerFlux} for polling of long-running operation.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<Void>, Void> beginDeleteAsync(
        String resourceGroupName, String clusterName, String applicationName, String serviceName) {
        Mono<Response<Flux<ByteBuffer>>> mono =
            deleteWithResponseAsync(resourceGroupName, clusterName, applicationName, serviceName);
        return this
            .client
            .<Void, Void>getLroResult(
                mono, this.client.getHttpPipeline(), Void.class, Void.class, this.client.getContext());
    }

    /**
     * Deletes a Service Fabric service resource.
     *
     * <p>Delete a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link PollerFlux} for polling of long-running operation.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<Void>, Void> beginDeleteAsync(
        String resourceGroupName, String clusterName, String applicationName, String serviceName, Context context) {
        context = this.client.mergeContext(context);
        Mono<Response<Flux<ByteBuffer>>> mono =
            deleteWithResponseAsync(resourceGroupName, clusterName, applicationName, serviceName, context);
        return this
            .client
            .<Void, Void>getLroResult(mono, this.client.getHttpPipeline(), Void.class, Void.class, context);
    }

    /**
     * Deletes a Service Fabric service resource.
     *
     * <p>Delete a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link SyncPoller} for polling of long-running operation.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<Void>, Void> beginDelete(
        String resourceGroupName, String clusterName, String applicationName, String serviceName) {
        return this.beginDeleteAsync(resourceGroupName, clusterName, applicationName, serviceName).getSyncPoller();
    }

    /**
     * Deletes a Service Fabric service resource.
     *
     * <p>Delete a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link SyncPoller} for polling of long-running operation.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<Void>, Void> beginDelete(
        String resourceGroupName, String clusterName, String applicationName, String serviceName, Context context) {
        return this
            .beginDeleteAsync(resourceGroupName, clusterName, applicationName, serviceName, context)
            .getSyncPoller();
    }

    /**
     * Deletes a Service Fabric service resource.
     *
     * <p>Delete a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @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 resourceGroupName, String clusterName, String applicationName, String serviceName) {
        return beginDeleteAsync(resourceGroupName, clusterName, applicationName, serviceName)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Deletes a Service Fabric service resource.
     *
     * <p>Delete a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @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 {@link Mono} that completes when a successful response is received.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Void> deleteAsync(
        String resourceGroupName, String clusterName, String applicationName, String serviceName, Context context) {
        return beginDeleteAsync(resourceGroupName, clusterName, applicationName, serviceName, context)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Deletes a Service Fabric service resource.
     *
     * <p>Delete a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @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 resourceGroupName, String clusterName, String applicationName, String serviceName) {
        deleteAsync(resourceGroupName, clusterName, applicationName, serviceName).block();
    }

    /**
     * Deletes a Service Fabric service resource.
     *
     * <p>Delete a Service Fabric service resource with the specified name.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @param serviceName The name of the service resource in the format of {applicationName}~{serviceName}.
     * @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.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public void delete(
        String resourceGroupName, String clusterName, String applicationName, String serviceName, Context context) {
        deleteAsync(resourceGroupName, clusterName, applicationName, serviceName, context).block();
    }

    /**
     * Gets the list of service resources created in the specified Service Fabric application resource.
     *
     * <p>Gets all service resources created or in the process of being created in the Service Fabric application
     * resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @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 all service resources created or in the process of being created in the Service Fabric application
     *     resource along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ServiceResourceListInner>> listWithResponseAsync(
        String resourceGroupName, String clusterName, String applicationName) {
        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 (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (clusterName == null) {
            return Mono.error(new IllegalArgumentException("Parameter clusterName is required and cannot be null."));
        }
        if (applicationName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter applicationName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .list(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            clusterName,
                            applicationName,
                            this.client.getApiVersion(),
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Gets the list of service resources created in the specified Service Fabric application resource.
     *
     * <p>Gets all service resources created or in the process of being created in the Service Fabric application
     * resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @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 all service resources created or in the process of being created in the Service Fabric application
     *     resource along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ServiceResourceListInner>> listWithResponseAsync(
        String resourceGroupName, String clusterName, String applicationName, 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 (resourceGroupName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null."));
        }
        if (clusterName == null) {
            return Mono.error(new IllegalArgumentException("Parameter clusterName is required and cannot be null."));
        }
        if (applicationName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter applicationName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .list(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                clusterName,
                applicationName,
                this.client.getApiVersion(),
                accept,
                context);
    }

    /**
     * Gets the list of service resources created in the specified Service Fabric application resource.
     *
     * <p>Gets all service resources created or in the process of being created in the Service Fabric application
     * resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @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 all service resources created or in the process of being created in the Service Fabric application
     *     resource on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ServiceResourceListInner> listAsync(
        String resourceGroupName, String clusterName, String applicationName) {
        return listWithResponseAsync(resourceGroupName, clusterName, applicationName)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Gets the list of service resources created in the specified Service Fabric application resource.
     *
     * <p>Gets all service resources created or in the process of being created in the Service Fabric application
     * resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @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 all service resources created or in the process of being created in the Service Fabric application
     *     resource along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<ServiceResourceListInner> listWithResponse(
        String resourceGroupName, String clusterName, String applicationName, Context context) {
        return listWithResponseAsync(resourceGroupName, clusterName, applicationName, context).block();
    }

    /**
     * Gets the list of service resources created in the specified Service Fabric application resource.
     *
     * <p>Gets all service resources created or in the process of being created in the Service Fabric application
     * resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param clusterName The name of the cluster resource.
     * @param applicationName The name of the application resource.
     * @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 all service resources created or in the process of being created in the Service Fabric application
     *     resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ServiceResourceListInner list(String resourceGroupName, String clusterName, String applicationName) {
        return listWithResponse(resourceGroupName, clusterName, applicationName, Context.NONE).getValue();
    }
}
