import { EN_AppEntities } from '@app_consts';
import { AppBaseEntity } from './generic/app.baseentity';
import { prepareArrayFromList, prepareListFromArray } from '@app_core/utils';

export class EmployeeToWebApp extends AppBaseEntity {
  typeName = EN_AppEntities.EmployeeToWebapp;
  public get entitytitle(): string { return 'Zuweisung von Webapp "' + this.webAppName + '" für Mitarbeiter'; } //is used for messages
  employeeToWebAppId: number;
  employeeId: number;
  webAppId: number;
  webAppRoleId: number;
  departmentId: number;
  webAppRoleName: string;
  webAppName: string;
  departmentName: string;
  extendedDepartmentNameList: string;
  extendedDepartmentIdList: string;

  additionalRoleNameList: string;
  additionalRoleIdList: string;

  private _arExtendedDepartmentIdList: number[];
  get arExtendedDepartmentIdList(): number[] {
    return prepareArrayFromList(this, 'extendedDepartmentIdList', '_arExtendedDepartmentIdList');
  }

  set arExtendedDepartmentIdList(array: number[]) {
    prepareListFromArray(this, 'extendedDepartmentIdList', '_arExtendedDepartmentIdList', array);
  }

  private _arAdditionalRoleIdList: number[];
  get arAdditionalRoleIdList(): number[] {
    return prepareArrayFromList(this, 'additionalRoleIdList', '_arAdditionalRoleIdList');
  }

  set arAdditionalRoleIdList(array: number[]) {
    prepareListFromArray(this, 'additionalRoleIdList', '_arAdditionalRoleIdList', array);
  }


  public get entityId() {
    return this.employeeToWebAppId;
  }
  public set entityId(value) {
    this.employeeToWebAppId = value;
  }

  clear() {
    super.clear();
    this._arExtendedDepartmentIdList = undefined;
    this._arAdditionalRoleIdList = undefined;
  }

  public assign(source: Partial<EmployeeToWebApp>): EmployeeToWebApp {
    const oldId = this.entityId;
    const oldDepIdList = this.extendedDepartmentIdList;
    const oldAddRoleIdList = this.additionalRoleIdList;
    this.clear();
    const obj = Object.assign(this, source);
    if (oldId <= 0 && !this.isNew()) {//don't update extendedDepartmentIdList and additionalRoleIdList if the entity is reload (f.e. after insert)
      this.extendedDepartmentIdList = oldDepIdList;
      this.additionalRoleIdList = oldAddRoleIdList;
    }

    return obj;
  }
}
