const clone = require('clone');
const tv4 = require('tv4');

class ControlAreaBuilding {
  constructor(opt) {
    this._id = null;
    this.name = null;
    this.campusCode = null;
    this.tenantCode = null;
    this.address = null;
    this.codeYear = null;
    this.fireSuppressionCoverage = false;
    this.floors = [];

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

  fillObject(opt) {
    this._id = opt._id || null;
    this.name = opt.name || null;
    this.campusCode = opt.campusCode || null;
    this.tenantCode = opt.tenantCode || null;
    this.address = opt.address || null;
    this.codeYear = opt.codeYear || null;
    this.fireSuppressionCoverage = !!opt.fireSuppressionCoverage || false;
    this.floors = opt.floors || [];
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'control-area-building.schema.json',
      type: 'object',
      additionalProperties: false,
      properties: {
        _id: { type: 'string' },
        name: { type: 'string' },
        campusCode: { type: 'string' },
        tenantCode: { type: 'string' },
        address: { type: 'string' },
        codeYear: { type: 'number' },
        fireSuppressionCoverage: { type: 'boolean' },
        floors: {
          type: 'array',
          item: {
            type: 'object',
            properties: {
              floorId: { type: ['null', 'string'] },
              floorName: { type: ['null', 'string'] },
              level: { type: ['null', 'number'] },
            },
          },
        },
      },
      required: Object.keys(this),
    };
  }
}

module.exports = ControlAreaBuilding;
