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

package com.azure.resourcemanager.appcomplianceautomation.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.Post;
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.appcomplianceautomation.fluent.SnapshotOperationsClient;
import com.azure.resourcemanager.appcomplianceautomation.fluent.models.DownloadResponseInner;
import com.azure.resourcemanager.appcomplianceautomation.fluent.models.SnapshotResourceInner;
import com.azure.resourcemanager.appcomplianceautomation.models.SnapshotDownloadRequest;
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 SnapshotOperationsClient. */
public final class SnapshotOperationsClientImpl implements SnapshotOperationsClient {
    /** The proxy service used to perform REST calls. */
    private final SnapshotOperationsService service;

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

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

    /**
     * The interface defining all the services for AppComplianceAutomationToolForMicrosoft365SnapshotOperations to be
     * used by the proxy service to perform REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "AppComplianceAutomat")
    public interface SnapshotOperationsService {
        @Headers({"Content-Type: application/json"})
        @Get("/providers/Microsoft.AppComplianceAutomation/reports/{reportName}/snapshots/{snapshotName}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<SnapshotResourceInner>> get(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("reportName") String reportName,
            @PathParam("snapshotName") String snapshotName,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Post("/providers/Microsoft.AppComplianceAutomation/reports/{reportName}/snapshots/{snapshotName}/download")
        @ExpectedResponses({200, 202})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<Flux<ByteBuffer>>> download(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("reportName") String reportName,
            @PathParam("snapshotName") String snapshotName,
            @BodyParam("application/json") SnapshotDownloadRequest parameters,
            @HeaderParam("Accept") String accept,
            Context context);
    }

    /**
     * Get the AppComplianceAutomation snapshot and its properties.
     *
     * @param reportName Report Name.
     * @param snapshotName Snapshot Name.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the AppComplianceAutomation snapshot and its properties along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<SnapshotResourceInner>> getWithResponseAsync(String reportName, String snapshotName) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (reportName == null) {
            return Mono.error(new IllegalArgumentException("Parameter reportName is required and cannot be null."));
        }
        if (snapshotName == null) {
            return Mono.error(new IllegalArgumentException("Parameter snapshotName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .get(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            reportName,
                            snapshotName,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Get the AppComplianceAutomation snapshot and its properties.
     *
     * @param reportName Report Name.
     * @param snapshotName Snapshot Name.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the AppComplianceAutomation snapshot and its properties along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<SnapshotResourceInner>> getWithResponseAsync(
        String reportName, String snapshotName, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (reportName == null) {
            return Mono.error(new IllegalArgumentException("Parameter reportName is required and cannot be null."));
        }
        if (snapshotName == null) {
            return Mono.error(new IllegalArgumentException("Parameter snapshotName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .get(this.client.getEndpoint(), this.client.getApiVersion(), reportName, snapshotName, accept, context);
    }

    /**
     * Get the AppComplianceAutomation snapshot and its properties.
     *
     * @param reportName Report Name.
     * @param snapshotName Snapshot Name.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the AppComplianceAutomation snapshot and its properties on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<SnapshotResourceInner> getAsync(String reportName, String snapshotName) {
        return getWithResponseAsync(reportName, snapshotName).flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Get the AppComplianceAutomation snapshot and its properties.
     *
     * @param reportName Report Name.
     * @param snapshotName Snapshot Name.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the AppComplianceAutomation snapshot and its properties along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<SnapshotResourceInner> getWithResponse(String reportName, String snapshotName, Context context) {
        return getWithResponseAsync(reportName, snapshotName, context).block();
    }

    /**
     * Get the AppComplianceAutomation snapshot and its properties.
     *
     * @param reportName Report Name.
     * @param snapshotName Snapshot Name.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the AppComplianceAutomation snapshot and its properties.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public SnapshotResourceInner get(String reportName, String snapshotName) {
        return getWithResponse(reportName, snapshotName, Context.NONE).getValue();
    }

    /**
     * Download compliance needs from snapshot, like: Compliance Report, Resource List.
     *
     * @param reportName Report Name.
     * @param snapshotName Snapshot Name.
     * @param parameters Parameters for the query 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 object that includes all the possible response for the download operation along with {@link Response} on
     *     successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> downloadWithResponseAsync(
        String reportName, String snapshotName, SnapshotDownloadRequest parameters) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (reportName == null) {
            return Mono.error(new IllegalArgumentException("Parameter reportName is required and cannot be null."));
        }
        if (snapshotName == null) {
            return Mono.error(new IllegalArgumentException("Parameter snapshotName 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
                        .download(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            reportName,
                            snapshotName,
                            parameters,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Download compliance needs from snapshot, like: Compliance Report, Resource List.
     *
     * @param reportName Report Name.
     * @param snapshotName Snapshot Name.
     * @param parameters Parameters for the query operation.
     * @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 object that includes all the possible response for the download operation along with {@link Response} on
     *     successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> downloadWithResponseAsync(
        String reportName, String snapshotName, SnapshotDownloadRequest parameters, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (reportName == null) {
            return Mono.error(new IllegalArgumentException("Parameter reportName is required and cannot be null."));
        }
        if (snapshotName == null) {
            return Mono.error(new IllegalArgumentException("Parameter snapshotName 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
            .download(
                this.client.getEndpoint(),
                this.client.getApiVersion(),
                reportName,
                snapshotName,
                parameters,
                accept,
                context);
    }

    /**
     * Download compliance needs from snapshot, like: Compliance Report, Resource List.
     *
     * @param reportName Report Name.
     * @param snapshotName Snapshot Name.
     * @param parameters Parameters for the query 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 object that includes all the possible response for the download
     *     operation.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<DownloadResponseInner>, DownloadResponseInner> beginDownloadAsync(
        String reportName, String snapshotName, SnapshotDownloadRequest parameters) {
        Mono<Response<Flux<ByteBuffer>>> mono = downloadWithResponseAsync(reportName, snapshotName, parameters);
        return this
            .client
            .<DownloadResponseInner, DownloadResponseInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                DownloadResponseInner.class,
                DownloadResponseInner.class,
                this.client.getContext());
    }

    /**
     * Download compliance needs from snapshot, like: Compliance Report, Resource List.
     *
     * @param reportName Report Name.
     * @param snapshotName Snapshot Name.
     * @param parameters Parameters for the query operation.
     * @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 object that includes all the possible response for the download
     *     operation.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<DownloadResponseInner>, DownloadResponseInner> beginDownloadAsync(
        String reportName, String snapshotName, SnapshotDownloadRequest parameters, Context context) {
        context = this.client.mergeContext(context);
        Mono<Response<Flux<ByteBuffer>>> mono =
            downloadWithResponseAsync(reportName, snapshotName, parameters, context);
        return this
            .client
            .<DownloadResponseInner, DownloadResponseInner>getLroResult(
                mono, this.client.getHttpPipeline(), DownloadResponseInner.class, DownloadResponseInner.class, context);
    }

    /**
     * Download compliance needs from snapshot, like: Compliance Report, Resource List.
     *
     * @param reportName Report Name.
     * @param snapshotName Snapshot Name.
     * @param parameters Parameters for the query 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 object that includes all the possible response for the download
     *     operation.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<DownloadResponseInner>, DownloadResponseInner> beginDownload(
        String reportName, String snapshotName, SnapshotDownloadRequest parameters) {
        return beginDownloadAsync(reportName, snapshotName, parameters).getSyncPoller();
    }

    /**
     * Download compliance needs from snapshot, like: Compliance Report, Resource List.
     *
     * @param reportName Report Name.
     * @param snapshotName Snapshot Name.
     * @param parameters Parameters for the query operation.
     * @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 object that includes all the possible response for the download
     *     operation.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<DownloadResponseInner>, DownloadResponseInner> beginDownload(
        String reportName, String snapshotName, SnapshotDownloadRequest parameters, Context context) {
        return beginDownloadAsync(reportName, snapshotName, parameters, context).getSyncPoller();
    }

    /**
     * Download compliance needs from snapshot, like: Compliance Report, Resource List.
     *
     * @param reportName Report Name.
     * @param snapshotName Snapshot Name.
     * @param parameters Parameters for the query 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 object that includes all the possible response for the download operation on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<DownloadResponseInner> downloadAsync(
        String reportName, String snapshotName, SnapshotDownloadRequest parameters) {
        return beginDownloadAsync(reportName, snapshotName, parameters)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Download compliance needs from snapshot, like: Compliance Report, Resource List.
     *
     * @param reportName Report Name.
     * @param snapshotName Snapshot Name.
     * @param parameters Parameters for the query operation.
     * @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 object that includes all the possible response for the download operation on successful completion of
     *     {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<DownloadResponseInner> downloadAsync(
        String reportName, String snapshotName, SnapshotDownloadRequest parameters, Context context) {
        return beginDownloadAsync(reportName, snapshotName, parameters, context)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Download compliance needs from snapshot, like: Compliance Report, Resource List.
     *
     * @param reportName Report Name.
     * @param snapshotName Snapshot Name.
     * @param parameters Parameters for the query 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 object that includes all the possible response for the download operation.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public DownloadResponseInner download(String reportName, String snapshotName, SnapshotDownloadRequest parameters) {
        return downloadAsync(reportName, snapshotName, parameters).block();
    }

    /**
     * Download compliance needs from snapshot, like: Compliance Report, Resource List.
     *
     * @param reportName Report Name.
     * @param snapshotName Snapshot Name.
     * @param parameters Parameters for the query operation.
     * @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 object that includes all the possible response for the download operation.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public DownloadResponseInner download(
        String reportName, String snapshotName, SnapshotDownloadRequest parameters, Context context) {
        return downloadAsync(reportName, snapshotName, parameters, context).block();
    }
}
