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

package com.azure.resourcemanager.alertsmanagement.implementation;

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.PagedFlux;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.PagedResponse;
import com.azure.core.http.rest.PagedResponseBase;
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.alertsmanagement.fluent.SmartGroupsClient;
import com.azure.resourcemanager.alertsmanagement.fluent.models.SmartGroupInner;
import com.azure.resourcemanager.alertsmanagement.fluent.models.SmartGroupModificationInner;
import com.azure.resourcemanager.alertsmanagement.models.AlertState;
import com.azure.resourcemanager.alertsmanagement.models.MonitorCondition;
import com.azure.resourcemanager.alertsmanagement.models.MonitorService;
import com.azure.resourcemanager.alertsmanagement.models.Severity;
import com.azure.resourcemanager.alertsmanagement.models.SmartGroupsChangeStateResponse;
import com.azure.resourcemanager.alertsmanagement.models.SmartGroupsGetByIdResponse;
import com.azure.resourcemanager.alertsmanagement.models.SmartGroupsList;
import com.azure.resourcemanager.alertsmanagement.models.SmartGroupsSortByFields;
import com.azure.resourcemanager.alertsmanagement.models.SortOrder;
import com.azure.resourcemanager.alertsmanagement.models.TimeRange;
import reactor.core.publisher.Mono;

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

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

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

    /**
     * The interface defining all the services for AlertsManagementClientSmartGroups to be used by the proxy service to
     * perform REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "AlertsManagementClie")
    private interface SmartGroupsService {
        @Headers({"Content-Type: application/json"})
        @Get("/subscriptions/{subscriptionId}/providers/Microsoft.AlertsManagement/smartGroups")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<SmartGroupsList>> list(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @QueryParam("targetResource") String targetResource,
            @QueryParam("targetResourceGroup") String targetResourceGroup,
            @QueryParam("targetResourceType") String targetResourceType,
            @QueryParam("monitorService") MonitorService monitorService,
            @QueryParam("monitorCondition") MonitorCondition monitorCondition,
            @QueryParam("severity") Severity severity,
            @QueryParam("smartGroupState") AlertState smartGroupState,
            @QueryParam("timeRange") TimeRange timeRange,
            @QueryParam("pageCount") Long pageCount,
            @QueryParam("sortBy") SmartGroupsSortByFields sortBy,
            @QueryParam("sortOrder") SortOrder sortOrder,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get("/subscriptions/{subscriptionId}/providers/Microsoft.AlertsManagement/smartGroups/{smartGroupId}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<SmartGroupsGetByIdResponse> getById(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("smartGroupId") String smartGroupId,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Post(
            "/subscriptions/{subscriptionId}/providers/Microsoft.AlertsManagement/smartGroups/{smartGroupId}"
                + "/changeState")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<SmartGroupsChangeStateResponse> changeState(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("smartGroupId") String smartGroupId,
            @QueryParam("api-version") String apiVersion,
            @QueryParam("newState") AlertState newState,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get("/subscriptions/{subscriptionId}/providers/Microsoft.AlertsManagement/smartGroups/{smartGroupId}/history")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<SmartGroupModificationInner>> getHistory(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("smartGroupId") String smartGroupId,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get("{nextLink}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<SmartGroupsList>> getAllNext(
            @PathParam(value = "nextLink", encoded = true) String nextLink,
            @HostParam("$host") String endpoint,
            @HeaderParam("Accept") String accept,
            Context context);
    }

    /**
     * Get all Smart Groups within a specified subscription
     *
     * <p>List all the Smart Groups within a specified subscription.
     *
     * @param targetResource Filter by target resource( which is full ARM ID) Default value is select all.
     * @param targetResourceGroup Filter by target resource group name. Default value is select all.
     * @param targetResourceType Filter by target resource type. Default value is select all.
     * @param monitorService Filter by monitor service which generates the alert instance. Default value is select all.
     * @param monitorCondition Filter by monitor condition which is either 'Fired' or 'Resolved'. Default value is to
     *     select all.
     * @param severity Filter by severity. Default value is select all.
     * @param smartGroupState Filter by state of the smart group. Default value is to select all.
     * @param timeRange Filter by time range by below listed values. Default value is 1 day.
     * @param pageCount Determines number of alerts returned per page in response. Permissible value is between 1 to
     *     250. When the "includeContent" filter is selected, maximum value allowed is 25. Default value is 25.
     * @param sortBy Sort the query results by input field. Default value is sort by 'lastModifiedDateTime'.
     * @param sortOrder Sort the query results order in either ascending or descending. Default value is 'desc' for time
     *     fields and 'asc' for others.
     * @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 list the alerts along with {@link PagedResponse} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<SmartGroupInner>> listSinglePageAsync(
        String targetResource,
        String targetResourceGroup,
        String targetResourceType,
        MonitorService monitorService,
        MonitorCondition monitorCondition,
        Severity severity,
        AlertState smartGroupState,
        TimeRange timeRange,
        Long pageCount,
        SmartGroupsSortByFields sortBy,
        SortOrder sortOrder) {
        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."));
        }
        final String apiVersion = "2019-05-05-preview";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .list(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            targetResource,
                            targetResourceGroup,
                            targetResourceType,
                            monitorService,
                            monitorCondition,
                            severity,
                            smartGroupState,
                            timeRange,
                            pageCount,
                            sortBy,
                            sortOrder,
                            apiVersion,
                            accept,
                            context))
            .<PagedResponse<SmartGroupInner>>map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Get all Smart Groups within a specified subscription
     *
     * <p>List all the Smart Groups within a specified subscription.
     *
     * @param targetResource Filter by target resource( which is full ARM ID) Default value is select all.
     * @param targetResourceGroup Filter by target resource group name. Default value is select all.
     * @param targetResourceType Filter by target resource type. Default value is select all.
     * @param monitorService Filter by monitor service which generates the alert instance. Default value is select all.
     * @param monitorCondition Filter by monitor condition which is either 'Fired' or 'Resolved'. Default value is to
     *     select all.
     * @param severity Filter by severity. Default value is select all.
     * @param smartGroupState Filter by state of the smart group. Default value is to select all.
     * @param timeRange Filter by time range by below listed values. Default value is 1 day.
     * @param pageCount Determines number of alerts returned per page in response. Permissible value is between 1 to
     *     250. When the "includeContent" filter is selected, maximum value allowed is 25. Default value is 25.
     * @param sortBy Sort the query results by input field. Default value is sort by 'lastModifiedDateTime'.
     * @param sortOrder Sort the query results order in either ascending or descending. Default value is 'desc' for time
     *     fields and 'asc' for others.
     * @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 list the alerts along with {@link PagedResponse} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<SmartGroupInner>> listSinglePageAsync(
        String targetResource,
        String targetResourceGroup,
        String targetResourceType,
        MonitorService monitorService,
        MonitorCondition monitorCondition,
        Severity severity,
        AlertState smartGroupState,
        TimeRange timeRange,
        Long pageCount,
        SmartGroupsSortByFields sortBy,
        SortOrder sortOrder,
        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."));
        }
        final String apiVersion = "2019-05-05-preview";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .list(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                targetResource,
                targetResourceGroup,
                targetResourceType,
                monitorService,
                monitorCondition,
                severity,
                smartGroupState,
                timeRange,
                pageCount,
                sortBy,
                sortOrder,
                apiVersion,
                accept,
                context)
            .map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null));
    }

    /**
     * Get all Smart Groups within a specified subscription
     *
     * <p>List all the Smart Groups within a specified subscription.
     *
     * @param targetResource Filter by target resource( which is full ARM ID) Default value is select all.
     * @param targetResourceGroup Filter by target resource group name. Default value is select all.
     * @param targetResourceType Filter by target resource type. Default value is select all.
     * @param monitorService Filter by monitor service which generates the alert instance. Default value is select all.
     * @param monitorCondition Filter by monitor condition which is either 'Fired' or 'Resolved'. Default value is to
     *     select all.
     * @param severity Filter by severity. Default value is select all.
     * @param smartGroupState Filter by state of the smart group. Default value is to select all.
     * @param timeRange Filter by time range by below listed values. Default value is 1 day.
     * @param pageCount Determines number of alerts returned per page in response. Permissible value is between 1 to
     *     250. When the "includeContent" filter is selected, maximum value allowed is 25. Default value is 25.
     * @param sortBy Sort the query results by input field. Default value is sort by 'lastModifiedDateTime'.
     * @param sortOrder Sort the query results order in either ascending or descending. Default value is 'desc' for time
     *     fields and 'asc' for others.
     * @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 list the alerts as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<SmartGroupInner> listAsync(
        String targetResource,
        String targetResourceGroup,
        String targetResourceType,
        MonitorService monitorService,
        MonitorCondition monitorCondition,
        Severity severity,
        AlertState smartGroupState,
        TimeRange timeRange,
        Long pageCount,
        SmartGroupsSortByFields sortBy,
        SortOrder sortOrder) {
        return new PagedFlux<>(
            () ->
                listSinglePageAsync(
                    targetResource,
                    targetResourceGroup,
                    targetResourceType,
                    monitorService,
                    monitorCondition,
                    severity,
                    smartGroupState,
                    timeRange,
                    pageCount,
                    sortBy,
                    sortOrder),
            nextLink -> getAllNextSinglePageAsync(nextLink));
    }

    /**
     * Get all Smart Groups within a specified subscription
     *
     * <p>List all the Smart Groups within a specified subscription.
     *
     * @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 list the alerts as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<SmartGroupInner> listAsync() {
        final String targetResource = null;
        final String targetResourceGroup = null;
        final String targetResourceType = null;
        final MonitorService monitorService = null;
        final MonitorCondition monitorCondition = null;
        final Severity severity = null;
        final AlertState smartGroupState = null;
        final TimeRange timeRange = null;
        final Long pageCount = null;
        final SmartGroupsSortByFields sortBy = null;
        final SortOrder sortOrder = null;
        return new PagedFlux<>(
            () ->
                listSinglePageAsync(
                    targetResource,
                    targetResourceGroup,
                    targetResourceType,
                    monitorService,
                    monitorCondition,
                    severity,
                    smartGroupState,
                    timeRange,
                    pageCount,
                    sortBy,
                    sortOrder),
            nextLink -> getAllNextSinglePageAsync(nextLink));
    }

    /**
     * Get all Smart Groups within a specified subscription
     *
     * <p>List all the Smart Groups within a specified subscription.
     *
     * @param targetResource Filter by target resource( which is full ARM ID) Default value is select all.
     * @param targetResourceGroup Filter by target resource group name. Default value is select all.
     * @param targetResourceType Filter by target resource type. Default value is select all.
     * @param monitorService Filter by monitor service which generates the alert instance. Default value is select all.
     * @param monitorCondition Filter by monitor condition which is either 'Fired' or 'Resolved'. Default value is to
     *     select all.
     * @param severity Filter by severity. Default value is select all.
     * @param smartGroupState Filter by state of the smart group. Default value is to select all.
     * @param timeRange Filter by time range by below listed values. Default value is 1 day.
     * @param pageCount Determines number of alerts returned per page in response. Permissible value is between 1 to
     *     250. When the "includeContent" filter is selected, maximum value allowed is 25. Default value is 25.
     * @param sortBy Sort the query results by input field. Default value is sort by 'lastModifiedDateTime'.
     * @param sortOrder Sort the query results order in either ascending or descending. Default value is 'desc' for time
     *     fields and 'asc' for others.
     * @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 list the alerts as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<SmartGroupInner> listAsync(
        String targetResource,
        String targetResourceGroup,
        String targetResourceType,
        MonitorService monitorService,
        MonitorCondition monitorCondition,
        Severity severity,
        AlertState smartGroupState,
        TimeRange timeRange,
        Long pageCount,
        SmartGroupsSortByFields sortBy,
        SortOrder sortOrder,
        Context context) {
        return new PagedFlux<>(
            () ->
                listSinglePageAsync(
                    targetResource,
                    targetResourceGroup,
                    targetResourceType,
                    monitorService,
                    monitorCondition,
                    severity,
                    smartGroupState,
                    timeRange,
                    pageCount,
                    sortBy,
                    sortOrder,
                    context),
            nextLink -> getAllNextSinglePageAsync(nextLink, context));
    }

    /**
     * Get all Smart Groups within a specified subscription
     *
     * <p>List all the Smart Groups within a specified subscription.
     *
     * @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 list the alerts as paginated response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<SmartGroupInner> list() {
        final String targetResource = null;
        final String targetResourceGroup = null;
        final String targetResourceType = null;
        final MonitorService monitorService = null;
        final MonitorCondition monitorCondition = null;
        final Severity severity = null;
        final AlertState smartGroupState = null;
        final TimeRange timeRange = null;
        final Long pageCount = null;
        final SmartGroupsSortByFields sortBy = null;
        final SortOrder sortOrder = null;
        return new PagedIterable<>(
            listAsync(
                targetResource,
                targetResourceGroup,
                targetResourceType,
                monitorService,
                monitorCondition,
                severity,
                smartGroupState,
                timeRange,
                pageCount,
                sortBy,
                sortOrder));
    }

    /**
     * Get all Smart Groups within a specified subscription
     *
     * <p>List all the Smart Groups within a specified subscription.
     *
     * @param targetResource Filter by target resource( which is full ARM ID) Default value is select all.
     * @param targetResourceGroup Filter by target resource group name. Default value is select all.
     * @param targetResourceType Filter by target resource type. Default value is select all.
     * @param monitorService Filter by monitor service which generates the alert instance. Default value is select all.
     * @param monitorCondition Filter by monitor condition which is either 'Fired' or 'Resolved'. Default value is to
     *     select all.
     * @param severity Filter by severity. Default value is select all.
     * @param smartGroupState Filter by state of the smart group. Default value is to select all.
     * @param timeRange Filter by time range by below listed values. Default value is 1 day.
     * @param pageCount Determines number of alerts returned per page in response. Permissible value is between 1 to
     *     250. When the "includeContent" filter is selected, maximum value allowed is 25. Default value is 25.
     * @param sortBy Sort the query results by input field. Default value is sort by 'lastModifiedDateTime'.
     * @param sortOrder Sort the query results order in either ascending or descending. Default value is 'desc' for time
     *     fields and 'asc' for others.
     * @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 list the alerts as paginated response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<SmartGroupInner> list(
        String targetResource,
        String targetResourceGroup,
        String targetResourceType,
        MonitorService monitorService,
        MonitorCondition monitorCondition,
        Severity severity,
        AlertState smartGroupState,
        TimeRange timeRange,
        Long pageCount,
        SmartGroupsSortByFields sortBy,
        SortOrder sortOrder,
        Context context) {
        return new PagedIterable<>(
            listAsync(
                targetResource,
                targetResourceGroup,
                targetResourceType,
                monitorService,
                monitorCondition,
                severity,
                smartGroupState,
                timeRange,
                pageCount,
                sortBy,
                sortOrder,
                context));
    }

    /**
     * Get information related to a specific Smart Group.
     *
     * @param smartGroupId Smart group unique id.
     * @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 information related to a specific Smart Group on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<SmartGroupsGetByIdResponse> getByIdWithResponseAsync(String smartGroupId) {
        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 (smartGroupId == null) {
            return Mono.error(new IllegalArgumentException("Parameter smartGroupId is required and cannot be null."));
        }
        final String apiVersion = "2019-05-05-preview";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .getById(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            smartGroupId,
                            apiVersion,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Get information related to a specific Smart Group.
     *
     * @param smartGroupId Smart group unique id.
     * @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 information related to a specific Smart Group on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<SmartGroupsGetByIdResponse> getByIdWithResponseAsync(String smartGroupId, 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 (smartGroupId == null) {
            return Mono.error(new IllegalArgumentException("Parameter smartGroupId is required and cannot be null."));
        }
        final String apiVersion = "2019-05-05-preview";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .getById(
                this.client.getEndpoint(), this.client.getSubscriptionId(), smartGroupId, apiVersion, accept, context);
    }

    /**
     * Get information related to a specific Smart Group.
     *
     * @param smartGroupId Smart group unique id.
     * @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 information related to a specific Smart Group on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<SmartGroupInner> getByIdAsync(String smartGroupId) {
        return getByIdWithResponseAsync(smartGroupId).flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Get information related to a specific Smart Group.
     *
     * @param smartGroupId Smart group unique id.
     * @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 information related to a specific Smart Group.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public SmartGroupInner getById(String smartGroupId) {
        return getByIdAsync(smartGroupId).block();
    }

    /**
     * Get information related to a specific Smart Group.
     *
     * @param smartGroupId Smart group unique id.
     * @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 information related to a specific Smart Group.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public SmartGroupsGetByIdResponse getByIdWithResponse(String smartGroupId, Context context) {
        return getByIdWithResponseAsync(smartGroupId, context).block();
    }

    /**
     * Change the state of a Smart Group.
     *
     * @param smartGroupId Smart group unique id.
     * @param newState New state of the alert.
     * @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 set of related alerts grouped together smartly by AMS on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<SmartGroupsChangeStateResponse> changeStateWithResponseAsync(
        String smartGroupId, AlertState newState) {
        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 (smartGroupId == null) {
            return Mono.error(new IllegalArgumentException("Parameter smartGroupId is required and cannot be null."));
        }
        if (newState == null) {
            return Mono.error(new IllegalArgumentException("Parameter newState is required and cannot be null."));
        }
        final String apiVersion = "2019-05-05-preview";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .changeState(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            smartGroupId,
                            apiVersion,
                            newState,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Change the state of a Smart Group.
     *
     * @param smartGroupId Smart group unique id.
     * @param newState New state of the alert.
     * @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 set of related alerts grouped together smartly by AMS on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<SmartGroupsChangeStateResponse> changeStateWithResponseAsync(
        String smartGroupId, AlertState newState, 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 (smartGroupId == null) {
            return Mono.error(new IllegalArgumentException("Parameter smartGroupId is required and cannot be null."));
        }
        if (newState == null) {
            return Mono.error(new IllegalArgumentException("Parameter newState is required and cannot be null."));
        }
        final String apiVersion = "2019-05-05-preview";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .changeState(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                smartGroupId,
                apiVersion,
                newState,
                accept,
                context);
    }

    /**
     * Change the state of a Smart Group.
     *
     * @param smartGroupId Smart group unique id.
     * @param newState New state of the alert.
     * @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 set of related alerts grouped together smartly by AMS on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<SmartGroupInner> changeStateAsync(String smartGroupId, AlertState newState) {
        return changeStateWithResponseAsync(smartGroupId, newState).flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Change the state of a Smart Group.
     *
     * @param smartGroupId Smart group unique id.
     * @param newState New state of the alert.
     * @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 set of related alerts grouped together smartly by AMS.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public SmartGroupInner changeState(String smartGroupId, AlertState newState) {
        return changeStateAsync(smartGroupId, newState).block();
    }

    /**
     * Change the state of a Smart Group.
     *
     * @param smartGroupId Smart group unique id.
     * @param newState New state of the alert.
     * @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 set of related alerts grouped together smartly by AMS.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public SmartGroupsChangeStateResponse changeStateWithResponse(
        String smartGroupId, AlertState newState, Context context) {
        return changeStateWithResponseAsync(smartGroupId, newState, context).block();
    }

    /**
     * Get the history a smart group, which captures any Smart Group state changes (New/Acknowledged/Closed) .
     *
     * @param smartGroupId Smart group unique id.
     * @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 history a smart group, which captures any Smart Group state changes (New/Acknowledged/Closed) along
     *     with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<SmartGroupModificationInner>> getHistoryWithResponseAsync(String smartGroupId) {
        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 (smartGroupId == null) {
            return Mono.error(new IllegalArgumentException("Parameter smartGroupId is required and cannot be null."));
        }
        final String apiVersion = "2019-05-05-preview";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .getHistory(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            smartGroupId,
                            apiVersion,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Get the history a smart group, which captures any Smart Group state changes (New/Acknowledged/Closed) .
     *
     * @param smartGroupId Smart group unique id.
     * @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 history a smart group, which captures any Smart Group state changes (New/Acknowledged/Closed) along
     *     with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<SmartGroupModificationInner>> getHistoryWithResponseAsync(
        String smartGroupId, 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 (smartGroupId == null) {
            return Mono.error(new IllegalArgumentException("Parameter smartGroupId is required and cannot be null."));
        }
        final String apiVersion = "2019-05-05-preview";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .getHistory(
                this.client.getEndpoint(), this.client.getSubscriptionId(), smartGroupId, apiVersion, accept, context);
    }

    /**
     * Get the history a smart group, which captures any Smart Group state changes (New/Acknowledged/Closed) .
     *
     * @param smartGroupId Smart group unique id.
     * @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 history a smart group, which captures any Smart Group state changes (New/Acknowledged/Closed) on
     *     successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<SmartGroupModificationInner> getHistoryAsync(String smartGroupId) {
        return getHistoryWithResponseAsync(smartGroupId).flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Get the history a smart group, which captures any Smart Group state changes (New/Acknowledged/Closed) .
     *
     * @param smartGroupId Smart group unique id.
     * @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 history a smart group, which captures any Smart Group state changes (New/Acknowledged/Closed).
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public SmartGroupModificationInner getHistory(String smartGroupId) {
        return getHistoryAsync(smartGroupId).block();
    }

    /**
     * Get the history a smart group, which captures any Smart Group state changes (New/Acknowledged/Closed) .
     *
     * @param smartGroupId Smart group unique id.
     * @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 history a smart group, which captures any Smart Group state changes (New/Acknowledged/Closed) along
     *     with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<SmartGroupModificationInner> getHistoryWithResponse(String smartGroupId, Context context) {
        return getHistoryWithResponseAsync(smartGroupId, context).block();
    }

    /**
     * Get the next page of items.
     *
     * @param nextLink The URL to get the next list of items
     *     <p>The nextLink parameter.
     * @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 list the alerts along with {@link PagedResponse} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<SmartGroupInner>> getAllNextSinglePageAsync(String nextLink) {
        if (nextLink == null) {
            return Mono.error(new IllegalArgumentException("Parameter nextLink is required and cannot be null."));
        }
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(context -> service.getAllNext(nextLink, this.client.getEndpoint(), accept, context))
            .<PagedResponse<SmartGroupInner>>map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Get the next page of items.
     *
     * @param nextLink The URL to get the next list of items
     *     <p>The nextLink parameter.
     * @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 list the alerts along with {@link PagedResponse} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<SmartGroupInner>> getAllNextSinglePageAsync(String nextLink, Context context) {
        if (nextLink == null) {
            return Mono.error(new IllegalArgumentException("Parameter nextLink is required and cannot be null."));
        }
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .getAllNext(nextLink, this.client.getEndpoint(), accept, context)
            .map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null));
    }
}
