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



@Injectable()
export class MajoreService extends Service<Majore> {
  public resource = Majore;
  public type = 'majors';
  public schema: ISchema = {
    relationships: {
      unit: {
        hasMany: false
      }
    }
  };
}


export class Majore extends Resource {
  public attributes: {
    name: string;
  };


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

  set name(name: string) {
    this.attributes.name = name;
  }
  get unit(): Units {
    if (Object.keys(this.relationships.role.data).length === 0) {
      const role = (new UnitsService()).new();
      this.unit = role;
    }
    return <Units>this.relationships.unit.data;
  }

  set unit(units: Units) {
    this.relationships.unit.data = units;
  }

  set unitId(id: string) {
    let unitService = new UnitsService();
    let unit = unitService.new();
    unit.id = id;
    this.unit = unit;
  }
}
