/*
 * 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 { Users } 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 {
  UsersGetUserMetadataOptionalParams,
  UsersGetUserMetadataResponse,
  UsersGetOptionalParams,
  UsersGetResponse,
  UsersUpdateOptionalParams,
  UsersUpdateResponse,
  UsersUpdateOrgRoleOptionalParams,
  UsersUpdateOrgRoleResponse,
  UsersRemoveFromOrgOptionalParams,
  UsersGetForOrgOptionalParams,
  UsersGetForOrgResponse,
  UsersListForOrgOptionalParams,
  UsersListForOrgResponse,
  UsersListOptionalParams,
  UsersListResponse
} from "../models";

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

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

  /**
   * test
   * @param options The options parameters.
   */
  getUserMetadata(
    options?: UsersGetUserMetadataOptionalParams
  ): Promise<UsersGetUserMetadataResponse> {
    return this.client.sendOperationRequest(
      { options },
      getUserMetadataOperationSpec
    );
  }

  /**
   * Returns the user profile data
   * @param options The options parameters.
   */
  get(options?: UsersGetOptionalParams): Promise<UsersGetResponse> {
    return this.client.sendOperationRequest({ options }, getOperationSpec);
  }

  /**
   * Updates the user profile and returns the updated user data
   * @param options The options parameters.
   */
  update(options?: UsersUpdateOptionalParams): Promise<UsersUpdateResponse> {
    return this.client.sendOperationRequest({ options }, updateOperationSpec);
  }

  /**
   * Updates the given organization user
   * @param orgName The organization's name
   * @param userName The slug name of the user
   * @param options The options parameters.
   */
  updateOrgRole(
    orgName: string,
    userName: string,
    options?: UsersUpdateOrgRoleOptionalParams
  ): Promise<UsersUpdateOrgRoleResponse> {
    return this.client.sendOperationRequest(
      { orgName, userName, options },
      updateOrgRoleOperationSpec
    );
  }

  /**
   * Removes a user from an organization.
   * @param orgName The organization's name
   * @param userName The slug name of the user
   * @param options The options parameters.
   */
  removeFromOrg(
    orgName: string,
    userName: string,
    options?: UsersRemoveFromOrgOptionalParams
  ): Promise<void> {
    return this.client.sendOperationRequest(
      { orgName, userName, options },
      removeFromOrgOperationSpec
    );
  }

  /**
   * Get a user information from an organization by name - if there is explicit permission return it, if
   * not if not return highest implicit permission
   * @param orgName The organization's name
   * @param userName The slug name of the user
   * @param options The options parameters.
   */
  getForOrg(
    orgName: string,
    userName: string,
    options?: UsersGetForOrgOptionalParams
  ): Promise<UsersGetForOrgResponse> {
    return this.client.sendOperationRequest(
      { orgName, userName, options },
      getForOrgOperationSpec
    );
  }

  /**
   * Returns a list of users that belong to an organization
   * @param orgName The organization's name
   * @param options The options parameters.
   */
  listForOrg(
    orgName: string,
    options?: UsersListForOrgOptionalParams
  ): Promise<UsersListForOrgResponse> {
    return this.client.sendOperationRequest(
      { orgName, options },
      listForOrgOperationSpec
    );
  }

  /**
   * Returns the users associated with the app specified with the given app name which belongs to the
   * given owner.
   * @param ownerName The name of the owner
   * @param appName The name of the application
   * @param options The options parameters.
   */
  list(
    ownerName: string,
    appName: string,
    options?: UsersListOptionalParams
  ): Promise<UsersListResponse> {
    return this.client.sendOperationRequest(
      { ownerName, appName, options },
      listOperationSpec
    );
  }
}
// Operation Specifications
const serializer = coreClient.createSerializer(Mappers, /* isXml */ false);

const getUserMetadataOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/user/metadata/optimizely",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper:
        Mappers.Paths1Bjp26QV01UserMetadataOptimizelyGetResponses200ContentApplicationJsonSchema
    },
    default: {
      bodyMapper:
        Mappers.Paths15Bir6ZV01UserMetadataOptimizelyGetResponsesDefaultContentApplicationJsonSchema
    }
  },
  urlParameters: [Parameters.$host],
  headerParameters: [Parameters.accept],
  serializer
};
const getOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/user",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper:
        Mappers.Paths12V17NbV01UserGetResponses200ContentApplicationJsonSchema
    },
    default: {
      bodyMapper:
        Mappers.Paths484N4EV01UserGetResponsesDefaultContentApplicationJsonSchema
    }
  },
  urlParameters: [Parameters.$host],
  headerParameters: [Parameters.accept],
  serializer
};
const updateOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/user",
  httpMethod: "PATCH",
  responses: {
    200: {
      bodyMapper:
        Mappers.Paths1Stph4IV01UserPatchResponses200ContentApplicationJsonSchema
    },
    default: {
      bodyMapper:
        Mappers.Paths4ScaxfV01UserPatchResponsesDefaultContentApplicationJsonSchema
    }
  },
  requestBody: {
    parameterPath: { displayName: ["options", "displayName"] },
    mapper: {
      ...Mappers.Paths2EstpdV01UserPatchRequestbodyContentApplicationJsonSchema,
      required: true
    }
  },
  urlParameters: [Parameters.$host],
  headerParameters: [Parameters.contentType, Parameters.accept],
  mediaType: "json",
  serializer
};
const updateOrgRoleOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/orgs/{org_name}/users/{user_name}",
  httpMethod: "PATCH",
  responses: {
    200: {
      bodyMapper:
        Mappers.PathsPp9E5UV01OrgsOrgNameUsersUserNamePatchResponses200ContentApplicationJsonSchema
    },
    default: {
      bodyMapper:
        Mappers.PathsAmri8GV01OrgsOrgNameUsersUserNamePatchResponsesDefaultContentApplicationJsonSchema
    }
  },
  requestBody: {
    parameterPath: { role: ["options", "role"] },
    mapper: {
      ...Mappers.PathsP1N751V01OrgsOrgNameUsersUserNamePatchRequestbodyContentApplicationJsonSchema,
      required: true
    }
  },
  urlParameters: [Parameters.$host, Parameters.orgName, Parameters.userName],
  headerParameters: [Parameters.contentType, Parameters.accept],
  mediaType: "json",
  serializer
};
const removeFromOrgOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/orgs/{org_name}/users/{user_name}",
  httpMethod: "DELETE",
  responses: {
    204: {},
    default: {
      bodyMapper:
        Mappers.Paths13YqyzyV01OrgsOrgNameUsersUserNameDeleteResponsesDefaultContentApplicationJsonSchema
    }
  },
  urlParameters: [Parameters.$host, Parameters.orgName, Parameters.userName],
  headerParameters: [Parameters.accept],
  serializer
};
const getForOrgOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/orgs/{org_name}/users/{user_name}",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper:
        Mappers.PathsJys8A7V01OrgsOrgNameUsersUserNameGetResponses200ContentApplicationJsonSchema
    },
    default: {
      bodyMapper:
        Mappers.Paths1H2Meo6V01OrgsOrgNameUsersUserNameGetResponsesDefaultContentApplicationJsonSchema
    }
  },
  urlParameters: [Parameters.$host, Parameters.orgName, Parameters.userName],
  headerParameters: [Parameters.accept],
  serializer
};
const listForOrgOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/orgs/{org_name}/users",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper: {
        type: {
          name: "Sequence",
          element: {
            type: {
              name: "Composite",
              className:
                "PathsCwimmqV01OrgsOrgNameUsersGetResponses200ContentApplicationJsonSchemaItems"
            }
          }
        }
      }
    },
    default: {
      bodyMapper:
        Mappers.Paths1Repp9WV01OrgsOrgNameUsersGetResponsesDefaultContentApplicationJsonSchema
    }
  },
  urlParameters: [Parameters.$host, Parameters.orgName],
  headerParameters: [Parameters.accept],
  serializer
};
const listOperationSpec: coreClient.OperationSpec = {
  path: "/v0.1/apps/{owner_name}/{app_name}/users",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper: {
        type: {
          name: "Sequence",
          element: {
            type: {
              name: "Composite",
              className:
                "PathsMm38I5V01AppsOwnerNameAppNameUsersGetResponses200ContentApplicationJsonSchemaItems"
            }
          }
        }
      }
    },
    default: {
      bodyMapper:
        Mappers.PathsBpjejcV01AppsOwnerNameAppNameUsersGetResponsesDefaultContentApplicationJsonSchema
    }
  },
  urlParameters: [Parameters.$host, Parameters.ownerName, Parameters.appName],
  headerParameters: [Parameters.accept],
  serializer
};
