const { ObjectID: ObjectId } = require('bson');
const tv4 = require('tv4');
const Helper = require('../../helper/helper');

class LaundryLocation {
  constructor(opt) {
    this._id = new ObjectId();
    this.barcode = null;
    this.campusCode = null;
    this.tenantCode = null;
    this.buildingName = null;
    this.roomNumber = null;
    this.buildingId = null;
    this.roomId = null;
    this.name = null;
    this.active = true;
    this.notes = '';

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

  fillObject(opt) {
    this._id = new ObjectId(opt._id);
    this.barcode = opt.barcode;
    this.campusCode = opt.campusCode;
    this.tenantCode = opt.tenantCode || Helper.campusCodeToTenantCode(opt.campusCode);
    this.buildingName = opt.buildingName || null;
    this.roomNumber = opt.roomNumber || null;
    this.buildingId = opt.buildingId || null;
    this.roomId = opt.roomId || null;
    this.name = opt.name || null;
    this.active = !!opt.active;
    this.notes = opt.notes || '';
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'laundry.schema.json',
      title: 'laundry location Schema',
      type: 'object',
      additionalProperties: false,
      properties: {
        _id: { type: 'object' },
        barcode: { type: ['string', 'null'] },
        campusCode: { type: 'string' },
        tenantCode: { type: 'string' },
        buildingName: { type: ['string', 'null'] },
        roomNumber: { type: ['string', 'null'] },
        buildingId: { type: ['string', 'null'] },
        roomId: { type: ['string', 'null'] },
        name: { type: 'string' },
        active: { type: 'boolean' },
        notes: { type: ['string', 'null'] },
      },
      required: Object.keys(this),
    };
  }
}

module.exports = LaundryLocation;
