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

const Card = require('./card');
const Band = require('../chemical/band');

class ChemicalBandCard extends Card {
  constructor(opt) {
    super(opt);
    this.type = 'CHEMICAL_BAND';
    this.details = {
      notes: null,
      bands: [],
    };

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

  fillObject(details) {
    this.details.bands = details.bands ? details.bands.map((opt) => new Band(opt)) : [];
    this.details.notes = details.notes || null;
    this.isValid = !!this.details.bands.length;
  }

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

  getLabel() {
    return this.details.bands.map((b) => b._id);
  }

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'chemical-band-card.schema.json',
      type: 'object',
      allOf: [
        { $ref: 'card.schema.json' },
        {
          properties: {
            details: {
              bands: { type: 'array', items: { $ref: 'band.schema.json' } },
              notes: { type: ['string', 'null'] },
            },
          },
        },
      ],
      required: Object.keys(new ChemicalBandCard()),
    };
  }
}

tv4.addSchema(Card.schema());

module.exports = ChemicalBandCard;
