const tv4 = require('tv4');

class Location {
  constructor(opt) {
    this.buildingId = '';
    this.floorId = '';
    this.roomId = '';
    this.sublocationId = '';

    this.buildingName = '';
    this.floorName = '';
    this.roomNumber = '';
    this.sublocationName = '';

    if (opt) {
      this.fillObject(opt);
    }
  }

  fillObject(opt) {
    this.buildingId = opt.buildingId || '';
    this.floorId = opt.floorId || '';
    this.roomId = opt.roomId || '';
    this.sublocationId = opt.sublocationId || '';

    this.buildingName = opt.buildingName || '';
    this.floorName = opt.floorName || '';
    this.roomNumber = opt.roomNumber || '';
    this.sublocationName = opt.sublocationName || '';
  }

  validate() {
    return tv4.validateMultiple(this, Location.schema());
  }

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'location.schema.json',
      type: 'object',
      properties: {
        buildingId: { type: 'string' },
        floorId: { type: 'string' },
        roomId: { type: 'string' },
        sublocationId: { type: ['string', 'null'] },
        buildingName: { type: 'string' },
        floorName: { type: 'string' },
        roomNumber: { type: 'string' },
        sublocationName: { type: ['string', 'null'] },
      },
      required: [
        'buildingId',
        'floorId',
        'roomId',
        'sublocationId',
        'buildingName',
        'floorName',
        'roomNumber',
        'sublocationName',
      ],
    };
  }
}

module.exports = Location;
