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

package com.azure.resourcemanager.hybridcontainerservice.implementation;

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

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

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

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

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

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

        @Headers({"Content-Type: application/json"})
        @Patch(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers"
                + "/Microsoft.HybridContainerService/provisionedClusters/{resourceName}/agentPools/{agentPoolName}")
        @ExpectedResponses({200, 202})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<AgentPoolInner>> update(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("resourceName") String resourceName,
            @PathParam("agentPoolName") String agentPoolName,
            @QueryParam("api-version") String apiVersion,
            @BodyParam("application/json") AgentPoolInner agentPool,
            @HeaderParam("Accept") String accept,
            Context context);

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

    /**
     * Gets the agent pool in the provisioned cluster
     *
     * <p>Gets the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @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 agent pool in the Hybrid AKS provisioned cluster along with {@link Response} on successful completion
     *     of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgentPoolInner>> getWithResponseAsync(
        String resourceGroupName, String resourceName, String agentPoolName) {
        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 (resourceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceName is required and cannot be null."));
        }
        if (agentPoolName == null) {
            return Mono.error(new IllegalArgumentException("Parameter agentPoolName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .get(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            resourceName,
                            agentPoolName,
                            this.client.getApiVersion(),
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Gets the agent pool in the provisioned cluster
     *
     * <p>Gets the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @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 agent pool in the Hybrid AKS provisioned cluster along with {@link Response} on successful completion
     *     of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgentPoolInner>> getWithResponseAsync(
        String resourceGroupName, String resourceName, String agentPoolName, 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 (resourceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceName is required and cannot be null."));
        }
        if (agentPoolName == null) {
            return Mono.error(new IllegalArgumentException("Parameter agentPoolName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .get(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                resourceName,
                agentPoolName,
                this.client.getApiVersion(),
                accept,
                context);
    }

    /**
     * Gets the agent pool in the provisioned cluster
     *
     * <p>Gets the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @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 agent pool in the Hybrid AKS provisioned cluster on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<AgentPoolInner> getAsync(String resourceGroupName, String resourceName, String agentPoolName) {
        return getWithResponseAsync(resourceGroupName, resourceName, agentPoolName)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Gets the agent pool in the provisioned cluster
     *
     * <p>Gets the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @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 agent pool in the Hybrid AKS provisioned cluster along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<AgentPoolInner> getWithResponse(
        String resourceGroupName, String resourceName, String agentPoolName, Context context) {
        return getWithResponseAsync(resourceGroupName, resourceName, agentPoolName, context).block();
    }

    /**
     * Gets the agent pool in the provisioned cluster
     *
     * <p>Gets the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @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 agent pool in the Hybrid AKS provisioned cluster.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public AgentPoolInner get(String resourceGroupName, String resourceName, String agentPoolName) {
        return getWithResponse(resourceGroupName, resourceName, agentPoolName, Context.NONE).getValue();
    }

    /**
     * Creates the agent pool in the provisioned cluster
     *
     * <p>Creates the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param agentPool The agentPool resource definition.
     * @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 agentPool resource definition along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> createOrUpdateWithResponseAsync(
        String resourceGroupName, String resourceName, String agentPoolName, AgentPoolInner agentPool) {
        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 (resourceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceName is required and cannot be null."));
        }
        if (agentPoolName == null) {
            return Mono.error(new IllegalArgumentException("Parameter agentPoolName is required and cannot be null."));
        }
        if (agentPool == null) {
            return Mono.error(new IllegalArgumentException("Parameter agentPool is required and cannot be null."));
        } else {
            agentPool.validate();
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .createOrUpdate(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            resourceName,
                            agentPoolName,
                            this.client.getApiVersion(),
                            agentPool,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Creates the agent pool in the provisioned cluster
     *
     * <p>Creates the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param agentPool The agentPool resource definition.
     * @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 agentPool resource definition along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> createOrUpdateWithResponseAsync(
        String resourceGroupName,
        String resourceName,
        String agentPoolName,
        AgentPoolInner agentPool,
        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 (resourceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceName is required and cannot be null."));
        }
        if (agentPoolName == null) {
            return Mono.error(new IllegalArgumentException("Parameter agentPoolName is required and cannot be null."));
        }
        if (agentPool == null) {
            return Mono.error(new IllegalArgumentException("Parameter agentPool is required and cannot be null."));
        } else {
            agentPool.validate();
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .createOrUpdate(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                resourceName,
                agentPoolName,
                this.client.getApiVersion(),
                agentPool,
                accept,
                context);
    }

    /**
     * Creates the agent pool in the provisioned cluster
     *
     * <p>Creates the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param agentPool The agentPool resource definition.
     * @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 the agentPool resource definition.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<AgentPoolInner>, AgentPoolInner> beginCreateOrUpdateAsync(
        String resourceGroupName, String resourceName, String agentPoolName, AgentPoolInner agentPool) {
        Mono<Response<Flux<ByteBuffer>>> mono =
            createOrUpdateWithResponseAsync(resourceGroupName, resourceName, agentPoolName, agentPool);
        return this
            .client
            .<AgentPoolInner, AgentPoolInner>getLroResult(
                mono,
                this.client.getHttpPipeline(),
                AgentPoolInner.class,
                AgentPoolInner.class,
                this.client.getContext());
    }

    /**
     * Creates the agent pool in the provisioned cluster
     *
     * <p>Creates the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param agentPool The agentPool resource definition.
     * @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 the agentPool resource definition.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<AgentPoolInner>, AgentPoolInner> beginCreateOrUpdateAsync(
        String resourceGroupName,
        String resourceName,
        String agentPoolName,
        AgentPoolInner agentPool,
        Context context) {
        context = this.client.mergeContext(context);
        Mono<Response<Flux<ByteBuffer>>> mono =
            createOrUpdateWithResponseAsync(resourceGroupName, resourceName, agentPoolName, agentPool, context);
        return this
            .client
            .<AgentPoolInner, AgentPoolInner>getLroResult(
                mono, this.client.getHttpPipeline(), AgentPoolInner.class, AgentPoolInner.class, context);
    }

    /**
     * Creates the agent pool in the provisioned cluster
     *
     * <p>Creates the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param agentPool The agentPool resource definition.
     * @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 the agentPool resource definition.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<AgentPoolInner>, AgentPoolInner> beginCreateOrUpdate(
        String resourceGroupName, String resourceName, String agentPoolName, AgentPoolInner agentPool) {
        return this.beginCreateOrUpdateAsync(resourceGroupName, resourceName, agentPoolName, agentPool).getSyncPoller();
    }

    /**
     * Creates the agent pool in the provisioned cluster
     *
     * <p>Creates the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param agentPool The agentPool resource definition.
     * @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 the agentPool resource definition.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<AgentPoolInner>, AgentPoolInner> beginCreateOrUpdate(
        String resourceGroupName,
        String resourceName,
        String agentPoolName,
        AgentPoolInner agentPool,
        Context context) {
        return this
            .beginCreateOrUpdateAsync(resourceGroupName, resourceName, agentPoolName, agentPool, context)
            .getSyncPoller();
    }

    /**
     * Creates the agent pool in the provisioned cluster
     *
     * <p>Creates the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param agentPool The agentPool resource definition.
     * @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 agentPool resource definition on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<AgentPoolInner> createOrUpdateAsync(
        String resourceGroupName, String resourceName, String agentPoolName, AgentPoolInner agentPool) {
        return beginCreateOrUpdateAsync(resourceGroupName, resourceName, agentPoolName, agentPool)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Creates the agent pool in the provisioned cluster
     *
     * <p>Creates the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param agentPool The agentPool resource definition.
     * @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 agentPool resource definition on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<AgentPoolInner> createOrUpdateAsync(
        String resourceGroupName,
        String resourceName,
        String agentPoolName,
        AgentPoolInner agentPool,
        Context context) {
        return beginCreateOrUpdateAsync(resourceGroupName, resourceName, agentPoolName, agentPool, context)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Creates the agent pool in the provisioned cluster
     *
     * <p>Creates the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param agentPool The agentPool resource definition.
     * @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 agentPool resource definition.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public AgentPoolInner createOrUpdate(
        String resourceGroupName, String resourceName, String agentPoolName, AgentPoolInner agentPool) {
        return createOrUpdateAsync(resourceGroupName, resourceName, agentPoolName, agentPool).block();
    }

    /**
     * Creates the agent pool in the provisioned cluster
     *
     * <p>Creates the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param agentPool The agentPool resource definition.
     * @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 agentPool resource definition.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public AgentPoolInner createOrUpdate(
        String resourceGroupName,
        String resourceName,
        String agentPoolName,
        AgentPoolInner agentPool,
        Context context) {
        return createOrUpdateAsync(resourceGroupName, resourceName, agentPoolName, agentPool, context).block();
    }

    /**
     * Deletes the agent pool in the provisioned cluster
     *
     * <p>Deletes the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Void>> deleteWithResponseAsync(
        String resourceGroupName, String resourceName, String agentPoolName) {
        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 (resourceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceName is required and cannot be null."));
        }
        if (agentPoolName == null) {
            return Mono.error(new IllegalArgumentException("Parameter agentPoolName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .delete(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            resourceName,
                            agentPoolName,
                            this.client.getApiVersion(),
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Deletes the agent pool in the provisioned cluster
     *
     * <p>Deletes the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Void>> deleteWithResponseAsync(
        String resourceGroupName, String resourceName, String agentPoolName, 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 (resourceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceName is required and cannot be null."));
        }
        if (agentPoolName == null) {
            return Mono.error(new IllegalArgumentException("Parameter agentPoolName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .delete(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                resourceName,
                agentPoolName,
                this.client.getApiVersion(),
                accept,
                context);
    }

    /**
     * Deletes the agent pool in the provisioned cluster
     *
     * <p>Deletes the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return A {@link Mono} that completes when a successful response is received.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Void> deleteAsync(String resourceGroupName, String resourceName, String agentPoolName) {
        return deleteWithResponseAsync(resourceGroupName, resourceName, agentPoolName).flatMap(ignored -> Mono.empty());
    }

    /**
     * Deletes the agent pool in the provisioned cluster
     *
     * <p>Deletes the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return the {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<Void> deleteWithResponse(
        String resourceGroupName, String resourceName, String agentPoolName, Context context) {
        return deleteWithResponseAsync(resourceGroupName, resourceName, agentPoolName, context).block();
    }

    /**
     * Deletes the agent pool in the provisioned cluster
     *
     * <p>Deletes the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public void delete(String resourceGroupName, String resourceName, String agentPoolName) {
        deleteWithResponse(resourceGroupName, resourceName, agentPoolName, Context.NONE);
    }

    /**
     * Updates the agent pool in the provisioned cluster
     *
     * <p>Updates the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param agentPool The agentPool resource definition.
     * @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 agentPool resource definition along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgentPoolInner>> updateWithResponseAsync(
        String resourceGroupName, String resourceName, String agentPoolName, AgentPoolInner agentPool) {
        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 (resourceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceName is required and cannot be null."));
        }
        if (agentPoolName == null) {
            return Mono.error(new IllegalArgumentException("Parameter agentPoolName is required and cannot be null."));
        }
        if (agentPool == null) {
            return Mono.error(new IllegalArgumentException("Parameter agentPool is required and cannot be null."));
        } else {
            agentPool.validate();
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .update(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            resourceName,
                            agentPoolName,
                            this.client.getApiVersion(),
                            agentPool,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Updates the agent pool in the provisioned cluster
     *
     * <p>Updates the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param agentPool The agentPool resource definition.
     * @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 agentPool resource definition along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgentPoolInner>> updateWithResponseAsync(
        String resourceGroupName,
        String resourceName,
        String agentPoolName,
        AgentPoolInner agentPool,
        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 (resourceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceName is required and cannot be null."));
        }
        if (agentPoolName == null) {
            return Mono.error(new IllegalArgumentException("Parameter agentPoolName is required and cannot be null."));
        }
        if (agentPool == null) {
            return Mono.error(new IllegalArgumentException("Parameter agentPool is required and cannot be null."));
        } else {
            agentPool.validate();
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .update(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                resourceName,
                agentPoolName,
                this.client.getApiVersion(),
                agentPool,
                accept,
                context);
    }

    /**
     * Updates the agent pool in the provisioned cluster
     *
     * <p>Updates the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param agentPool The agentPool resource definition.
     * @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 agentPool resource definition on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<AgentPoolInner> updateAsync(
        String resourceGroupName, String resourceName, String agentPoolName, AgentPoolInner agentPool) {
        return updateWithResponseAsync(resourceGroupName, resourceName, agentPoolName, agentPool)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Updates the agent pool in the provisioned cluster
     *
     * <p>Updates the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param agentPool The agentPool resource definition.
     * @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 agentPool resource definition along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<AgentPoolInner> updateWithResponse(
        String resourceGroupName,
        String resourceName,
        String agentPoolName,
        AgentPoolInner agentPool,
        Context context) {
        return updateWithResponseAsync(resourceGroupName, resourceName, agentPoolName, agentPool, context).block();
    }

    /**
     * Updates the agent pool in the provisioned cluster
     *
     * <p>Updates the agent pool in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @param agentPoolName Parameter for the name of the agent pool in the provisioned cluster.
     * @param agentPool The agentPool resource definition.
     * @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 agentPool resource definition.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public AgentPoolInner update(
        String resourceGroupName, String resourceName, String agentPoolName, AgentPoolInner agentPool) {
        return updateWithResponse(resourceGroupName, resourceName, agentPoolName, agentPool, Context.NONE).getValue();
    }

    /**
     * Gets the agent pools in the provisioned cluster
     *
     * <p>Gets the agent pools in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @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 agent pools in the Hybrid AKS provisioned cluster along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgentPoolListResultInner>> listByProvisionedClusterWithResponseAsync(
        String resourceGroupName, String resourceName) {
        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 (resourceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .listByProvisionedCluster(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            resourceName,
                            this.client.getApiVersion(),
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Gets the agent pools in the provisioned cluster
     *
     * <p>Gets the agent pools in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @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 agent pools in the Hybrid AKS provisioned cluster along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AgentPoolListResultInner>> listByProvisionedClusterWithResponseAsync(
        String resourceGroupName, String resourceName, 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 (resourceName == null) {
            return Mono.error(new IllegalArgumentException("Parameter resourceName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .listByProvisionedCluster(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                resourceName,
                this.client.getApiVersion(),
                accept,
                context);
    }

    /**
     * Gets the agent pools in the provisioned cluster
     *
     * <p>Gets the agent pools in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @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 agent pools in the Hybrid AKS provisioned cluster on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<AgentPoolListResultInner> listByProvisionedClusterAsync(
        String resourceGroupName, String resourceName) {
        return listByProvisionedClusterWithResponseAsync(resourceGroupName, resourceName)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Gets the agent pools in the provisioned cluster
     *
     * <p>Gets the agent pools in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @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 agent pools in the Hybrid AKS provisioned cluster along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<AgentPoolListResultInner> listByProvisionedClusterWithResponse(
        String resourceGroupName, String resourceName, Context context) {
        return listByProvisionedClusterWithResponseAsync(resourceGroupName, resourceName, context).block();
    }

    /**
     * Gets the agent pools in the provisioned cluster
     *
     * <p>Gets the agent pools in the Hybrid AKS provisioned cluster.
     *
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param resourceName Parameter for the name of the provisioned cluster.
     * @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 agent pools in the Hybrid AKS provisioned cluster.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public AgentPoolListResultInner listByProvisionedCluster(String resourceGroupName, String resourceName) {
        return listByProvisionedClusterWithResponse(resourceGroupName, resourceName, Context.NONE).getValue();
    }
}
