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

package com.azure.resourcemanager.reservations.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.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.reservations.fluent.ResourceProvidersClient;
import com.azure.resourcemanager.reservations.fluent.models.AppliedReservationsInner;
import com.azure.resourcemanager.reservations.fluent.models.CatalogInner;
import com.azure.resourcemanager.reservations.models.CatalogsResult;
import reactor.core.publisher.Mono;

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

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

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

    /**
     * The interface defining all the services for AzureReservationApiResourceProviders to be used by the proxy service
     * to perform REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "AzureReservationApiR")
    public interface ResourceProvidersService {
        @Headers({"Content-Type: application/json"})
        @Get("/subscriptions/{subscriptionId}/providers/Microsoft.Capacity/catalogs")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<CatalogsResult>> list(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("subscriptionId") String subscriptionId,
            @QueryParam("reservedResourceType") String reservedResourceType,
            @QueryParam("location") String location,
            @QueryParam("publisherId") String publisherId,
            @QueryParam("offerId") String offerId,
            @QueryParam("planId") String planId,
            @QueryParam("$filter") String filter,
            @QueryParam("$skip") Float skip,
            @QueryParam("$take") Float take,
            @HeaderParam("Accept") String accept,
            Context context);

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

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

    /**
     * Get the regions and skus that are available for RI purchase for the specified Azure subscription.
     *
     * @param subscriptionId Id of the subscription.
     * @param reservedResourceType The type of the resource for which the skus should be provided.
     * @param location Filters the skus based on the location specified in this parameter. This can be an Azure region
     *     or global.
     * @param publisherId Publisher id used to get the third party products.
     * @param offerId Offer id used to get the third party products.
     * @param planId Plan id used to get the third party products.
     * @param filter May be used to filter by Catalog properties. The filter supports 'eq', 'or', and 'and'.
     * @param skip The number of reservations to skip from the list before returning results.
     * @param take To number of reservations to return.
     * @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 regions and skus that are available for RI purchase for the specified Azure subscription along with
     *     {@link PagedResponse} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<CatalogInner>> listSinglePageAsync(
        String subscriptionId,
        String reservedResourceType,
        String location,
        String publisherId,
        String offerId,
        String planId,
        String filter,
        Float skip,
        Float take) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (subscriptionId == null) {
            return Mono.error(new IllegalArgumentException("Parameter subscriptionId is required and cannot be null."));
        }
        final String apiVersion = "2022-11-01";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .list(
                            this.client.getEndpoint(),
                            apiVersion,
                            subscriptionId,
                            reservedResourceType,
                            location,
                            publisherId,
                            offerId,
                            planId,
                            filter,
                            skip,
                            take,
                            accept,
                            context))
            .<PagedResponse<CatalogInner>>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 regions and skus that are available for RI purchase for the specified Azure subscription.
     *
     * @param subscriptionId Id of the subscription.
     * @param reservedResourceType The type of the resource for which the skus should be provided.
     * @param location Filters the skus based on the location specified in this parameter. This can be an Azure region
     *     or global.
     * @param publisherId Publisher id used to get the third party products.
     * @param offerId Offer id used to get the third party products.
     * @param planId Plan id used to get the third party products.
     * @param filter May be used to filter by Catalog properties. The filter supports 'eq', 'or', and 'and'.
     * @param skip The number of reservations to skip from the list before returning results.
     * @param take To number of reservations to return.
     * @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 regions and skus that are available for RI purchase for the specified Azure subscription along with
     *     {@link PagedResponse} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<CatalogInner>> listSinglePageAsync(
        String subscriptionId,
        String reservedResourceType,
        String location,
        String publisherId,
        String offerId,
        String planId,
        String filter,
        Float skip,
        Float take,
        Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (subscriptionId == null) {
            return Mono.error(new IllegalArgumentException("Parameter subscriptionId is required and cannot be null."));
        }
        final String apiVersion = "2022-11-01";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .list(
                this.client.getEndpoint(),
                apiVersion,
                subscriptionId,
                reservedResourceType,
                location,
                publisherId,
                offerId,
                planId,
                filter,
                skip,
                take,
                accept,
                context)
            .map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null));
    }

    /**
     * Get the regions and skus that are available for RI purchase for the specified Azure subscription.
     *
     * @param subscriptionId Id of the subscription.
     * @param reservedResourceType The type of the resource for which the skus should be provided.
     * @param location Filters the skus based on the location specified in this parameter. This can be an Azure region
     *     or global.
     * @param publisherId Publisher id used to get the third party products.
     * @param offerId Offer id used to get the third party products.
     * @param planId Plan id used to get the third party products.
     * @param filter May be used to filter by Catalog properties. The filter supports 'eq', 'or', and 'and'.
     * @param skip The number of reservations to skip from the list before returning results.
     * @param take To number of reservations to return.
     * @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 regions and skus that are available for RI purchase for the specified Azure subscription as paginated
     *     response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<CatalogInner> listAsync(
        String subscriptionId,
        String reservedResourceType,
        String location,
        String publisherId,
        String offerId,
        String planId,
        String filter,
        Float skip,
        Float take) {
        return new PagedFlux<>(
            () ->
                listSinglePageAsync(
                    subscriptionId, reservedResourceType, location, publisherId, offerId, planId, filter, skip, take),
            nextLink -> getCatalogNextSinglePageAsync(nextLink));
    }

    /**
     * Get the regions and skus that are available for RI purchase for the specified Azure subscription.
     *
     * @param subscriptionId Id of the subscription.
     * @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 regions and skus that are available for RI purchase for the specified Azure subscription as paginated
     *     response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<CatalogInner> listAsync(String subscriptionId) {
        final String reservedResourceType = null;
        final String location = null;
        final String publisherId = null;
        final String offerId = null;
        final String planId = null;
        final String filter = null;
        final Float skip = null;
        final Float take = null;
        return new PagedFlux<>(
            () ->
                listSinglePageAsync(
                    subscriptionId, reservedResourceType, location, publisherId, offerId, planId, filter, skip, take),
            nextLink -> getCatalogNextSinglePageAsync(nextLink));
    }

    /**
     * Get the regions and skus that are available for RI purchase for the specified Azure subscription.
     *
     * @param subscriptionId Id of the subscription.
     * @param reservedResourceType The type of the resource for which the skus should be provided.
     * @param location Filters the skus based on the location specified in this parameter. This can be an Azure region
     *     or global.
     * @param publisherId Publisher id used to get the third party products.
     * @param offerId Offer id used to get the third party products.
     * @param planId Plan id used to get the third party products.
     * @param filter May be used to filter by Catalog properties. The filter supports 'eq', 'or', and 'and'.
     * @param skip The number of reservations to skip from the list before returning results.
     * @param take To number of reservations to return.
     * @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 regions and skus that are available for RI purchase for the specified Azure subscription as paginated
     *     response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<CatalogInner> listAsync(
        String subscriptionId,
        String reservedResourceType,
        String location,
        String publisherId,
        String offerId,
        String planId,
        String filter,
        Float skip,
        Float take,
        Context context) {
        return new PagedFlux<>(
            () ->
                listSinglePageAsync(
                    subscriptionId,
                    reservedResourceType,
                    location,
                    publisherId,
                    offerId,
                    planId,
                    filter,
                    skip,
                    take,
                    context),
            nextLink -> getCatalogNextSinglePageAsync(nextLink, context));
    }

    /**
     * Get the regions and skus that are available for RI purchase for the specified Azure subscription.
     *
     * @param subscriptionId Id of the subscription.
     * @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 regions and skus that are available for RI purchase for the specified Azure subscription as paginated
     *     response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<CatalogInner> list(String subscriptionId) {
        final String reservedResourceType = null;
        final String location = null;
        final String publisherId = null;
        final String offerId = null;
        final String planId = null;
        final String filter = null;
        final Float skip = null;
        final Float take = null;
        return new PagedIterable<>(
            listAsync(
                subscriptionId, reservedResourceType, location, publisherId, offerId, planId, filter, skip, take));
    }

    /**
     * Get the regions and skus that are available for RI purchase for the specified Azure subscription.
     *
     * @param subscriptionId Id of the subscription.
     * @param reservedResourceType The type of the resource for which the skus should be provided.
     * @param location Filters the skus based on the location specified in this parameter. This can be an Azure region
     *     or global.
     * @param publisherId Publisher id used to get the third party products.
     * @param offerId Offer id used to get the third party products.
     * @param planId Plan id used to get the third party products.
     * @param filter May be used to filter by Catalog properties. The filter supports 'eq', 'or', and 'and'.
     * @param skip The number of reservations to skip from the list before returning results.
     * @param take To number of reservations to return.
     * @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 regions and skus that are available for RI purchase for the specified Azure subscription as paginated
     *     response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<CatalogInner> list(
        String subscriptionId,
        String reservedResourceType,
        String location,
        String publisherId,
        String offerId,
        String planId,
        String filter,
        Float skip,
        Float take,
        Context context) {
        return new PagedIterable<>(
            listAsync(
                subscriptionId,
                reservedResourceType,
                location,
                publisherId,
                offerId,
                planId,
                filter,
                skip,
                take,
                context));
    }

    /**
     * Get list of applicable `Reservation`s.
     *
     * <p>Get applicable `Reservation`s that are applied to this subscription or a resource group under this
     * subscription.
     *
     * @param subscriptionId Id of the subscription.
     * @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 applicable `Reservation`s that are applied to this subscription or a resource group under this
     *     subscription along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AppliedReservationsInner>> getAppliedReservationListWithResponseAsync(String subscriptionId) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (subscriptionId == null) {
            return Mono.error(new IllegalArgumentException("Parameter subscriptionId is required and cannot be null."));
        }
        final String apiVersion = "2022-11-01";
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .getAppliedReservationList(
                            this.client.getEndpoint(), apiVersion, subscriptionId, accept, context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Get list of applicable `Reservation`s.
     *
     * <p>Get applicable `Reservation`s that are applied to this subscription or a resource group under this
     * subscription.
     *
     * @param subscriptionId Id of the subscription.
     * @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 applicable `Reservation`s that are applied to this subscription or a resource group under this
     *     subscription along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<AppliedReservationsInner>> getAppliedReservationListWithResponseAsync(
        String subscriptionId, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (subscriptionId == null) {
            return Mono.error(new IllegalArgumentException("Parameter subscriptionId is required and cannot be null."));
        }
        final String apiVersion = "2022-11-01";
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .getAppliedReservationList(this.client.getEndpoint(), apiVersion, subscriptionId, accept, context);
    }

    /**
     * Get list of applicable `Reservation`s.
     *
     * <p>Get applicable `Reservation`s that are applied to this subscription or a resource group under this
     * subscription.
     *
     * @param subscriptionId Id of the subscription.
     * @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 applicable `Reservation`s that are applied to this subscription or a resource group under this
     *     subscription on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<AppliedReservationsInner> getAppliedReservationListAsync(String subscriptionId) {
        return getAppliedReservationListWithResponseAsync(subscriptionId)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Get list of applicable `Reservation`s.
     *
     * <p>Get applicable `Reservation`s that are applied to this subscription or a resource group under this
     * subscription.
     *
     * @param subscriptionId Id of the subscription.
     * @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 applicable `Reservation`s that are applied to this subscription or a resource group under this
     *     subscription along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<AppliedReservationsInner> getAppliedReservationListWithResponse(
        String subscriptionId, Context context) {
        return getAppliedReservationListWithResponseAsync(subscriptionId, context).block();
    }

    /**
     * Get list of applicable `Reservation`s.
     *
     * <p>Get applicable `Reservation`s that are applied to this subscription or a resource group under this
     * subscription.
     *
     * @param subscriptionId Id of the subscription.
     * @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 applicable `Reservation`s that are applied to this subscription or a resource group under this
     *     subscription.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public AppliedReservationsInner getAppliedReservationList(String subscriptionId) {
        return getAppliedReservationListWithResponse(subscriptionId, Context.NONE).getValue();
    }

    /**
     * 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 the list of catalogs and pagination information along with {@link PagedResponse} on successful completion
     *     of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<CatalogInner>> getCatalogNextSinglePageAsync(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.getCatalogNext(nextLink, this.client.getEndpoint(), accept, context))
            .<PagedResponse<CatalogInner>>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 the list of catalogs and pagination information along with {@link PagedResponse} on successful completion
     *     of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<CatalogInner>> getCatalogNextSinglePageAsync(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
            .getCatalogNext(nextLink, this.client.getEndpoint(), accept, context)
            .map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null));
    }
}
