import {Injectable} from '@angular/core';
import {ISchema, Resource, Service} from 'ngx-jsonapi';
import {Role, RolesService} from './roles.service';


@Injectable()
export class UsersService extends Service<User> {
  public resource = User;
  public type = 'users';
  public schema: ISchema = {
    relationships: {
      role: {
        hasMany: false
      },
    }
  };
}

export class User extends Resource {
  public static NURSE_TYPE_ID = '2';
  public attributes: {
    name: string,
    email: string,
    password: string,
    mobile: string,
    enable: boolean,
  };

  get enable(): boolean {
    return this.attributes.enable;
  }

  set enable(enable: boolean) {
    this.attributes.enable = enable;
  }

  get name(): string {
    return this.attributes.name;
  }

  set name(name: string) {
    this.attributes.name = name;
  }

  get mobile(): string {
    return this.attributes.mobile;
  }

  set mobile(mobile: string) {
    this.attributes.mobile = mobile;
  }

  get email(): string {
    return this.attributes.email;
  }

  set email(email: string) {
    this.attributes.email = email;
  }

  get password(): string {
    return this.attributes.password;
  }

  set password(password: string) {
    this.attributes.password = password;
  }

  get role(): Role {
    if (Object.keys(this.relationships.role.data).length === 0) {
      const role = (new RolesService()).new();
      this.role = role;
    }
    return <Role>this.relationships.role.data;
  }

  set role(role: Role) {
    this.relationships.role.data = role;
  }

  set roleId(id: string) {
    let roleService = new RolesService();
    let role = roleService.new();
    role.id = id;
    this.role = role;
  }
}
