/*
 * 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 { Builds } from "../operationsInterfaces";
import * as coreClient from "@azure/core-client";
import * as Mappers from "../models/mappers";
import * as Parameters from "../models/parameters";
import { AppCenterClient } from "../appCenterClient";
import {
  BuildsListXcodeVersionsOptionalParams,
  BuildsListXcodeVersionsResponse,
  BuildsListXamarinSDKBundlesOptionalParams,
  BuildsListXamarinSDKBundlesResponse,
  BuildsListToolsetsOptionalParams,
  BuildsListToolsetsResponse,
  BuildsGetLogOptionalParams,
  BuildsGetLogResponse,
  Enum475,
  BuildsGetDownloadUriOptionalParams,
  BuildsGetDownloadUriResponse,
  Paths1P28L53V01AppsOwnerNameAppNameBuildsBuildIdDistributePostRequestbodyContentApplicationJsonSchema,
  BuildsDistributeOptionalParams,
  BuildsDistributeResponse,
  BuildsGetOptionalParams,
  BuildsGetResponse,
  BuildsUpdateOptionalParams,
  BuildsUpdateResponse,
  BuildsGetStatusByAppIdOptionalParams,
  BuildsGetStatusByAppIdResponse,
  Enum484,
  Enum485,
  BuildsListToolsetProjectsOptionalParams,
  BuildsListToolsetProjectsResponse,
  BuildsListByBranchOptionalParams,
  BuildsListByBranchResponse,
  BuildsCreateOptionalParams,
  BuildsCreateResponse,
  BuildsListBranchesOptionalParams,
  BuildsListBranchesResponse
} from "../models";

/** Class containing Builds operations. */
export class BuildsImpl implements Builds {
  private readonly client: AppCenterClient;

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

  /**
   * Gets the Xcode versions available to this app
   * @param ownerName The name of the owner
   * @param appName The name of the application
   * @param options The options parameters.
   */
  listXcodeVersions(
    ownerName: string,
    appName: string,
    options?: BuildsListXcodeVersionsOptionalParams
  ): Promise<BuildsListXcodeVersionsResponse> {
    return this.client.sendOperationRequest(
      { ownerName, appName, options },
      listXcodeVersionsOperationSpec
    );
  }

  /**
   * Gets the Xamarin SDK bundles available to this app
   * @param ownerName The name of the owner
   * @param appName The name of the application
   * @param options The options parameters.
   */
  listXamarinSDKBundles(
    ownerName: string,
    appName: string,
    options?: BuildsListXamarinSDKBundlesOptionalParams
  ): Promise<BuildsListXamarinSDKBundlesResponse> {
    return this.client.sendOperationRequest(
      { ownerName, appName, options },
      listXamarinSDKBundlesOperationSpec
    );
  }

  /**
   * Returns available toolsets for application
   * @param ownerName The name of the owner
   * @param appName The name of the application
   * @param options The options parameters.
   */
  listToolsets(
    ownerName: string,
    appName: string,
    options?: BuildsListToolsetsOptionalParams
  ): Promise<BuildsListToolsetsResponse> {
    return this.client.sendOperationRequest(
      { ownerName, appName, options },
      listToolsetsOperationSpec
    );
  }

  /**
   * Get the build log
   * @param buildId The build ID
   * @param ownerName The name of the owner
   * @param appName The name of the application
   * @param options The options parameters.
   */
  getLog(
    buildId: number,
    ownerName: string,
    appName: string,
    options?: BuildsGetLogOptionalParams
  ): Promise<BuildsGetLogResponse> {
    return this.client.sendOperationRequest(
      { buildId, ownerName, appName, options },
      getLogOperationSpec
    );
  }

  /**
   * Gets the download URI
   * @param buildId The build ID
   * @param downloadType The download type
   * @param ownerName The name of the owner
   * @param appName The name of the application
   * @param options The options parameters.
   */
  getDownloadUri(
    buildId: number,
    downloadType: Enum475,
    ownerName: string,
    appName: string,
    options?: BuildsGetDownloadUriOptionalParams
  ): Promise<BuildsGetDownloadUriResponse> {
    return this.client.sendOperationRequest(
      { buildId, downloadType, ownerName, appName, options },
      getDownloadUriOperationSpec
    );
  }

  /**
   * Distribute a build
   * @param buildId The build ID
   * @param ownerName The name of the owner
   * @param appName The name of the application
   * @param distributeInfo The distribution details
   * @param options The options parameters.
   */
  distribute(
    buildId: number,
    ownerName: string,
    appName: string,
    distributeInfo: Paths1P28L53V01AppsOwnerNameAppNameBuildsBuildIdDistributePostRequestbodyContentApplicationJsonSchema,
    options?: BuildsDistributeOptionalParams
  ): Promise<BuildsDistributeResponse> {
    return this.client.sendOperationRequest(
      { buildId, ownerName, appName, distributeInfo, options },
      distributeOperationSpec
    );
  }

  /**
   * Returns the build detail for the given build ID
   * @param buildId The build ID
   * @param ownerName The name of the owner
   * @param appName The name of the application
   * @param options The options parameters.
   */
  get(
    buildId: number,
    ownerName: string,
    appName: string,
    options?: BuildsGetOptionalParams
  ): Promise<BuildsGetResponse> {
    return this.client.sendOperationRequest(
      { buildId, ownerName, appName, options },
      getOperationSpec
    );
  }

  /**
   * Cancels a build
   * @param buildId The build ID
   * @param ownerName The name of the owner
   * @param appName The name of the application
   * @param options The options parameters.
   */
  update(
    buildId: number,
    ownerName: string,
    appName: string,
    options?: BuildsUpdateOptionalParams
  ): Promise<BuildsUpdateResponse> {
    return this.client.sendOperationRequest(
      { buildId, ownerName, appName, options },
      updateOperationSpec
    );
  }

  /**
   * Application specific build service status
   * @param ownerName The name of the owner
   * @param appName The name of the application
   * @param options The options parameters.
   */
  getStatusByAppId(
    ownerName: string,
    appName: string,
    options?: BuildsGetStatusByAppIdOptionalParams
  ): Promise<BuildsGetStatusByAppIdResponse> {
    return this.client.sendOperationRequest(
      { ownerName, appName, options },
      getStatusByAppIdOperationSpec
    );
  }

  /**
   * Returns the projects in the repository for the branch, for all toolsets
   * @param branch The branch name
   * @param os The desired OS for the project scan; normally the same as the app OS
   * @param platform The desired platform for the project scan
   * @param ownerName The name of the owner
   * @param appName The name of the application
   * @param options The options parameters.
   */
  listToolsetProjects(
    branch: string,
    os: Enum484,
    platform: Enum485,
    ownerName: string,
    appName: string,
    options?: BuildsListToolsetProjectsOptionalParams
  ): Promise<BuildsListToolsetProjectsResponse> {
    return this.client.sendOperationRequest(
      { branch, os, platform, ownerName, appName, options },
      listToolsetProjectsOperationSpec
    );
  }

  /**
   * Returns the list of builds for the branch
   * @param branch The branch name
   * @param ownerName The name of the owner
   * @param appName The name of the application
   * @param options The options parameters.
   */
  listByBranch(
    branch: string,
    ownerName: string,
    appName: string,
    options?: BuildsListByBranchOptionalParams
  ): Promise<BuildsListByBranchResponse> {
    return this.client.sendOperationRequest(
      { branch, ownerName, appName, options },
      listByBranchOperationSpec
    );
  }

  /**
   * Create a build
   * @param branch The branch name
   * @param ownerName The name of the owner
   * @param appName The name of the application
   * @param options The options parameters.
   */
  create(
    branch: string,
    ownerName: string,
    appName: string,
    options?: BuildsCreateOptionalParams
  ): Promise<BuildsCreateResponse> {
    return this.client.sendOperationRequest(
      { branch, ownerName, appName, options },
      createOperationSpec
    );
  }

  /**
   * Returns the list of Git branches for this application
   * @param ownerName The name of the owner
   * @param appName The name of the application
   * @param options The options parameters.
   */
  listBranches(
    ownerName: string,
    appName: string,
    options?: BuildsListBranchesOptionalParams
  ): Promise<BuildsListBranchesResponse> {
    return this.client.sendOperationRequest(
      { ownerName, appName, options },
      listBranchesOperationSpec
    );
  }
}
// Operation Specifications
const serializer = coreClient.createSerializer(Mappers, /* isXml */ false);

const listXcodeVersionsOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/apps/{owner_name}/{app_name}/xcode_versions",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper: {
        type: {
          name: "Sequence",
          element: {
            type: {
              name: "Composite",
              className:
                "PathsSnbseaV01AppsOwnerNameAppNameXcodeVersionsGetResponses200ContentApplicationJsonSchemaItems"
            }
          }
        }
      }
    },
    default: {
      bodyMapper:
        Mappers.Paths9Yy0MkV01AppsOwnerNameAppNameXcodeVersionsGetResponsesDefaultContentApplicationJsonSchema
    }
  },
  urlParameters: [Parameters.$host, Parameters.ownerName, Parameters.appName],
  headerParameters: [Parameters.accept],
  serializer
};
const listXamarinSDKBundlesOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/apps/{owner_name}/{app_name}/xamarin_sdk_bundles",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper: {
        type: {
          name: "Sequence",
          element: {
            type: {
              name: "Composite",
              className:
                "Paths1Nv9E3SV01AppsOwnerNameAppNameXamarinSdkBundlesGetResponses200ContentApplicationJsonSchemaItems"
            }
          }
        }
      }
    },
    default: {
      bodyMapper:
        Mappers.Paths1G32ByzV01AppsOwnerNameAppNameXamarinSdkBundlesGetResponsesDefaultContentApplicationJsonSchema
    }
  },
  urlParameters: [Parameters.$host, Parameters.ownerName, Parameters.appName],
  headerParameters: [Parameters.accept],
  serializer
};
const listToolsetsOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/apps/{owner_name}/{app_name}/toolsets",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper:
        Mappers.PathsJw4J38V01AppsOwnerNameAppNameToolsetsGetResponses200ContentApplicationJsonSchema
    },
    default: {
      bodyMapper:
        Mappers.Paths1Dh1CmbV01AppsOwnerNameAppNameToolsetsGetResponsesDefaultContentApplicationJsonSchema
    }
  },
  queryParameters: [Parameters.tools],
  urlParameters: [Parameters.$host, Parameters.ownerName, Parameters.appName],
  headerParameters: [Parameters.accept],
  serializer
};
const getLogOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/apps/{owner_name}/{app_name}/builds/{build_id}/logs",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper:
        Mappers.Paths1A4WxwuV01AppsOwnerNameAppNameBuildsBuildIdLogsGetResponses200ContentApplicationJsonSchema
    }
  },
  urlParameters: [
    Parameters.$host,
    Parameters.ownerName,
    Parameters.appName,
    Parameters.buildId
  ],
  headerParameters: [Parameters.accept],
  serializer
};
const getDownloadUriOperationSpec: coreClient.OperationSpec = {
  path:
    "/v0.1/apps/{owner_name}/{app_name}/builds/{build_id}/downloads/{download_type}",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper:
        Mappers.PathsQ75XzuV01AppsOwnerNameAppNameBuildsBuildIdDownloadsDownloadTypeGetResponses200ContentApplicationJsonSchema
    }
  },
  urlParameters: [
    Parameters.$host,
    Parameters.ownerName,
    Parameters.appName,
    Parameters.buildId,
    Parameters.downloadType
  ],
  headerParameters: [Parameters.accept],
  serializer
};
const distributeOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/apps/{owner_name}/{app_name}/builds/{build_id}/distribute",
  httpMethod: "POST",
  responses: {
    200: {
      bodyMapper:
        Mappers.Paths1D42Nj4V01AppsOwnerNameAppNameBuildsBuildIdDistributePostResponses200ContentApplicationJsonSchema
    }
  },
  requestBody: Parameters.distributeInfo,
  urlParameters: [
    Parameters.$host,
    Parameters.ownerName,
    Parameters.appName,
    Parameters.buildId
  ],
  headerParameters: [Parameters.contentType, Parameters.accept],
  mediaType: "json",
  serializer
};
const getOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/apps/{owner_name}/{app_name}/builds/{build_id}",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper:
        Mappers.PathsN3VrpV01AppsOwnerNameAppNameBuildsBuildIdGetResponses200ContentApplicationJsonSchema
    }
  },
  urlParameters: [
    Parameters.$host,
    Parameters.ownerName,
    Parameters.appName,
    Parameters.buildId
  ],
  headerParameters: [Parameters.accept],
  serializer
};
const updateOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/apps/{owner_name}/{app_name}/builds/{build_id}",
  httpMethod: "PATCH",
  responses: {
    200: {
      bodyMapper:
        Mappers.Paths1Kppxb1V01AppsOwnerNameAppNameBuildsBuildIdPatchResponses200ContentApplicationJsonSchema
    }
  },
  requestBody: {
    parameterPath: { status: ["options", "status"] },
    mapper: {
      ...Mappers.PathsFcn32WV01AppsOwnerNameAppNameBuildsBuildIdPatchRequestbodyContentApplicationJsonSchema,
      required: true
    }
  },
  urlParameters: [
    Parameters.$host,
    Parameters.ownerName,
    Parameters.appName,
    Parameters.buildId
  ],
  headerParameters: [Parameters.contentType, Parameters.accept],
  mediaType: "json",
  serializer
};
const getStatusByAppIdOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/apps/{owner_name}/{app_name}/build_service_status",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper:
        Mappers.PathsPo7UnwV01AppsOwnerNameAppNameBuildServiceStatusGetResponses200ContentApplicationJsonSchema
    },
    default: {}
  },
  urlParameters: [Parameters.$host, Parameters.ownerName, Parameters.appName],
  headerParameters: [Parameters.accept],
  serializer
};
const listToolsetProjectsOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/apps/{owner_name}/{app_name}/branches/{branch}/toolset_projects",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper:
        Mappers.Paths17D5VcoV01AppsOwnerNameAppNameBranchesBranchToolsetProjectsGetResponses200ContentApplicationJsonSchema
    }
  },
  queryParameters: [
    Parameters.os,
    Parameters.platform,
    Parameters.maxSearchDepth
  ],
  urlParameters: [
    Parameters.$host,
    Parameters.ownerName,
    Parameters.appName,
    Parameters.branch
  ],
  headerParameters: [Parameters.accept],
  serializer
};
const listByBranchOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/apps/{owner_name}/{app_name}/branches/{branch}/builds",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper: {
        type: {
          name: "Sequence",
          element: {
            type: {
              name: "Composite",
              className:
                "PathsRyxelmV01AppsOwnerNameAppNameBranchesBranchBuildsGetResponses200ContentApplicationJsonSchemaItems"
            }
          }
        }
      }
    }
  },
  urlParameters: [
    Parameters.$host,
    Parameters.ownerName,
    Parameters.appName,
    Parameters.branch
  ],
  headerParameters: [Parameters.accept],
  serializer
};
const createOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/apps/{owner_name}/{app_name}/branches/{branch}/builds",
  httpMethod: "POST",
  responses: {
    200: {
      bodyMapper:
        Mappers.PathsIgnnsjV01AppsOwnerNameAppNameBranchesBranchBuildsPostResponses200ContentApplicationJsonSchema
    }
  },
  requestBody: {
    parameterPath: {
      sourceVersion: ["options", "sourceVersion"],
      debug: ["options", "debug"]
    },
    mapper:
      Mappers.Paths1Jgsne9V01AppsOwnerNameAppNameBranchesBranchBuildsPostRequestbodyContentApplicationJsonSchema
  },
  urlParameters: [
    Parameters.$host,
    Parameters.ownerName,
    Parameters.appName,
    Parameters.branch
  ],
  headerParameters: [Parameters.contentType, Parameters.accept],
  mediaType: "json",
  serializer
};
const listBranchesOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/apps/{owner_name}/{app_name}/branches",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper: {
        type: {
          name: "Sequence",
          element: {
            type: {
              name: "Composite",
              className:
                "Paths5Mg2CrV01AppsOwnerNameAppNameBranchesGetResponses200ContentApplicationJsonSchemaItems"
            }
          }
        }
      }
    },
    default: {
      bodyMapper:
        Mappers.Paths1WpnegrV01AppsOwnerNameAppNameBranchesGetResponsesDefaultContentApplicationJsonSchema
    }
  },
  urlParameters: [Parameters.$host, Parameters.ownerName, Parameters.appName],
  headerParameters: [Parameters.accept],
  serializer
};
