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

package com.azure.resourcemanager.imagebuilder.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.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.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.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.imagebuilder.fluent.TriggersClient;
import com.azure.resourcemanager.imagebuilder.fluent.models.TriggerInner;
import com.azure.resourcemanager.imagebuilder.models.TriggerCollection;
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 TriggersClient. */
public final class TriggersClientImpl implements TriggersClient {
    /** The proxy service used to perform REST calls. */
    private final TriggersService service;

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

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

    /**
     * The interface defining all the services for ImageBuilderClientTriggers to be used by the proxy service to perform
     * REST calls.
     */
    @Host("{$host}")
    @ServiceInterface(name = "ImageBuilderClientTr")
    public interface TriggersService {
        @Headers({"Content-Type: application/json"})
        @Get(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.VirtualMachineImages/imageTemplates/{imageTemplateName}/triggers")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<TriggerCollection>> listByImageTemplate(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("imageTemplateName") String imageTemplateName,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Get(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.VirtualMachineImages/imageTemplates/{imageTemplateName}/triggers/{triggerName}")
        @ExpectedResponses({200})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<TriggerInner>> get(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("imageTemplateName") String imageTemplateName,
            @PathParam("triggerName") String triggerName,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Put(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.VirtualMachineImages/imageTemplates/{imageTemplateName}/triggers/{triggerName}")
        @ExpectedResponses({200, 201})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<Flux<ByteBuffer>>> createOrUpdate(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("imageTemplateName") String imageTemplateName,
            @PathParam("triggerName") String triggerName,
            @BodyParam("application/json") TriggerInner parameters,
            @HeaderParam("Accept") String accept,
            Context context);

        @Headers({"Content-Type: application/json"})
        @Delete(
            "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.VirtualMachineImages/imageTemplates/{imageTemplateName}/triggers/{triggerName}")
        @ExpectedResponses({200, 202, 204})
        @UnexpectedResponseExceptionType(ManagementException.class)
        Mono<Response<Flux<ByteBuffer>>> delete(
            @HostParam("$host") String endpoint,
            @QueryParam("api-version") String apiVersion,
            @PathParam("subscriptionId") String subscriptionId,
            @PathParam("resourceGroupName") String resourceGroupName,
            @PathParam("imageTemplateName") String imageTemplateName,
            @PathParam("triggerName") String triggerName,
            @HeaderParam("Accept") String accept,
            Context context);

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

    /**
     * List all triggers for the specified Image Template resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @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 result of List triggers operation along with {@link PagedResponse} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<TriggerInner>> listByImageTemplateSinglePageAsync(
        String resourceGroupName, String imageTemplateName) {
        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 (imageTemplateName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter imageTemplateName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .listByImageTemplate(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            imageTemplateName,
                            accept,
                            context))
            .<PagedResponse<TriggerInner>>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()));
    }

    /**
     * List all triggers for the specified Image Template resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @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 result of List triggers operation along with {@link PagedResponse} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<TriggerInner>> listByImageTemplateSinglePageAsync(
        String resourceGroupName, String imageTemplateName, 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 (imageTemplateName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter imageTemplateName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .listByImageTemplate(
                this.client.getEndpoint(),
                this.client.getApiVersion(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                imageTemplateName,
                accept,
                context)
            .map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null));
    }

    /**
     * List all triggers for the specified Image Template resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @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 result of List triggers operation as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<TriggerInner> listByImageTemplateAsync(String resourceGroupName, String imageTemplateName) {
        return new PagedFlux<>(
            () -> listByImageTemplateSinglePageAsync(resourceGroupName, imageTemplateName),
            nextLink -> listByImageTemplateNextSinglePageAsync(nextLink));
    }

    /**
     * List all triggers for the specified Image Template resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @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 result of List triggers operation as paginated response with {@link PagedFlux}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    private PagedFlux<TriggerInner> listByImageTemplateAsync(
        String resourceGroupName, String imageTemplateName, Context context) {
        return new PagedFlux<>(
            () -> listByImageTemplateSinglePageAsync(resourceGroupName, imageTemplateName, context),
            nextLink -> listByImageTemplateNextSinglePageAsync(nextLink, context));
    }

    /**
     * List all triggers for the specified Image Template resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @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 result of List triggers operation as paginated response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<TriggerInner> listByImageTemplate(String resourceGroupName, String imageTemplateName) {
        return new PagedIterable<>(listByImageTemplateAsync(resourceGroupName, imageTemplateName));
    }

    /**
     * List all triggers for the specified Image Template resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @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 result of List triggers operation as paginated response with {@link PagedIterable}.
     */
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<TriggerInner> listByImageTemplate(
        String resourceGroupName, String imageTemplateName, Context context) {
        return new PagedIterable<>(listByImageTemplateAsync(resourceGroupName, imageTemplateName, context));
    }

    /**
     * Get the specified trigger for the specified image template resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @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 specified trigger for the specified image template resource along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<TriggerInner>> getWithResponseAsync(
        String resourceGroupName, String imageTemplateName, String triggerName) {
        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 (imageTemplateName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter imageTemplateName is required and cannot be null."));
        }
        if (triggerName == null) {
            return Mono.error(new IllegalArgumentException("Parameter triggerName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .get(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            imageTemplateName,
                            triggerName,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Get the specified trigger for the specified image template resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @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 specified trigger for the specified image template resource along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<TriggerInner>> getWithResponseAsync(
        String resourceGroupName, String imageTemplateName, String triggerName, 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 (imageTemplateName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter imageTemplateName is required and cannot be null."));
        }
        if (triggerName == null) {
            return Mono.error(new IllegalArgumentException("Parameter triggerName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .get(
                this.client.getEndpoint(),
                this.client.getApiVersion(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                imageTemplateName,
                triggerName,
                accept,
                context);
    }

    /**
     * Get the specified trigger for the specified image template resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @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 specified trigger for the specified image template resource on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<TriggerInner> getAsync(String resourceGroupName, String imageTemplateName, String triggerName) {
        return getWithResponseAsync(resourceGroupName, imageTemplateName, triggerName)
            .flatMap(res -> Mono.justOrEmpty(res.getValue()));
    }

    /**
     * Get the specified trigger for the specified image template resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @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 specified trigger for the specified image template resource along with {@link Response}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public Response<TriggerInner> getWithResponse(
        String resourceGroupName, String imageTemplateName, String triggerName, Context context) {
        return getWithResponseAsync(resourceGroupName, imageTemplateName, triggerName, context).block();
    }

    /**
     * Get the specified trigger for the specified image template resource.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @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 specified trigger for the specified image template resource.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public TriggerInner get(String resourceGroupName, String imageTemplateName, String triggerName) {
        return getWithResponse(resourceGroupName, imageTemplateName, triggerName, Context.NONE).getValue();
    }

    /**
     * Create or update a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @param parameters Parameters supplied to the CreateTrigger 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 represents a trigger that can invoke an image template build along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> createOrUpdateWithResponseAsync(
        String resourceGroupName, String imageTemplateName, String triggerName, TriggerInner parameters) {
        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 (imageTemplateName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter imageTemplateName is required and cannot be null."));
        }
        if (triggerName == null) {
            return Mono.error(new IllegalArgumentException("Parameter triggerName is required and cannot be null."));
        }
        if (parameters == null) {
            return Mono.error(new IllegalArgumentException("Parameter parameters is required and cannot be null."));
        } else {
            parameters.validate();
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .createOrUpdate(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            imageTemplateName,
                            triggerName,
                            parameters,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Create or update a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @param parameters Parameters supplied to the CreateTrigger operation.
     * @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 represents a trigger that can invoke an image template build along with {@link Response} on successful
     *     completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Response<Flux<ByteBuffer>>> createOrUpdateWithResponseAsync(
        String resourceGroupName,
        String imageTemplateName,
        String triggerName,
        TriggerInner parameters,
        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 (imageTemplateName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter imageTemplateName is required and cannot be null."));
        }
        if (triggerName == null) {
            return Mono.error(new IllegalArgumentException("Parameter triggerName is required and cannot be null."));
        }
        if (parameters == null) {
            return Mono.error(new IllegalArgumentException("Parameter parameters is required and cannot be null."));
        } else {
            parameters.validate();
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .createOrUpdate(
                this.client.getEndpoint(),
                this.client.getApiVersion(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                imageTemplateName,
                triggerName,
                parameters,
                accept,
                context);
    }

    /**
     * Create or update a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @param parameters Parameters supplied to the CreateTrigger 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 represents a trigger that can invoke an image template build.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<TriggerInner>, TriggerInner> beginCreateOrUpdateAsync(
        String resourceGroupName, String imageTemplateName, String triggerName, TriggerInner parameters) {
        Mono<Response<Flux<ByteBuffer>>> mono =
            createOrUpdateWithResponseAsync(resourceGroupName, imageTemplateName, triggerName, parameters);
        return this
            .client
            .<TriggerInner, TriggerInner>getLroResult(
                mono, this.client.getHttpPipeline(), TriggerInner.class, TriggerInner.class, this.client.getContext());
    }

    /**
     * Create or update a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @param parameters Parameters supplied to the CreateTrigger operation.
     * @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 represents a trigger that can invoke an image template build.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<TriggerInner>, TriggerInner> beginCreateOrUpdateAsync(
        String resourceGroupName,
        String imageTemplateName,
        String triggerName,
        TriggerInner parameters,
        Context context) {
        context = this.client.mergeContext(context);
        Mono<Response<Flux<ByteBuffer>>> mono =
            createOrUpdateWithResponseAsync(resourceGroupName, imageTemplateName, triggerName, parameters, context);
        return this
            .client
            .<TriggerInner, TriggerInner>getLroResult(
                mono, this.client.getHttpPipeline(), TriggerInner.class, TriggerInner.class, context);
    }

    /**
     * Create or update a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @param parameters Parameters supplied to the CreateTrigger 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 represents a trigger that can invoke an image template build.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<TriggerInner>, TriggerInner> beginCreateOrUpdate(
        String resourceGroupName, String imageTemplateName, String triggerName, TriggerInner parameters) {
        return this
            .beginCreateOrUpdateAsync(resourceGroupName, imageTemplateName, triggerName, parameters)
            .getSyncPoller();
    }

    /**
     * Create or update a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @param parameters Parameters supplied to the CreateTrigger operation.
     * @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 represents a trigger that can invoke an image template build.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<TriggerInner>, TriggerInner> beginCreateOrUpdate(
        String resourceGroupName,
        String imageTemplateName,
        String triggerName,
        TriggerInner parameters,
        Context context) {
        return this
            .beginCreateOrUpdateAsync(resourceGroupName, imageTemplateName, triggerName, parameters, context)
            .getSyncPoller();
    }

    /**
     * Create or update a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @param parameters Parameters supplied to the CreateTrigger 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 represents a trigger that can invoke an image template build on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<TriggerInner> createOrUpdateAsync(
        String resourceGroupName, String imageTemplateName, String triggerName, TriggerInner parameters) {
        return beginCreateOrUpdateAsync(resourceGroupName, imageTemplateName, triggerName, parameters)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Create or update a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @param parameters Parameters supplied to the CreateTrigger operation.
     * @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 represents a trigger that can invoke an image template build on successful completion of {@link Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<TriggerInner> createOrUpdateAsync(
        String resourceGroupName,
        String imageTemplateName,
        String triggerName,
        TriggerInner parameters,
        Context context) {
        return beginCreateOrUpdateAsync(resourceGroupName, imageTemplateName, triggerName, parameters, context)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Create or update a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @param parameters Parameters supplied to the CreateTrigger 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 represents a trigger that can invoke an image template build.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public TriggerInner createOrUpdate(
        String resourceGroupName, String imageTemplateName, String triggerName, TriggerInner parameters) {
        return createOrUpdateAsync(resourceGroupName, imageTemplateName, triggerName, parameters).block();
    }

    /**
     * Create or update a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @param parameters Parameters supplied to the CreateTrigger operation.
     * @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 represents a trigger that can invoke an image template build.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public TriggerInner createOrUpdate(
        String resourceGroupName,
        String imageTemplateName,
        String triggerName,
        TriggerInner parameters,
        Context context) {
        return createOrUpdateAsync(resourceGroupName, imageTemplateName, triggerName, parameters, context).block();
    }

    /**
     * Delete a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @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<Flux<ByteBuffer>>> deleteWithResponseAsync(
        String resourceGroupName, String imageTemplateName, String triggerName) {
        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 (imageTemplateName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter imageTemplateName is required and cannot be null."));
        }
        if (triggerName == null) {
            return Mono.error(new IllegalArgumentException("Parameter triggerName is required and cannot be null."));
        }
        final String accept = "application/json";
        return FluxUtil
            .withContext(
                context ->
                    service
                        .delete(
                            this.client.getEndpoint(),
                            this.client.getApiVersion(),
                            this.client.getSubscriptionId(),
                            resourceGroupName,
                            imageTemplateName,
                            triggerName,
                            accept,
                            context))
            .contextWrite(context -> context.putAll(FluxUtil.toReactorContext(this.client.getContext()).readOnly()));
    }

    /**
     * Delete a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @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<Flux<ByteBuffer>>> deleteWithResponseAsync(
        String resourceGroupName, String imageTemplateName, String triggerName, 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 (imageTemplateName == null) {
            return Mono
                .error(new IllegalArgumentException("Parameter imageTemplateName is required and cannot be null."));
        }
        if (triggerName == null) {
            return Mono.error(new IllegalArgumentException("Parameter triggerName is required and cannot be null."));
        }
        final String accept = "application/json";
        context = this.client.mergeContext(context);
        return service
            .delete(
                this.client.getEndpoint(),
                this.client.getApiVersion(),
                this.client.getSubscriptionId(),
                resourceGroupName,
                imageTemplateName,
                triggerName,
                accept,
                context);
    }

    /**
     * Delete a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @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 long-running operation.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<Void>, Void> beginDeleteAsync(
        String resourceGroupName, String imageTemplateName, String triggerName) {
        Mono<Response<Flux<ByteBuffer>>> mono =
            deleteWithResponseAsync(resourceGroupName, imageTemplateName, triggerName);
        return this
            .client
            .<Void, Void>getLroResult(
                mono, this.client.getHttpPipeline(), Void.class, Void.class, this.client.getContext());
    }

    /**
     * Delete a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @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 long-running operation.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    private PollerFlux<PollResult<Void>, Void> beginDeleteAsync(
        String resourceGroupName, String imageTemplateName, String triggerName, Context context) {
        context = this.client.mergeContext(context);
        Mono<Response<Flux<ByteBuffer>>> mono =
            deleteWithResponseAsync(resourceGroupName, imageTemplateName, triggerName, context);
        return this
            .client
            .<Void, Void>getLroResult(mono, this.client.getHttpPipeline(), Void.class, Void.class, context);
    }

    /**
     * Delete a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @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 long-running operation.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<Void>, Void> beginDelete(
        String resourceGroupName, String imageTemplateName, String triggerName) {
        return this.beginDeleteAsync(resourceGroupName, imageTemplateName, triggerName).getSyncPoller();
    }

    /**
     * Delete a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @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 long-running operation.
     */
    @ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
    public SyncPoller<PollResult<Void>, Void> beginDelete(
        String resourceGroupName, String imageTemplateName, String triggerName, Context context) {
        return this.beginDeleteAsync(resourceGroupName, imageTemplateName, triggerName, context).getSyncPoller();
    }

    /**
     * Delete a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @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 imageTemplateName, String triggerName) {
        return beginDeleteAsync(resourceGroupName, imageTemplateName, triggerName)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Delete a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @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 {@link Mono} that completes when a successful response is received.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<Void> deleteAsync(
        String resourceGroupName, String imageTemplateName, String triggerName, Context context) {
        return beginDeleteAsync(resourceGroupName, imageTemplateName, triggerName, context)
            .last()
            .flatMap(this.client::getLroFinalResultOrError);
    }

    /**
     * Delete a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @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 imageTemplateName, String triggerName) {
        deleteAsync(resourceGroupName, imageTemplateName, triggerName).block();
    }

    /**
     * Delete a trigger for the specified virtual machine image template.
     *
     * @param resourceGroupName The name of the resource group.
     * @param imageTemplateName The name of the image Template.
     * @param triggerName The name of the trigger.
     * @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.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    public void delete(String resourceGroupName, String imageTemplateName, String triggerName, Context context) {
        deleteAsync(resourceGroupName, imageTemplateName, triggerName, 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 the result of List triggers operation along with {@link PagedResponse} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<TriggerInner>> listByImageTemplateNextSinglePageAsync(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.listByImageTemplateNext(nextLink, this.client.getEndpoint(), accept, context))
            .<PagedResponse<TriggerInner>>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 result of List triggers operation along with {@link PagedResponse} on successful completion of {@link
     *     Mono}.
     */
    @ServiceMethod(returns = ReturnType.SINGLE)
    private Mono<PagedResponse<TriggerInner>> listByImageTemplateNextSinglePageAsync(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
            .listByImageTemplateNext(nextLink, this.client.getEndpoint(), accept, context)
            .map(
                res ->
                    new PagedResponseBase<>(
                        res.getRequest(),
                        res.getStatusCode(),
                        res.getHeaders(),
                        res.getValue().value(),
                        res.getValue().nextLink(),
                        null));
    }
}
