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

package com.azure.resourcemanager.azurestack.implementation;

import com.azure.core.annotation.BodyParam;
import com.azure.core.annotation.ExpectedResponses;
import com.azure.core.annotation.HeaderParam;
import com.azure.core.annotation.Headers;
import com.azure.core.annotation.Host;
import com.azure.core.annotation.HostParam;
import com.azure.core.annotation.PathParam;
import com.azure.core.annotation.Post;
import com.azure.core.annotation.QueryParam;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceInterface;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.annotation.UnexpectedResponseExceptionType;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.RestProxy;
import com.azure.core.management.exception.ManagementException;
import com.azure.core.util.Context;
import com.azure.core.util.FluxUtil;
import com.azure.resourcemanager.azurestack.fluent.DeploymentLicensesClient;
import com.azure.resourcemanager.azurestack.fluent.models.DeploymentLicenseResponseInner;
import com.azure.resourcemanager.azurestack.models.DeploymentLicenseRequest;
import reactor.core.publisher.Mono;

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

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

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

    /**
     * The interface defining all the services for AzureStackManagementClientDeploymentLicenses to be used by the proxy
     * service to perform REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "AzureStackManagement")
    public interface DeploymentLicensesService {
        @Headers({"Content-Type: application/json"})
        @Post("/subscriptions/{subscriptionId}/providers/Microsoft.AzureStack/generateDeploymentLicense")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<DeploymentLicenseResponseInner>> create(
            @HostParam("$host") String endpoint,
            @PathParam("subscriptionId") String subscriptionId,
            @QueryParam("api-version") String apiVersion,
            @BodyParam("application/json") DeploymentLicenseRequest deploymentLicenseRequest,
            @HeaderParam("Accept") String accept,
            Context context);
    }

    /**
     * Creates a license that can be used to deploy an Azure Stack device.
     *
     * @param deploymentLicenseRequest Request body for creating a deployment license.
     * @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 license that can be used to deploy an Azure Stack device along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<DeploymentLicenseResponseInner>> createWithResponseAsync(
        DeploymentLicenseRequest deploymentLicenseRequest) {
        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 (deploymentLicenseRequest == null) {
            return Mono
                .error(
                    new IllegalArgumentException("Parameter deploymentLicenseRequest is required and cannot be null."));
        } else {
            deploymentLicenseRequest.validate();
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .create(
                            this.client.getEndpoint(),
                            this.client.getSubscriptionId(),
                            this.client.getApiVersion(),
                            deploymentLicenseRequest,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Creates a license that can be used to deploy an Azure Stack device.
     *
     * @param deploymentLicenseRequest Request body for creating a deployment license.
     * @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 a license that can be used to deploy an Azure Stack device along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<DeploymentLicenseResponseInner>> createWithResponseAsync(
        DeploymentLicenseRequest deploymentLicenseRequest, 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 (deploymentLicenseRequest == null) {
            return Mono
                .error(
                    new IllegalArgumentException("Parameter deploymentLicenseRequest is required and cannot be null."));
        } else {
            deploymentLicenseRequest.validate();
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .create(
                this.client.getEndpoint(),
                this.client.getSubscriptionId(),
                this.client.getApiVersion(),
                deploymentLicenseRequest,
                accept,
                context);
    }

    /**
     * Creates a license that can be used to deploy an Azure Stack device.
     *
     * @param deploymentLicenseRequest Request body for creating a deployment license.
     * @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 license that can be used to deploy an Azure Stack device on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<DeploymentLicenseResponseInner> createAsync(DeploymentLicenseRequest deploymentLicenseRequest) {
        return createWithResponseAsync(deploymentLicenseRequest).flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Creates a license that can be used to deploy an Azure Stack device.
     *
     * @param deploymentLicenseRequest Request body for creating a deployment license.
     * @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 a license that can be used to deploy an Azure Stack device along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<DeploymentLicenseResponseInner> createWithResponse(
        DeploymentLicenseRequest deploymentLicenseRequest, Context context) {
        return createWithResponseAsync(deploymentLicenseRequest, context).block();
    }

    /**
     * Creates a license that can be used to deploy an Azure Stack device.
     *
     * @param deploymentLicenseRequest Request body for creating a deployment license.
     * @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 license that can be used to deploy an Azure Stack device.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public DeploymentLicenseResponseInner create(DeploymentLicenseRequest deploymentLicenseRequest) {
        return createWithResponse(deploymentLicenseRequest, Context.NONE).getValue();
    }
}
