const { ObjectId } = require('bson');
const tv4 = require('tv4');
const clone = require('clone');
const Ghs = require('../chemical/ghs');

class SafetyDataSheet {
  constructor(opt) {
    this._id = null;
    this.seriesId = null;
    this.substanceId = null;
    this.name = null;
    this.version = null;
    this.supplierInfo = null;
    this.recommendedUse = null;
    this.restrictions = null;
    this.ghs = [];
    this.revisions = [];
    this.substanceIdentifiers = {
      _id: null,
      name: null,
      vendor: null,
      url: null,
      products: [],
      synonyms: [],
      cas: null,
    };
    this.mixtureIdentifiers = null;
    this.tradeSecret = false;
    this.firstAid = null;
    this.fireFight = null;
    this.accidentalRelease = null;
    this.publicationDate = null;
    this.revisionDate = null;
    this.revisionNote = null;
    this.sdsLink = null;
    this.verified = false;
    this.otherInfo = null;
    this.regulatoryInfo = null;
    this.transportInfo = null;
    this.disposalInfo = null;
    this.ecologicalInfo = null;
    this.toxicologicalInfo = null;
    this.reactivityInfo = null;
    this.handlingInfo = null;
    this.NFPAInfo = null;
    this.HMISInfo = null;

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

  fillObject(opt) {
    this._id = new ObjectId(opt._id);
    this.seriesId = new ObjectId(opt.seriesId);
    this.name = opt.name || null;
    this.substanceId = opt.substanceId || null;
    this.version = opt.version || null;
    this.supplierInfo = opt.supplierInfo || null;
    this.recommendedUse = opt.recommendedUse || null;
    this.restrictions = opt.restrictions || null;
    this.ghs = Array.isArray(opt.ghs) ? opt.ghs.map((item) => new Ghs(item)) : [];
    this.revisions = opt.revisions || [];
    this.substanceIdentifiers = opt.substanceIdentifiers || {
      _id: null,
      name: null,
      vendor: null,
      url: null,
      products: [],
      synonyms: [],
      cas: null,
    };
    this.mixtureIdentifiers = opt.mixtureIdentifiers || null;
    this.tradeSecret = opt.tradeSecret || false;
    this.firstAid = opt.firstAid || null;
    this.fireFight = opt.fireFight || null;
    this.accidentalRelease = opt.accidentalRelease || null;
    this.publicationDate = opt.publicationDate ? new Date(opt.publicationDate) : null;
    this.revisionDate = opt.revisionDate ? new Date(opt.revisionDate) : null;
    this.revisionNote = opt.revisionNote || null;
    this.sdsLink = opt.sdsLink || null;
    this.verified = opt.verified || false;
    this.otherInfo = opt.otherInfo || null;
    this.regulatoryInfo = opt.regulatoryInfo || null;
    this.transportInfo = opt.transportInfo || null;
    this.disposalInfo = opt.disposalInfo || null;
    this.ecologicalInfo = opt.ecologicalInfo || null;
    this.toxicologicalInfo = opt.toxicologicalInfo || null;
    this.reactivityInfo = opt.reactivityInfo || null;
    this.handlingInfo = opt.handlingInfo || null;
    this.NFPAInfo = opt.NFPAInfo || null;
    this.HMISInfo = opt.HMISInfo || null;
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'sds.schema.json',
      type: 'object',
      additionalProperties: false,
      properties: {
        _id: { type: 'object' },
        seriesId: { type: ['object', 'null'] },
        name: { type: 'string' },
        substanceId: { type: ['object', 'null'] },
        version: { type: 'string' },
        supplierInfo: { type: 'object' },
        recommendedUse: { type: ['object', 'null'] },
        restrictions: { type: ['object', 'null'] },
        ghs: { type: 'array', item: { $ref: 'ghs.schema.json' } },
        revisions: { type: 'array', items: { type: 'object' } },
        substanceIdentifiers: { type: 'object' },
        mixtureIdentifiers: { type: 'object' },
        tradeSecret: { type: 'boolean' },
        firstAid: { type: ['object', 'null'] },
        fireFight: { type: ['object', 'null'] },
        accidentalRelease: { type: ['object', 'null'] },
        publicationDate: { type: ['object', 'null'] },
        revisionDate: { type: ['object', 'null'] },
        revisionNote: { type: 'array', items: { type: 'string' } },
        sdsLink: { type: ['string', 'null'] },
        verified: { type: 'boolean' },
        otherInfo: { type: ['object', 'null'] },
        transportInfo: { type: ['object', 'null'] },
        disposalInfo: { type: ['object', 'null'] },
        ecologicalInfo: { type: ['object', 'null'] },
        toxicologicalInfo: { type: ['object', 'null'] },
        reactivityInfo: { type: ['object', 'null'] },
        handlingInfo: { type: ['object', 'null'] },
        regulatoryInfo: { type: 'array', items: { type: 'string' } },
        NFPAInfo: { type: ['object', 'null'] },
        HMISInfo: { type: ['object', 'null'] },
      },
      required: ['_id', 'seriesId'],
    };
  }
}

module.exports = SafetyDataSheet;
