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

package com.azure.resourcemanager.synapse.implementation;

import com.azure.core.annotation.BodyParam;
import com.azure.core.annotation.ExpectedResponses;
import com.azure.core.annotation.Get;
import com.azure.core.annotation.HeaderParam;
import com.azure.core.annotation.Headers;
import com.azure.core.annotation.Host;
import com.azure.core.annotation.HostParam;
import com.azure.core.annotation.PathParam;
import com.azure.core.annotation.Put;
import com.azure.core.annotation.QueryParam;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceInterface;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.annotation.UnexpectedResponseExceptionType;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.RestProxy;
import com.azure.core.management.exception.ManagementException;
import com.azure.core.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.synapse.fluent.WorkspaceManagedIdentitySqlControlSettingsClient;
import com.azure.resourcemanager.synapse.fluent.models.ManagedIdentitySqlControlSettingsModelInner;
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
 * WorkspaceManagedIdentitySqlControlSettingsClient.
 */
public final class WorkspaceManagedIdentitySqlControlSettingsClientImpl
    implements WorkspaceManagedIdentitySqlControlSettingsClient {
    /** The proxy service used to perform REST calls. */
    private final WorkspaceManagedIdentitySqlControlSettingsService service;

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

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

    /**
     * The interface defining all the services for SynapseManagementClientWorkspaceManagedIdentitySqlControlSettings to
     * be used by the proxy service to perform REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "SynapseManagementCli")
    public interface WorkspaceManagedIdentitySqlControlSettingsService {
        @Headers({"Content-Type: application/json"})
        @Get(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Synapse/workspaces"
                + "/{workspaceName}/managedIdentitySqlControlSettings/default")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<ManagedIdentitySqlControlSettingsModelInner>> get(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("workspaceName") String workspaceName,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Put(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Synapse/workspaces"
                + "/{workspaceName}/managedIdentitySqlControlSettings/default")
        @ExpectedResponses({200, 201})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<Flux<ByteBuffer>>> createOrUpdate(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("workspaceName") String workspaceName,
            @BodyParam("application/json")
                ManagedIdentitySqlControlSettingsModelInner managedIdentitySqlControlSettings,
            @HeaderParam("Accept") String accept,
            Context context);
    }

    /**
     * Get Managed Identity Sql Control Settings.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param workspaceName The name of the workspace.
     * @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 managed Identity Sql Control Settings along with {@link Response} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ManagedIdentitySqlControlSettingsModelInner>> getWithResponseAsync(
        String resourceGroupName, String workspaceName) {
        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 (workspaceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter workspaceName is required and cannot be null."));
        }
        final String apiVersion = "2021-06-01";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .get(
                            this.client.getEndpoint(),
                            apiVersion,
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            workspaceName,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Get Managed Identity Sql Control Settings.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param workspaceName The name of the workspace.
     * @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 managed Identity Sql Control Settings along with {@link Response} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<ManagedIdentitySqlControlSettingsModelInner>> getWithResponseAsync(
        String resourceGroupName, String workspaceName, 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 (workspaceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter workspaceName is required and cannot be null."));
        }
        final String apiVersion = "2021-06-01";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .get(
                this.client.getEndpoint(),
                apiVersion,
                this.client.getSubscriptionId(),
                resourceGroupName,
                workspaceName,
                accept,
                context);
    }

    /**
     * Get Managed Identity Sql Control Settings.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param workspaceName The name of the workspace.
     * @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 managed Identity Sql Control Settings on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ManagedIdentitySqlControlSettingsModelInner> getAsync(String resourceGroupName, String workspaceName) {
        return getWithResponseAsync(resourceGroupName, workspaceName).flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Get Managed Identity Sql Control Settings.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param workspaceName The name of the workspace.
     * @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 managed Identity Sql Control Settings along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<ManagedIdentitySqlControlSettingsModelInner> getWithResponse(
        String resourceGroupName, String workspaceName, Context context) {
        return getWithResponseAsync(resourceGroupName, workspaceName, context).block();
    }

    /**
     * Get Managed Identity Sql Control Settings.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param workspaceName The name of the workspace.
     * @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 managed Identity Sql Control Settings.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ManagedIdentitySqlControlSettingsModelInner get(String resourceGroupName, String workspaceName) {
        return getWithResponse(resourceGroupName, workspaceName, Context.NONE).getValue();
    }

    /**
     * Create or update Managed Identity Sql Control Settings.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param workspaceName The name of the workspace.
     * @param managedIdentitySqlControlSettings Managed Identity Sql Control Settings.
     * @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 managed Identity Sql Control Settings along with {@link Response} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> createOrUpdateWithResponseAsync(
        String resourceGroupName,
        String workspaceName,
        ManagedIdentitySqlControlSettingsModelInner managedIdentitySqlControlSettings) {
        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 (workspaceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter workspaceName is required and cannot be null."));
        }
        if (managedIdentitySqlControlSettings == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter managedIdentitySqlControlSettings is required and cannot be null."));
        } else {
            managedIdentitySqlControlSettings.validate();
        }
        final String apiVersion = "2021-06-01";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .createOrUpdate(
                            this.client.getEndpoint(),
                            apiVersion,
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            workspaceName,
                            managedIdentitySqlControlSettings,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Create or update Managed Identity Sql Control Settings.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param workspaceName The name of the workspace.
     * @param managedIdentitySqlControlSettings Managed Identity Sql Control Settings.
     * @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 managed Identity Sql Control Settings along with {@link Response} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> createOrUpdateWithResponseAsync(
        String resourceGroupName,
        String workspaceName,
        ManagedIdentitySqlControlSettingsModelInner managedIdentitySqlControlSettings,
        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 (workspaceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter workspaceName is required and cannot be null."));
        }
        if (managedIdentitySqlControlSettings == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter managedIdentitySqlControlSettings is required and cannot be null."));
        } else {
            managedIdentitySqlControlSettings.validate();
        }
        final String apiVersion = "2021-06-01";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .createOrUpdate(
                this.client.getEndpoint(),
                apiVersion,
                this.client.getSubscriptionId(),
                resourceGroupName,
                workspaceName,
                managedIdentitySqlControlSettings,
                accept,
                context);
    }

    /**
     * Create or update Managed Identity Sql Control Settings.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param workspaceName The name of the workspace.
     * @param managedIdentitySqlControlSettings Managed Identity Sql Control Settings.
     * @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 managed Identity Sql Control Settings.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<
            PollResult<ManagedIdentitySqlControlSettingsModelInner>, ManagedIdentitySqlControlSettingsModelInner>
        beginCreateOrUpdateAsync(
            String resourceGroupName,
            String workspaceName,
            ManagedIdentitySqlControlSettingsModelInner managedIdentitySqlControlSettings) {
        Mono<Response<Flux<ByteBuffer>>> mono =
            createOrUpdateWithResponseAsync(resourceGroupName, workspaceName, managedIdentitySqlControlSettings);
        return this
            .client
            .<ManagedIdentitySqlControlSettingsModelInner, ManagedIdentitySqlControlSettingsModelInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                ManagedIdentitySqlControlSettingsModelInner.class,
                ManagedIdentitySqlControlSettingsModelInner.class,
                this.client.getContext());
    }

    /**
     * Create or update Managed Identity Sql Control Settings.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param workspaceName The name of the workspace.
     * @param managedIdentitySqlControlSettings Managed Identity Sql Control Settings.
     * @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 managed Identity Sql Control Settings.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<
            PollResult<ManagedIdentitySqlControlSettingsModelInner>, ManagedIdentitySqlControlSettingsModelInner>
        beginCreateOrUpdateAsync(
            String resourceGroupName,
            String workspaceName,
            ManagedIdentitySqlControlSettingsModelInner managedIdentitySqlControlSettings,
            Context context) {
        context = this.client.mergeContext(context);
        Mono<Response<Flux<ByteBuffer>>> mono =
            createOrUpdateWithResponseAsync(
                resourceGroupName, workspaceName, managedIdentitySqlControlSettings, context);
        return this
            .client
            .<ManagedIdentitySqlControlSettingsModelInner, ManagedIdentitySqlControlSettingsModelInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                ManagedIdentitySqlControlSettingsModelInner.class,
                ManagedIdentitySqlControlSettingsModelInner.class,
                context);
    }

    /**
     * Create or update Managed Identity Sql Control Settings.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param workspaceName The name of the workspace.
     * @param managedIdentitySqlControlSettings Managed Identity Sql Control Settings.
     * @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 managed Identity Sql Control Settings.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<
            PollResult<ManagedIdentitySqlControlSettingsModelInner>, ManagedIdentitySqlControlSettingsModelInner>
        beginCreateOrUpdate(
            String resourceGroupName,
            String workspaceName,
            ManagedIdentitySqlControlSettingsModelInner managedIdentitySqlControlSettings) {
        return this
            .beginCreateOrUpdateAsync(resourceGroupName, workspaceName, managedIdentitySqlControlSettings)
            .getSyncPoller();
    }

    /**
     * Create or update Managed Identity Sql Control Settings.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param workspaceName The name of the workspace.
     * @param managedIdentitySqlControlSettings Managed Identity Sql Control Settings.
     * @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 managed Identity Sql Control Settings.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<
            PollResult<ManagedIdentitySqlControlSettingsModelInner>, ManagedIdentitySqlControlSettingsModelInner>
        beginCreateOrUpdate(
            String resourceGroupName,
            String workspaceName,
            ManagedIdentitySqlControlSettingsModelInner managedIdentitySqlControlSettings,
            Context context) {
        return this
            .beginCreateOrUpdateAsync(resourceGroupName, workspaceName, managedIdentitySqlControlSettings, context)
            .getSyncPoller();
    }

    /**
     * Create or update Managed Identity Sql Control Settings.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param workspaceName The name of the workspace.
     * @param managedIdentitySqlControlSettings Managed Identity Sql Control Settings.
     * @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 managed Identity Sql Control Settings on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ManagedIdentitySqlControlSettingsModelInner> createOrUpdateAsync(
        String resourceGroupName,
        String workspaceName,
        ManagedIdentitySqlControlSettingsModelInner managedIdentitySqlControlSettings) {
        return beginCreateOrUpdateAsync(resourceGroupName, workspaceName, managedIdentitySqlControlSettings)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Create or update Managed Identity Sql Control Settings.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param workspaceName The name of the workspace.
     * @param managedIdentitySqlControlSettings Managed Identity Sql Control Settings.
     * @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 managed Identity Sql Control Settings on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<ManagedIdentitySqlControlSettingsModelInner> createOrUpdateAsync(
        String resourceGroupName,
        String workspaceName,
        ManagedIdentitySqlControlSettingsModelInner managedIdentitySqlControlSettings,
        Context context) {
        return beginCreateOrUpdateAsync(resourceGroupName, workspaceName, managedIdentitySqlControlSettings, context)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Create or update Managed Identity Sql Control Settings.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param workspaceName The name of the workspace.
     * @param managedIdentitySqlControlSettings Managed Identity Sql Control Settings.
     * @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 managed Identity Sql Control Settings.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ManagedIdentitySqlControlSettingsModelInner createOrUpdate(
        String resourceGroupName,
        String workspaceName,
        ManagedIdentitySqlControlSettingsModelInner managedIdentitySqlControlSettings) {
        return createOrUpdateAsync(resourceGroupName, workspaceName, managedIdentitySqlControlSettings).block();
    }

    /**
     * Create or update Managed Identity Sql Control Settings.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param workspaceName The name of the workspace.
     * @param managedIdentitySqlControlSettings Managed Identity Sql Control Settings.
     * @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 managed Identity Sql Control Settings.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public ManagedIdentitySqlControlSettingsModelInner createOrUpdate(
        String resourceGroupName,
        String workspaceName,
        ManagedIdentitySqlControlSettingsModelInner managedIdentitySqlControlSettings,
        Context context) {
        return createOrUpdateAsync(resourceGroupName, workspaceName, managedIdentitySqlControlSettings, context)
            .block();
    }
}
