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

package com.azure.resourcemanager.security.implementation;

import com.azure.core.annotation.BodyParam;
import com.azure.core.annotation.ExpectedResponses;
import com.azure.core.annotation.Get;
import com.azure.core.annotation.HeaderParam;
import com.azure.core.annotation.Headers;
import com.azure.core.annotation.Host;
import com.azure.core.annotation.HostParam;
import com.azure.core.annotation.PathParam;
import com.azure.core.annotation.Put;
import com.azure.core.annotation.QueryParam;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceInterface;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.annotation.UnexpectedResponseExceptionType;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.RestProxy;
import com.azure.core.management.exception.ManagementException;
import com.azure.core.util.Context;
import com.azure.core.util.FluxUtil;
import com.azure.resourcemanager.security.fluent.AdvancedThreatProtectionsClient;
import com.azure.resourcemanager.security.fluent.models.AdvancedThreatProtectionSettingInner;
import reactor.core.publisher.Mono;

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

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

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

    /**
     * The interface defining all the services for SecurityCenterAdvancedThreatProtections to be used by the proxy
     * service to perform REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "SecurityCenterAdvanc")
    public interface AdvancedThreatProtectionsService {
        @Headers({"Content-Type: application/json"})
        @Get("/{resourceId}/providers/Microsoft.Security/advancedThreatProtectionSettings/{settingName}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<AdvancedThreatProtectionSettingInner>> get(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam(value = "resourceId", encoded = true) String resourceId,
            @PathParam("settingName") String settingName,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Put("/{resourceId}/providers/Microsoft.Security/advancedThreatProtectionSettings/{settingName}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<AdvancedThreatProtectionSettingInner>> create(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam(value = "resourceId", encoded = true) String resourceId,
            @PathParam("settingName") String settingName,
            @BodyParam("application/json") AdvancedThreatProtectionSettingInner advancedThreatProtectionSetting,
            @HeaderParam("Accept") String accept,
            Context context);
    }

    /**
     * Gets the Advanced Threat Protection settings for the specified resource.
     *
     * @param resourceId The identifier of the 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 Advanced Threat Protection settings for the specified resource along with {@link Response} on
     *     successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AdvancedThreatProtectionSettingInner>> getWithResponseAsync(String resourceId) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (resourceId == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceId is required and cannot be null."));
        }
        final String apiVersion = "2019-01-01";
        final String settingName = "current";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context -> service.get(this.client.getEndpoint(), apiVersion, resourceId, settingName, accept, context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Gets the Advanced Threat Protection settings for the specified resource.
     *
     * @param resourceId The identifier of the 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 Advanced Threat Protection settings for the specified resource along with {@link Response} on
     *     successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AdvancedThreatProtectionSettingInner>> getWithResponseAsync(
        String resourceId, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (resourceId == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceId is required and cannot be null."));
        }
        final String apiVersion = "2019-01-01";
        final String settingName = "current";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service.get(this.client.getEndpoint(), apiVersion, resourceId, settingName, accept, context);
    }

    /**
     * Gets the Advanced Threat Protection settings for the specified resource.
     *
     * @param resourceId The identifier of the 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 Advanced Threat Protection settings for the specified resource on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<AdvancedThreatProtectionSettingInner> getAsync(String resourceId) {
        return getWithResponseAsync(resourceId).flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Gets the Advanced Threat Protection settings for the specified resource.
     *
     * @param resourceId The identifier of the 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 Advanced Threat Protection settings for the specified resource along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<AdvancedThreatProtectionSettingInner> getWithResponse(String resourceId, Context context) {
        return getWithResponseAsync(resourceId, context).block();
    }

    /**
     * Gets the Advanced Threat Protection settings for the specified resource.
     *
     * @param resourceId The identifier of the 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 Advanced Threat Protection settings for the specified resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public AdvancedThreatProtectionSettingInner get(String resourceId) {
        return getWithResponse(resourceId, Context.NONE).getValue();
    }

    /**
     * Creates or updates the Advanced Threat Protection settings on a specified resource.
     *
     * @param resourceId The identifier of the resource.
     * @param advancedThreatProtectionSetting Advanced Threat Protection 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 Advanced Threat Protection resource along with {@link Response} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AdvancedThreatProtectionSettingInner>> createWithResponseAsync(
        String resourceId, AdvancedThreatProtectionSettingInner advancedThreatProtectionSetting) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (resourceId == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceId is required and cannot be null."));
        }
        if (advancedThreatProtectionSetting == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter advancedThreatProtectionSetting is required and cannot be null."));
        } else {
            advancedThreatProtectionSetting.validate();
        }
        final String apiVersion = "2019-01-01";
        final String settingName = "current";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .create(
                            this.client.getEndpoint(),
                            apiVersion,
                            resourceId,
                            settingName,
                            advancedThreatProtectionSetting,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Creates or updates the Advanced Threat Protection settings on a specified resource.
     *
     * @param resourceId The identifier of the resource.
     * @param advancedThreatProtectionSetting Advanced Threat Protection 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 Advanced Threat Protection resource along with {@link Response} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AdvancedThreatProtectionSettingInner>> createWithResponseAsync(
        String resourceId, AdvancedThreatProtectionSettingInner advancedThreatProtectionSetting, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (resourceId == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceId is required and cannot be null."));
        }
        if (advancedThreatProtectionSetting == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter advancedThreatProtectionSetting is required and cannot be null."));
        } else {
            advancedThreatProtectionSetting.validate();
        }
        final String apiVersion = "2019-01-01";
        final String settingName = "current";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .create(
                this.client.getEndpoint(),
                apiVersion,
                resourceId,
                settingName,
                advancedThreatProtectionSetting,
                accept,
                context);
    }

    /**
     * Creates or updates the Advanced Threat Protection settings on a specified resource.
     *
     * @param resourceId The identifier of the resource.
     * @param advancedThreatProtectionSetting Advanced Threat Protection 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 Advanced Threat Protection resource on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<AdvancedThreatProtectionSettingInner> createAsync(
        String resourceId, AdvancedThreatProtectionSettingInner advancedThreatProtectionSetting) {
        return createWithResponseAsync(resourceId, advancedThreatProtectionSetting)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Creates or updates the Advanced Threat Protection settings on a specified resource.
     *
     * @param resourceId The identifier of the resource.
     * @param advancedThreatProtectionSetting Advanced Threat Protection 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 Advanced Threat Protection resource along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<AdvancedThreatProtectionSettingInner> createWithResponse(
        String resourceId, AdvancedThreatProtectionSettingInner advancedThreatProtectionSetting, Context context) {
        return createWithResponseAsync(resourceId, advancedThreatProtectionSetting, context).block();
    }

    /**
     * Creates or updates the Advanced Threat Protection settings on a specified resource.
     *
     * @param resourceId The identifier of the resource.
     * @param advancedThreatProtectionSetting Advanced Threat Protection 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 Advanced Threat Protection resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public AdvancedThreatProtectionSettingInner create(
        String resourceId, AdvancedThreatProtectionSettingInner advancedThreatProtectionSetting) {
        return createWithResponse(resourceId, advancedThreatProtectionSetting, Context.NONE).getValue();
    }
}
