/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT License.
 *
 * Code generated by Microsoft (R) AutoRest Code Generator.
 * Changes may cause incorrect behavior and will be lost if the code is regenerated.
 */

import { PagedAsyncIterableIterator, PageSettings } from "@azure/core-paging";
import { setContinuationToken } from "../pagingHelper";
import { ContainerAppsRevisions } from "../operationsInterfaces";
import * as coreClient from "@azure/core-client";
import * as Mappers from "../models/mappers";
import * as Parameters from "../models/parameters";
import { WebSiteManagementClient } from "../webSiteManagementClient";
import {
  Revision,
  ContainerAppsRevisionsListRevisionsNextOptionalParams,
  ContainerAppsRevisionsListRevisionsOptionalParams,
  ContainerAppsRevisionsListRevisionsResponse,
  ContainerAppsRevisionsGetRevisionOptionalParams,
  ContainerAppsRevisionsGetRevisionResponse,
  ContainerAppsRevisionsActivateRevisionOptionalParams,
  ContainerAppsRevisionsDeactivateRevisionOptionalParams,
  ContainerAppsRevisionsRestartRevisionOptionalParams,
  ContainerAppsRevisionsListRevisionsNextResponse
} from "../models";

/// <reference lib="esnext.asynciterable" />
/** Class containing ContainerAppsRevisions operations. */
export class ContainerAppsRevisionsImpl implements ContainerAppsRevisions {
  private readonly client: WebSiteManagementClient;

  /**
   * Initialize a new instance of the class ContainerAppsRevisions class.
   * @param client Reference to the service client
   */
  constructor(client: WebSiteManagementClient) {
    this.client = client;
  }

  /**
   * Get the Revisions for a given Container App.
   * @param resourceGroupName Name of the resource group to which the resource belongs.
   * @param containerAppName Name of the Container App for which Revisions are needed.
   * @param options The options parameters.
   */
  public listRevisions(
    resourceGroupName: string,
    containerAppName: string,
    options?: ContainerAppsRevisionsListRevisionsOptionalParams
  ): PagedAsyncIterableIterator<Revision> {
    const iter = this.listRevisionsPagingAll(
      resourceGroupName,
      containerAppName,
      options
    );
    return {
      next() {
        return iter.next();
      },
      [Symbol.asyncIterator]() {
        return this;
      },
      byPage: (settings?: PageSettings) => {
        if (settings?.maxPageSize) {
          throw new Error("maxPageSize is not supported by this operation.");
        }
        return this.listRevisionsPagingPage(
          resourceGroupName,
          containerAppName,
          options,
          settings
        );
      }
    };
  }

  private async *listRevisionsPagingPage(
    resourceGroupName: string,
    containerAppName: string,
    options?: ContainerAppsRevisionsListRevisionsOptionalParams,
    settings?: PageSettings
  ): AsyncIterableIterator<Revision[]> {
    let result: ContainerAppsRevisionsListRevisionsResponse;
    let continuationToken = settings?.continuationToken;
    if (!continuationToken) {
      result = await this._listRevisions(
        resourceGroupName,
        containerAppName,
        options
      );
      let page = result.value || [];
      continuationToken = result.nextLink;
      setContinuationToken(page, continuationToken);
      yield page;
    }
    while (continuationToken) {
      result = await this._listRevisionsNext(
        resourceGroupName,
        containerAppName,
        continuationToken,
        options
      );
      continuationToken = result.nextLink;
      let page = result.value || [];
      setContinuationToken(page, continuationToken);
      yield page;
    }
  }

  private async *listRevisionsPagingAll(
    resourceGroupName: string,
    containerAppName: string,
    options?: ContainerAppsRevisionsListRevisionsOptionalParams
  ): AsyncIterableIterator<Revision> {
    for await (const page of this.listRevisionsPagingPage(
      resourceGroupName,
      containerAppName,
      options
    )) {
      yield* page;
    }
  }

  /**
   * Get the Revisions for a given Container App.
   * @param resourceGroupName Name of the resource group to which the resource belongs.
   * @param containerAppName Name of the Container App for which Revisions are needed.
   * @param options The options parameters.
   */
  private _listRevisions(
    resourceGroupName: string,
    containerAppName: string,
    options?: ContainerAppsRevisionsListRevisionsOptionalParams
  ): Promise<ContainerAppsRevisionsListRevisionsResponse> {
    return this.client.sendOperationRequest(
      { resourceGroupName, containerAppName, options },
      listRevisionsOperationSpec
    );
  }

  /**
   * Get a revision of a Container App.
   * @param resourceGroupName Name of the resource group to which the resource belongs.
   * @param containerAppName Name of the Container App.
   * @param name Name of the Container App Revision.
   * @param options The options parameters.
   */
  getRevision(
    resourceGroupName: string,
    containerAppName: string,
    name: string,
    options?: ContainerAppsRevisionsGetRevisionOptionalParams
  ): Promise<ContainerAppsRevisionsGetRevisionResponse> {
    return this.client.sendOperationRequest(
      { resourceGroupName, containerAppName, name, options },
      getRevisionOperationSpec
    );
  }

  /**
   * Activates a revision for a Container App
   * @param resourceGroupName Name of the resource group to which the resource belongs.
   * @param containerAppName Name of the Container App.
   * @param name Name of the Container App Revision to activate
   * @param options The options parameters.
   */
  activateRevision(
    resourceGroupName: string,
    containerAppName: string,
    name: string,
    options?: ContainerAppsRevisionsActivateRevisionOptionalParams
  ): Promise<void> {
    return this.client.sendOperationRequest(
      { resourceGroupName, containerAppName, name, options },
      activateRevisionOperationSpec
    );
  }

  /**
   * Deactivates a revision for a Container App
   * @param resourceGroupName Name of the resource group to which the resource belongs.
   * @param containerAppName Name of the Container App.
   * @param name Name of the Container App Revision to deactivate
   * @param options The options parameters.
   */
  deactivateRevision(
    resourceGroupName: string,
    containerAppName: string,
    name: string,
    options?: ContainerAppsRevisionsDeactivateRevisionOptionalParams
  ): Promise<void> {
    return this.client.sendOperationRequest(
      { resourceGroupName, containerAppName, name, options },
      deactivateRevisionOperationSpec
    );
  }

  /**
   * Restarts a revision for a Container App
   * @param resourceGroupName Name of the resource group to which the resource belongs.
   * @param containerAppName Name of the Container App.
   * @param name Name of the Container App Revision to restart
   * @param options The options parameters.
   */
  restartRevision(
    resourceGroupName: string,
    containerAppName: string,
    name: string,
    options?: ContainerAppsRevisionsRestartRevisionOptionalParams
  ): Promise<void> {
    return this.client.sendOperationRequest(
      { resourceGroupName, containerAppName, name, options },
      restartRevisionOperationSpec
    );
  }

  /**
   * ListRevisionsNext
   * @param resourceGroupName Name of the resource group to which the resource belongs.
   * @param containerAppName Name of the Container App for which Revisions are needed.
   * @param nextLink The nextLink from the previous successful call to the ListRevisions method.
   * @param options The options parameters.
   */
  private _listRevisionsNext(
    resourceGroupName: string,
    containerAppName: string,
    nextLink: string,
    options?: ContainerAppsRevisionsListRevisionsNextOptionalParams
  ): Promise<ContainerAppsRevisionsListRevisionsNextResponse> {
    return this.client.sendOperationRequest(
      { resourceGroupName, containerAppName, nextLink, options },
      listRevisionsNextOperationSpec
    );
  }
}
// Operation Specifications
const serializer = coreClient.createSerializer(Mappers, /* isXml */ false);

const listRevisionsOperationSpec: coreClient.OperationSpec = {
  path:
    "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/containerApps/{containerAppName}/revisions",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper: Mappers.RevisionCollection
    },
    default: {
      bodyMapper: Mappers.DefaultErrorResponse
    }
  },
  queryParameters: [Parameters.apiVersion],
  urlParameters: [
    Parameters.$host,
    Parameters.subscriptionId,
    Parameters.resourceGroupName,
    Parameters.containerAppName
  ],
  headerParameters: [Parameters.accept],
  serializer
};
const getRevisionOperationSpec: coreClient.OperationSpec = {
  path:
    "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/containerApps/{containerAppName}/revisions/{name}",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper: Mappers.Revision
    },
    default: {
      bodyMapper: Mappers.DefaultErrorResponse
    }
  },
  queryParameters: [Parameters.apiVersion],
  urlParameters: [
    Parameters.$host,
    Parameters.subscriptionId,
    Parameters.resourceGroupName,
    Parameters.name,
    Parameters.containerAppName
  ],
  headerParameters: [Parameters.accept],
  serializer
};
const activateRevisionOperationSpec: coreClient.OperationSpec = {
  path:
    "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/containerApps/{containerAppName}/revisions/{name}/activate",
  httpMethod: "POST",
  responses: {
    200: {},
    default: {
      bodyMapper: Mappers.DefaultErrorResponse
    }
  },
  queryParameters: [Parameters.apiVersion],
  urlParameters: [
    Parameters.$host,
    Parameters.subscriptionId,
    Parameters.resourceGroupName,
    Parameters.name,
    Parameters.containerAppName
  ],
  headerParameters: [Parameters.accept],
  serializer
};
const deactivateRevisionOperationSpec: coreClient.OperationSpec = {
  path:
    "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/containerApps/{containerAppName}/revisions/{name}/deactivate",
  httpMethod: "POST",
  responses: {
    200: {},
    default: {
      bodyMapper: Mappers.DefaultErrorResponse
    }
  },
  queryParameters: [Parameters.apiVersion],
  urlParameters: [
    Parameters.$host,
    Parameters.subscriptionId,
    Parameters.resourceGroupName,
    Parameters.name,
    Parameters.containerAppName
  ],
  headerParameters: [Parameters.accept],
  serializer
};
const restartRevisionOperationSpec: coreClient.OperationSpec = {
  path:
    "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/containerApps/{containerAppName}/revisions/{name}/restart",
  httpMethod: "POST",
  responses: {
    200: {},
    default: {
      bodyMapper: Mappers.DefaultErrorResponse
    }
  },
  queryParameters: [Parameters.apiVersion],
  urlParameters: [
    Parameters.$host,
    Parameters.subscriptionId,
    Parameters.resourceGroupName,
    Parameters.name,
    Parameters.containerAppName
  ],
  headerParameters: [Parameters.accept],
  serializer
};
const listRevisionsNextOperationSpec: coreClient.OperationSpec = {
  path: "{nextLink}",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper: Mappers.RevisionCollection
    },
    default: {
      bodyMapper: Mappers.DefaultErrorResponse
    }
  },
  urlParameters: [
    Parameters.$host,
    Parameters.subscriptionId,
    Parameters.resourceGroupName,
    Parameters.nextLink,
    Parameters.containerAppName
  ],
  headerParameters: [Parameters.accept],
  serializer
};
