import { BaseResponse } from "../../../../models/response/base.response";
import { KdfType } from "../../../../platform/enums";
import { OrganizationUserStatusType, OrganizationUserType } from "../../../enums";
import { PermissionsApi } from "../../../models/api/permissions.api";
import { SelectionReadOnlyResponse } from "../../../models/response/selection-read-only.response";

export class OrganizationUserResponse extends BaseResponse {
  id: string;
  userId: string;
  type: OrganizationUserType;
  status: OrganizationUserStatusType;
  externalId: string;
  /**
   * @deprecated
   * To be removed after Flexible Collections.
   **/
  accessAll: boolean;
  accessSecretsManager: boolean;
  permissions: PermissionsApi;
  resetPasswordEnrolled: boolean;
  hasMasterPassword: boolean;
  collections: SelectionReadOnlyResponse[] = [];
  groups: string[] = [];

  constructor(response: any) {
    super(response);
    this.id = this.getResponseProperty("Id");
    this.userId = this.getResponseProperty("UserId");
    this.type = this.getResponseProperty("Type");
    this.status = this.getResponseProperty("Status");
    this.permissions = new PermissionsApi(this.getResponseProperty("Permissions"));
    this.externalId = this.getResponseProperty("ExternalId");
    this.accessAll = this.getResponseProperty("AccessAll");
    this.accessSecretsManager = this.getResponseProperty("AccessSecretsManager");
    this.resetPasswordEnrolled = this.getResponseProperty("ResetPasswordEnrolled");
    this.hasMasterPassword = this.getResponseProperty("HasMasterPassword");

    const collections = this.getResponseProperty("Collections");
    if (collections != null) {
      this.collections = collections.map((c: any) => new SelectionReadOnlyResponse(c));
    }
    const groups = this.getResponseProperty("Groups");
    if (groups != null) {
      this.groups = groups;
    }
  }
}

export class OrganizationUserUserDetailsResponse extends OrganizationUserResponse {
  name: string;
  email: string;
  avatarColor: string;
  twoFactorEnabled: boolean;
  usesKeyConnector: boolean;

  constructor(response: any) {
    super(response);
    this.name = this.getResponseProperty("Name");
    this.email = this.getResponseProperty("Email");
    this.avatarColor = this.getResponseProperty("AvatarColor");
    this.twoFactorEnabled = this.getResponseProperty("TwoFactorEnabled");
    this.usesKeyConnector = this.getResponseProperty("UsesKeyConnector") ?? false;
  }
}

export class OrganizationUserDetailsResponse extends OrganizationUserResponse {
  constructor(response: any) {
    super(response);
  }
}

export class OrganizationUserResetPasswordDetailsResponse extends BaseResponse {
  kdf: KdfType;
  kdfIterations: number;
  kdfMemory?: number;
  kdfParallelism?: number;
  resetPasswordKey: string;
  encryptedPrivateKey: string;

  constructor(response: any) {
    super(response);
    this.kdf = this.getResponseProperty("Kdf");
    this.kdfIterations = this.getResponseProperty("KdfIterations");
    this.kdfMemory = this.getResponseProperty("KdfMemory");
    this.kdfParallelism = this.getResponseProperty("KdfParallelism");
    this.resetPasswordKey = this.getResponseProperty("ResetPasswordKey");
    this.encryptedPrivateKey = this.getResponseProperty("EncryptedPrivateKey");
  }
}
