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

package com.azure.resourcemanager.agrifood.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.agrifood.fluent.FarmBeatsExtensionsClient;
import com.azure.resourcemanager.agrifood.fluent.models.FarmBeatsExtensionInner;
import com.azure.resourcemanager.agrifood.models.FarmBeatsExtensionListResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import reactor.core.publisher.Mono;

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

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

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

    /**
     * The interface defining all the services for AgriFoodManagementClientFarmBeatsExtensions to be used by the proxy
     * service to perform REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "AgriFoodManagementCl")
    private interface FarmBeatsExtensionsService {
        @Headers({"Content-Type: application/json"})
        @Get("/providers/Microsoft.AgFoodPlatform/farmBeatsExtensionDefinitions")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<FarmBeatsExtensionListResponse>> list(
            @HostParam("$host") String endpoint,
            @QueryParam(value = "farmBeatsExtensionIds", multipleQueryParams = true) List<String> farmBeatsExtensionIds,
            @QueryParam(value = "farmBeatsExtensionNames", multipleQueryParams = true)
                List<String> farmBeatsExtensionNames,
            @QueryParam(value = "extensionCategories", multipleQueryParams = true) List<String> extensionCategories,
            @QueryParam(value = "publisherIds", multipleQueryParams = true) List<String> publisherIds,
            @QueryParam("$maxPageSize") Integer maxPageSize,
            @QueryParam("api-version") String apiVersion,
            @HeaderParam("Accept") String accept,
            Context context);

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

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

    /**
     * Get list of farmBeats extension.
     *
     * @param farmBeatsExtensionIds FarmBeatsExtension ids.
     * @param farmBeatsExtensionNames FarmBeats extension names.
     * @param extensionCategories Extension categories.
     * @param publisherIds Publisher ids.
     * @param maxPageSize Maximum number of items needed (inclusive). Minimum = 10, Maximum = 1000, Default value = 50.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return list of farmBeats extension along with {@link PagedResponse} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<FarmBeatsExtensionInner>> listSinglePageAsync(
        List<String> farmBeatsExtensionIds,
        List<String> farmBeatsExtensionNames,
        List<String> extensionCategories,
        List<String> publisherIds,
        Integer maxPageSize) {
        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";
        List<String> farmBeatsExtensionIdsConverted =
            (farmBeatsExtensionIds == null)
                ? new ArrayList<>()
                : farmBeatsExtensionIds.stream().map(item -> Objects.toString(item, "")).collect(Collectors.toList());
        List<String> farmBeatsExtensionNamesConverted =
            (farmBeatsExtensionNames == null)
                ? new ArrayList<>()
                : farmBeatsExtensionNames.stream().map(item -> Objects.toString(item, "")).collect(Collectors.toList());
        List<String> extensionCategoriesConverted =
            (extensionCategories == null)
                ? new ArrayList<>()
                : extensionCategories.stream().map(item -> Objects.toString(item, "")).collect(Collectors.toList());
        List<String> publisherIdsConverted =
            (publisherIds == null)
                ? new ArrayList<>()
                : publisherIds.stream().map(item -> Objects.toString(item, "")).collect(Collectors.toList());
        return FluxUtil
            .withContext(
                context ->
                    service
                        .list(
                            this.client.getEndpoint(),
                            farmBeatsExtensionIdsConverted,
                            farmBeatsExtensionNamesConverted,
                            extensionCategoriesConverted,
                            publisherIdsConverted,
                            maxPageSize,
                            this.client.getApiVersion(),
                            accept,
                            context))
            .<PagedResponse<FarmBeatsExtensionInner>>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 list of farmBeats extension.
     *
     * @param farmBeatsExtensionIds FarmBeatsExtension ids.
     * @param farmBeatsExtensionNames FarmBeats extension names.
     * @param extensionCategories Extension categories.
     * @param publisherIds Publisher ids.
     * @param maxPageSize Maximum number of items needed (inclusive). Minimum = 10, Maximum = 1000, Default value = 50.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return list of farmBeats extension along with {@link PagedResponse} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<FarmBeatsExtensionInner>> listSinglePageAsync(
        List<String> farmBeatsExtensionIds,
        List<String> farmBeatsExtensionNames,
        List<String> extensionCategories,
        List<String> publisherIds,
        Integer maxPageSize,
        Context context) {
        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";
        List<String> farmBeatsExtensionIdsConverted =
            (farmBeatsExtensionIds == null)
                ? new ArrayList<>()
                : farmBeatsExtensionIds.stream().map(item -> Objects.toString(item, "")).collect(Collectors.toList());
        List<String> farmBeatsExtensionNamesConverted =
            (farmBeatsExtensionNames == null)
                ? new ArrayList<>()
                : farmBeatsExtensionNames.stream().map(item -> Objects.toString(item, "")).collect(Collectors.toList());
        List<String> extensionCategoriesConverted =
            (extensionCategories == null)
                ? new ArrayList<>()
                : extensionCategories.stream().map(item -> Objects.toString(item, "")).collect(Collectors.toList());
        List<String> publisherIdsConverted =
            (publisherIds == null)
                ? new ArrayList<>()
                : publisherIds.stream().map(item -> Objects.toString(item, "")).collect(Collectors.toList());
        context = this.client.mergeContext(context);
        return service
            .list(
                this.client.getEndpoint(),
                farmBeatsExtensionIdsConverted,
                farmBeatsExtensionNamesConverted,
                extensionCategoriesConverted,
                publisherIdsConverted,
                maxPageSize,
                this.client.getApiVersion(),
                accept,
                context)
            .map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null));
    }

    /**
     * Get list of farmBeats extension.
     *
     * @param farmBeatsExtensionIds FarmBeatsExtension ids.
     * @param farmBeatsExtensionNames FarmBeats extension names.
     * @param extensionCategories Extension categories.
     * @param publisherIds Publisher ids.
     * @param maxPageSize Maximum number of items needed (inclusive). Minimum = 10, Maximum = 1000, Default value = 50.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return list of farmBeats extension as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<FarmBeatsExtensionInner> listAsync(
        List<String> farmBeatsExtensionIds,
        List<String> farmBeatsExtensionNames,
        List<String> extensionCategories,
        List<String> publisherIds,
        Integer maxPageSize) {
        return new PagedFlux<>(
            () ->
                listSinglePageAsync(
                    farmBeatsExtensionIds, farmBeatsExtensionNames, extensionCategories, publisherIds, maxPageSize),
            nextLink -> listNextSinglePageAsync(nextLink));
    }

    /**
     * Get list of farmBeats extension.
     *
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return list of farmBeats extension as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<FarmBeatsExtensionInner> listAsync() {
        final List<String> farmBeatsExtensionIds = null;
        final List<String> farmBeatsExtensionNames = null;
        final List<String> extensionCategories = null;
        final List<String> publisherIds = null;
        final Integer maxPageSize = null;
        return new PagedFlux<>(
            () ->
                listSinglePageAsync(
                    farmBeatsExtensionIds, farmBeatsExtensionNames, extensionCategories, publisherIds, maxPageSize),
            nextLink -> listNextSinglePageAsync(nextLink));
    }

    /**
     * Get list of farmBeats extension.
     *
     * @param farmBeatsExtensionIds FarmBeatsExtension ids.
     * @param farmBeatsExtensionNames FarmBeats extension names.
     * @param extensionCategories Extension categories.
     * @param publisherIds Publisher ids.
     * @param maxPageSize Maximum number of items needed (inclusive). Minimum = 10, Maximum = 1000, Default value = 50.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return list of farmBeats extension as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<FarmBeatsExtensionInner> listAsync(
        List<String> farmBeatsExtensionIds,
        List<String> farmBeatsExtensionNames,
        List<String> extensionCategories,
        List<String> publisherIds,
        Integer maxPageSize,
        Context context) {
        return new PagedFlux<>(
            () ->
                listSinglePageAsync(
                    farmBeatsExtensionIds,
                    farmBeatsExtensionNames,
                    extensionCategories,
                    publisherIds,
                    maxPageSize,
                    context),
            nextLink -> listNextSinglePageAsync(nextLink, context));
    }

    /**
     * Get list of farmBeats extension.
     *
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return list of farmBeats extension as paginated response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<FarmBeatsExtensionInner> list() {
        final List<String> farmBeatsExtensionIds = null;
        final List<String> farmBeatsExtensionNames = null;
        final List<String> extensionCategories = null;
        final List<String> publisherIds = null;
        final Integer maxPageSize = null;
        return new PagedIterable<>(
            listAsync(farmBeatsExtensionIds, farmBeatsExtensionNames, extensionCategories, publisherIds, maxPageSize));
    }

    /**
     * Get list of farmBeats extension.
     *
     * @param farmBeatsExtensionIds FarmBeatsExtension ids.
     * @param farmBeatsExtensionNames FarmBeats extension names.
     * @param extensionCategories Extension categories.
     * @param publisherIds Publisher ids.
     * @param maxPageSize Maximum number of items needed (inclusive). Minimum = 10, Maximum = 1000, Default value = 50.
     * @param context The context to associate with this operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return list of farmBeats extension as paginated response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<FarmBeatsExtensionInner> list(
        List<String> farmBeatsExtensionIds,
        List<String> farmBeatsExtensionNames,
        List<String> extensionCategories,
        List<String> publisherIds,
        Integer maxPageSize,
        Context context) {
        return new PagedIterable<>(
            listAsync(
                farmBeatsExtensionIds,
                farmBeatsExtensionNames,
                extensionCategories,
                publisherIds,
                maxPageSize,
                context));
    }

    /**
     * Get farmBeats extension.
     *
     * @param farmBeatsExtensionId farmBeatsExtensionId to be queried.
     * @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 farmBeats extension along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<FarmBeatsExtensionInner>> getWithResponseAsync(String farmBeatsExtensionId) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (farmBeatsExtensionId == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter farmBeatsExtensionId is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .get(
                            this.client.getEndpoint(),
                            farmBeatsExtensionId,
                            this.client.getApiVersion(),
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Get farmBeats extension.
     *
     * @param farmBeatsExtensionId farmBeatsExtensionId to be queried.
     * @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 farmBeats extension along with {@link Response} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<FarmBeatsExtensionInner>> getWithResponseAsync(String farmBeatsExtensionId, Context context) {
        if (this.client.getEndpoint() == null) {
            return Mono
                .error(
                    new IllegalArgumentException(
                        "Parameter this.client.getEndpoint() is required and cannot be null."));
        }
        if (farmBeatsExtensionId == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter farmBeatsExtensionId is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .get(this.client.getEndpoint(), farmBeatsExtensionId, this.client.getApiVersion(), accept, context);
    }

    /**
     * Get farmBeats extension.
     *
     * @param farmBeatsExtensionId farmBeatsExtensionId to be queried.
     * @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 farmBeats extension on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<FarmBeatsExtensionInner> getAsync(String farmBeatsExtensionId) {
        return getWithResponseAsync(farmBeatsExtensionId).flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Get farmBeats extension.
     *
     * @param farmBeatsExtensionId farmBeatsExtensionId to be queried.
     * @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 farmBeats extension.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public FarmBeatsExtensionInner get(String farmBeatsExtensionId) {
        return getAsync(farmBeatsExtensionId).block();
    }

    /**
     * Get farmBeats extension.
     *
     * @param farmBeatsExtensionId farmBeatsExtensionId to be queried.
     * @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 farmBeats extension along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<FarmBeatsExtensionInner> getWithResponse(String farmBeatsExtensionId, Context context) {
        return getWithResponseAsync(farmBeatsExtensionId, context).block();
    }

    /**
     * Get the next page of items.
     *
     * @param nextLink The URL to get the next list of items
     *     <p>The nextLink parameter.
     * @throws IllegalArgumentException thrown if parameters fail the validation.
     * @throws ManagementException thrown if the request is rejected by server.
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
     * @return paged response contains list of requested objects and a URL link to get the next set of results along
     *     with {@link PagedResponse} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<FarmBeatsExtensionInner>> listNextSinglePageAsync(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.listNext(nextLink, this.client.getEndpoint(), accept, context))
            .<PagedResponse<FarmBeatsExtensionInner>>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 paged response contains list of requested objects and a URL link to get the next set of results along
     *     with {@link PagedResponse} on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<FarmBeatsExtensionInner>> listNextSinglePageAsync(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
            .listNext(nextLink, this.client.getEndpoint(), accept, context)
            .map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null));
    }
}
