// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
//
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.quantum.jobs.implementation;

import com.azure.core.annotation.ExpectedResponses;
import com.azure.core.annotation.Get;
import com.azure.core.annotation.HeaderParam;
import com.azure.core.annotation.Host;
import com.azure.core.annotation.HostParam;
import com.azure.core.annotation.PathParam;
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.quantum.jobs.models.ProviderStatus;
import com.azure.quantum.jobs.models.ProviderStatusList;
import com.azure.quantum.jobs.models.RestErrorException;
import reactor.core.publisher.Mono;

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

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

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

    /**
     * The interface defining all the services for QuantumClientProviders to be used by the proxy service to perform
     * REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "QuantumClientProvide")
    private interface ProvidersService {
        @Get(
                "/v1.0/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Quantum/workspaces/{workspaceName}/providerStatus")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(RestErrorException.class)
        Mono<Response<ProviderStatusList>> getStatus(
                @HostParam("$host") String host,
                @PathParam("subscriptionId") String subscriptionId,
                @PathParam("resourceGroupName") String resourceGroupName,
                @PathParam("workspaceName") String workspaceName,
                @HeaderParam("Accept") String accept);

        @Get("{nextLink}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(RestErrorException.class)
        Mono<Response<ProviderStatusList>> getStatusNext(
                @PathParam(value = "nextLink", encoded = true) String nextLink,
                @HostParam("$host") String host,
                @HeaderParam("Accept") String accept);
    }

    /**
     * Get provider status.
     *
     * @throws RestErrorException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return provider status.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Mono<PagedResponse<ProviderStatus>> getStatusSinglePageAsync() {
        final String accept = "application/json";
        return service.getStatus(
                        this.client.getHost(),
                        this.client.getSubscriptionId(),
                        this.client.getResourceGroupName(),
                        this.client.getWorkspaceName(),
                        accept)
                .map(
                        res ->
                                new PagedResponseBase<>(
                                        res.getRequest(),
                                        res.getStatusCode(),
                                        res.getHeaders(),
                                        res.getValue().getValue(),
                                        res.getValue().getNextLink(),
                                        null));
    }

    /**
     * Get provider status.
     *
     * @throws RestErrorException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return provider status.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedFlux<ProviderStatus> getStatusAsync() {
        return new PagedFlux<>(() -> getStatusSinglePageAsync(), nextLink -> getStatusNextSinglePageAsync(nextLink));
    }

    /**
     * Get provider status.
     *
     * @throws RestErrorException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return provider status.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<ProviderStatus> getStatus() {
        return new PagedIterable<>(getStatusAsync());
    }

    /**
     * Get the next page of items.
     *
     * @param nextLink The nextLink parameter.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws RestErrorException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return providers status.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Mono<PagedResponse<ProviderStatus>> getStatusNextSinglePageAsync(String nextLink) {
        final String accept = "application/json";
        return service.getStatusNext(nextLink, this.client.getHost(), accept)
                .map(
                        res ->
                                new PagedResponseBase<>(
                                        res.getRequest(),
                                        res.getStatusCode(),
                                        res.getHeaders(),
                                        res.getValue().getValue(),
                                        res.getValue().getNextLink(),
                                        null));
    }
}
