import { BaseEntity } from '../models/generics/baseentity';
import { EN_CoreEntities } from '../services/globals';

export interface UserRole {
  code: string;
  caption: string;
}


export const enum EN_UserRoles {
  User = 'sDigital Data - IIM-Benutzer',
  Master = 'sDigital Data - IIM-Administratoren',
  Admin = 'sDigital Data - IIM-Administratoren',
  DepartmentMaster = 'departmentmaster',
  DepartmentUser = 'departmentuser'
}


export class CoreUser extends BaseEntity {
  typeName: string = EN_CoreEntities.User;

  webAppUserId: number;
  name: string;
  shortName: string;
  loginName: string;
  password: string;
  roleList: string;
  webAppRoleList: string;
  token?: string;
  jwtExpiredOn: Date;
  lastLogin: string | Date | null;
  clientVersion: string;
  timeZoneOffsetInMin: number;
  language: string;
  culture: string;
  showDisclaimer: boolean;

  get entityId(): number { return this.webAppUserId; }
  set entityId(id: number) { this.webAppUserId = id; }

  get isAdmin(): boolean {
    return this.isInRolle(EN_UserRoles.Admin);
  }

  get isMaster(): boolean {
    return this.isInRolle(EN_UserRoles.Master) || this.isInRolle(EN_UserRoles.DepartmentMaster) || this.isAdmin;
  }

  showFooter: boolean;

  public isInRolle(role: string): boolean {
    role = role?.toLowerCase();
    return this.roleList.toLowerCase().includes(role);
    return (this.roleList && ((',' + this.roleList.replace(' ', '') + ',').toLowerCase().indexOf(',' + role + ',') > -1))
        || (this.webAppRoleList && ((',' + this.webAppRoleList.replace(' ', '') + ',').toLowerCase().indexOf(',' + role + ',') > -1));
  }


  assign(source: Partial<CoreUser>): CoreUser {
    super.assign(source);
    if (this.jwtExpiredOn && typeof this.jwtExpiredOn !== 'object') this.jwtExpiredOn = new Date(this.jwtExpiredOn);
    return this;
  }

  clear() {
    super.clear();
    this.jwtExpiredOn = undefined;
    this.token = undefined;
  }
}


export interface UserRoles {
  admin?: boolean;
  master?: boolean;
}
