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

class Nfpa {
  constructor(opt) {
    this.flammability = null;
    this.health = null;
    this.reactivity = null;
    this.specialHazard = null;

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

  fillObject(opt) {
    Object.keys(this).forEach((prop) => {
      this[prop] = opt[prop] !== undefined ? opt[prop] : null;
    });
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'nfpa.schema.json',
      type: 'object',
      properties: {
        flammability: { type: ['number', 'null'] },
        health: { type: ['number', 'null'] },
        reactivity: { type: ['number', 'null'] },
        specialHazard: { type: ['string', 'null'] },
      },
      required: Object.keys(this),
    };
  }
}

module.exports = Nfpa;
