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

package com.azure.resourcemanager.timeseriesinsights.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.util.Context;
import com.azure.core.util.FluxUtil;
import com.azure.resourcemanager.timeseriesinsights.fluent.EventSourcesClient;
import com.azure.resourcemanager.timeseriesinsights.fluent.models.EventSourceListResponseInner;
import com.azure.resourcemanager.timeseriesinsights.fluent.models.EventSourceResourceInner;
import com.azure.resourcemanager.timeseriesinsights.models.EventSourceCreateOrUpdateParameters;
import com.azure.resourcemanager.timeseriesinsights.models.EventSourceUpdateParameters;
import reactor.core.publisher.Mono;

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

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

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

    /**
     * The interface defining all the services for TimeSeriesInsightsClientEventSources to be used by the proxy service
     * to perform REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "TimeSeriesInsightsCl")
    public interface EventSourcesService {
        @Headers({"Content-Type: application/json"})
        @Put(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.TimeSeriesInsights"
                + "/environments/{environmentName}/eventSources/{eventSourceName}")
        @ExpectedResponses({200, 201})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<EventSourceResourceInner>> createOrUpdate(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("environmentName") String environmentName,
            @PathParam("eventSourceName") String eventSourceName,
            @QueryParam("api-version") String apiVersion,
            @BodyParam("application/json") EventSourceCreateOrUpdateParameters parameters,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.TimeSeriesInsights"
                + "/environments/{environmentName}/eventSources/{eventSourceName}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<EventSourceResourceInner>> get(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("environmentName") String environmentName,
            @PathParam("eventSourceName") String eventSourceName,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Patch(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.TimeSeriesInsights"
                + "/environments/{environmentName}/eventSources/{eventSourceName}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<EventSourceResourceInner>> update(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("environmentName") String environmentName,
            @PathParam("eventSourceName") String eventSourceName,
            @QueryParam("api-version") String apiVersion,
            @BodyParam("application/json") EventSourceUpdateParameters eventSourceUpdateParameters,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Delete(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.TimeSeriesInsights"
                + "/environments/{environmentName}/eventSources/{eventSourceName}")
        @ExpectedResponses({200, 204})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<Void>> delete(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("environmentName") String environmentName,
            @PathParam("eventSourceName") String eventSourceName,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.TimeSeriesInsights"
                + "/environments/{environmentName}/eventSources")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<EventSourceListResponseInner>> listByEnvironment(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("environmentName") String environmentName,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);
    }

    /**
     * Create or update an event source under the specified environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName Name of the event source.
     * @param parameters Parameters for creating an event source 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 an environment receives data from one or more event sources along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<EventSourceResourceInner>> createOrUpdateWithResponseAsync(
        String resourceGroupName,
        String environmentName,
        String eventSourceName,
        EventSourceCreateOrUpdateParameters 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 (environmentName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter environmentName is required and cannot be null."));
        }
        if (eventSourceName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter eventSourceName 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,
                            environmentName,
                            eventSourceName,
                            this.client.getApiVersion(),
                            parameters,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Create or update an event source under the specified environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName Name of the event source.
     * @param parameters Parameters for creating an event source 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 an environment receives data from one or more event sources along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<EventSourceResourceInner>> createOrUpdateWithResponseAsync(
        String resourceGroupName,
        String environmentName,
        String eventSourceName,
        EventSourceCreateOrUpdateParameters 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 (environmentName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter environmentName is required and cannot be null."));
        }
        if (eventSourceName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter eventSourceName 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,
                environmentName,
                eventSourceName,
                this.client.getApiVersion(),
                parameters,
                accept,
                context);
    }

    /**
     * Create or update an event source under the specified environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName Name of the event source.
     * @param parameters Parameters for creating an event source 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 an environment receives data from one or more event sources on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<EventSourceResourceInner> createOrUpdateAsync(
        String resourceGroupName,
        String environmentName,
        String eventSourceName,
        EventSourceCreateOrUpdateParameters parameters) {
        return createOrUpdateWithResponseAsync(resourceGroupName, environmentName, eventSourceName, parameters)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Create or update an event source under the specified environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName Name of the event source.
     * @param parameters Parameters for creating an event source 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 an environment receives data from one or more event sources along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<EventSourceResourceInner> createOrUpdateWithResponse(
        String resourceGroupName,
        String environmentName,
        String eventSourceName,
        EventSourceCreateOrUpdateParameters parameters,
        Context context) {
        return createOrUpdateWithResponseAsync(resourceGroupName, environmentName, eventSourceName, parameters, context)
            .block();
    }

    /**
     * Create or update an event source under the specified environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName Name of the event source.
     * @param parameters Parameters for creating an event source 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 an environment receives data from one or more event sources.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public EventSourceResourceInner createOrUpdate(
        String resourceGroupName,
        String environmentName,
        String eventSourceName,
        EventSourceCreateOrUpdateParameters parameters) {
        return createOrUpdateWithResponse(resourceGroupName, environmentName, eventSourceName, parameters, Context.NONE)
            .getValue();
    }

    /**
     * Gets the event source with the specified name in the specified environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName The name of the Time Series Insights event source associated with the specified
     *     environment.
     * @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 event source with the specified name in the specified environment along with {@link Response} on
     *     successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<EventSourceResourceInner>> getWithResponseAsync(
        String resourceGroupName, String environmentName, String eventSourceName) {
        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 (environmentName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter environmentName is required and cannot be null."));
        }
        if (eventSourceName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter eventSourceName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .get(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            environmentName,
                            eventSourceName,
                            this.client.getApiVersion(),
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Gets the event source with the specified name in the specified environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName The name of the Time Series Insights event source associated with the specified
     *     environment.
     * @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 event source with the specified name in the specified environment along with {@link Response} on
     *     successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<EventSourceResourceInner>> getWithResponseAsync(
        String resourceGroupName, String environmentName, String eventSourceName, 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 (environmentName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter environmentName is required and cannot be null."));
        }
        if (eventSourceName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter eventSourceName 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,
                environmentName,
                eventSourceName,
                this.client.getApiVersion(),
                accept,
                context);
    }

    /**
     * Gets the event source with the specified name in the specified environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName The name of the Time Series Insights event source associated with the specified
     *     environment.
     * @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 event source with the specified name in the specified environment on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<EventSourceResourceInner> getAsync(
        String resourceGroupName, String environmentName, String eventSourceName) {
        return getWithResponseAsync(resourceGroupName, environmentName, eventSourceName)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Gets the event source with the specified name in the specified environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName The name of the Time Series Insights event source associated with the specified
     *     environment.
     * @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 event source with the specified name in the specified environment along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<EventSourceResourceInner> getWithResponse(
        String resourceGroupName, String environmentName, String eventSourceName, Context context) {
        return getWithResponseAsync(resourceGroupName, environmentName, eventSourceName, context).block();
    }

    /**
     * Gets the event source with the specified name in the specified environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName The name of the Time Series Insights event source associated with the specified
     *     environment.
     * @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 event source with the specified name in the specified environment.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public EventSourceResourceInner get(String resourceGroupName, String environmentName, String eventSourceName) {
        return getWithResponse(resourceGroupName, environmentName, eventSourceName, Context.NONE).getValue();
    }

    /**
     * Updates the event source with the specified name in the specified subscription, resource group, and environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName The name of the Time Series Insights event source associated with the specified
     *     environment.
     * @param eventSourceUpdateParameters Request object that contains the updated information for the event source.
     * @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 an environment receives data from one or more event sources along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<EventSourceResourceInner>> updateWithResponseAsync(
        String resourceGroupName,
        String environmentName,
        String eventSourceName,
        EventSourceUpdateParameters eventSourceUpdateParameters) {
        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 (environmentName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter environmentName is required and cannot be null."));
        }
        if (eventSourceName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter eventSourceName is required and cannot be null."));
        }
        if (eventSourceUpdateParameters == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter eventSourceUpdateParameters is required and cannot be null."));
        } else {
            eventSourceUpdateParameters.validate();
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .update(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            environmentName,
                            eventSourceName,
                            this.client.getApiVersion(),
                            eventSourceUpdateParameters,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Updates the event source with the specified name in the specified subscription, resource group, and environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName The name of the Time Series Insights event source associated with the specified
     *     environment.
     * @param eventSourceUpdateParameters Request object that contains the updated information for the event source.
     * @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 an environment receives data from one or more event sources along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<EventSourceResourceInner>> updateWithResponseAsync(
        String resourceGroupName,
        String environmentName,
        String eventSourceName,
        EventSourceUpdateParameters eventSourceUpdateParameters,
        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 (environmentName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter environmentName is required and cannot be null."));
        }
        if (eventSourceName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter eventSourceName is required and cannot be null."));
        }
        if (eventSourceUpdateParameters == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter eventSourceUpdateParameters is required and cannot be null."));
        } else {
            eventSourceUpdateParameters.validate();
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .update(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                environmentName,
                eventSourceName,
                this.client.getApiVersion(),
                eventSourceUpdateParameters,
                accept,
                context);
    }

    /**
     * Updates the event source with the specified name in the specified subscription, resource group, and environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName The name of the Time Series Insights event source associated with the specified
     *     environment.
     * @param eventSourceUpdateParameters Request object that contains the updated information for the event source.
     * @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 an environment receives data from one or more event sources on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<EventSourceResourceInner> updateAsync(
        String resourceGroupName,
        String environmentName,
        String eventSourceName,
        EventSourceUpdateParameters eventSourceUpdateParameters) {
        return updateWithResponseAsync(resourceGroupName, environmentName, eventSourceName, eventSourceUpdateParameters)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Updates the event source with the specified name in the specified subscription, resource group, and environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName The name of the Time Series Insights event source associated with the specified
     *     environment.
     * @param eventSourceUpdateParameters Request object that contains the updated information for the event source.
     * @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 an environment receives data from one or more event sources along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<EventSourceResourceInner> updateWithResponse(
        String resourceGroupName,
        String environmentName,
        String eventSourceName,
        EventSourceUpdateParameters eventSourceUpdateParameters,
        Context context) {
        return updateWithResponseAsync(
                resourceGroupName, environmentName, eventSourceName, eventSourceUpdateParameters, context)
            .block();
    }

    /**
     * Updates the event source with the specified name in the specified subscription, resource group, and environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName The name of the Time Series Insights event source associated with the specified
     *     environment.
     * @param eventSourceUpdateParameters Request object that contains the updated information for the event source.
     * @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 an environment receives data from one or more event sources.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public EventSourceResourceInner update(
        String resourceGroupName,
        String environmentName,
        String eventSourceName,
        EventSourceUpdateParameters eventSourceUpdateParameters) {
        return updateWithResponse(
                resourceGroupName, environmentName, eventSourceName, eventSourceUpdateParameters, Context.NONE)
            .getValue();
    }

    /**
     * Deletes the event source with the specified name in the specified subscription, resource group, and environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName The name of the Time Series Insights event source associated with the specified
     *     environment.
     * @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 resourceGroupName, String environmentName, String eventSourceName) {
        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 (environmentName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter environmentName is required and cannot be null."));
        }
        if (eventSourceName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter eventSourceName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .delete(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            environmentName,
                            eventSourceName,
                            this.client.getApiVersion(),
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Deletes the event source with the specified name in the specified subscription, resource group, and environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName The name of the Time Series Insights event source associated with the specified
     *     environment.
     * @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 resourceGroupName, String environmentName, String eventSourceName, 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 (environmentName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter environmentName is required and cannot be null."));
        }
        if (eventSourceName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter eventSourceName 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,
                environmentName,
                eventSourceName,
                this.client.getApiVersion(),
                accept,
                context);
    }

    /**
     * Deletes the event source with the specified name in the specified subscription, resource group, and environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName The name of the Time Series Insights event source associated with the specified
     *     environment.
     * @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 environmentName, String eventSourceName) {
        return deleteWithResponseAsync(resourceGroupName, environmentName, eventSourceName)
            .flatMap(ignored -> Mono.empty());
    }

    /**
     * Deletes the event source with the specified name in the specified subscription, resource group, and environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName The name of the Time Series Insights event source associated with the specified
     *     environment.
     * @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 resourceGroupName, String environmentName, String eventSourceName, Context context) {
        return deleteWithResponseAsync(resourceGroupName, environmentName, eventSourceName, context).block();
    }

    /**
     * Deletes the event source with the specified name in the specified subscription, resource group, and environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @param eventSourceName The name of the Time Series Insights event source associated with the specified
     *     environment.
     * @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 environmentName, String eventSourceName) {
        deleteWithResponse(resourceGroupName, environmentName, eventSourceName, Context.NONE);
    }

    /**
     * Lists all the available event sources associated with the subscription and within the specified resource group
     * and environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @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 response of the List EventSources operation along with {@link Response} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<EventSourceListResponseInner>> listByEnvironmentWithResponseAsync(
        String resourceGroupName, String environmentName) {
        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 (environmentName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter environmentName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .listByEnvironment(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            environmentName,
                            this.client.getApiVersion(),
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Lists all the available event sources associated with the subscription and within the specified resource group
     * and environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @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 response of the List EventSources operation along with {@link Response} on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<EventSourceListResponseInner>> listByEnvironmentWithResponseAsync(
        String resourceGroupName, String environmentName, 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 (environmentName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter environmentName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .listByEnvironment(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                environmentName,
                this.client.getApiVersion(),
                accept,
                context);
    }

    /**
     * Lists all the available event sources associated with the subscription and within the specified resource group
     * and environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @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 response of the List EventSources operation on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<EventSourceListResponseInner> listByEnvironmentAsync(
        String resourceGroupName, String environmentName) {
        return listByEnvironmentWithResponseAsync(resourceGroupName, environmentName)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Lists all the available event sources associated with the subscription and within the specified resource group
     * and environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @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 response of the List EventSources operation along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<EventSourceListResponseInner> listByEnvironmentWithResponse(
        String resourceGroupName, String environmentName, Context context) {
        return listByEnvironmentWithResponseAsync(resourceGroupName, environmentName, context).block();
    }

    /**
     * Lists all the available event sources associated with the subscription and within the specified resource group
     * and environment.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param environmentName The name of the Time Series Insights environment associated with the specified resource
     *     group.
     * @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 response of the List EventSources operation.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public EventSourceListResponseInner listByEnvironment(String resourceGroupName, String environmentName) {
        return listByEnvironmentWithResponse(resourceGroupName, environmentName, Context.NONE).getValue();
    }
}
