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

const Card = require('./card');

class BuaAttendanceCard extends Card {
  constructor(opt) {
    super(opt);
    this.type = 'BUA_ATTENDANCE';
    this.details = {
      options: [],
      selectedOptions: [],
    };

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

  fillObject(details) {
    this.details.options = details.options || [];
    this.details.selectedOptions = details.selectedOptions || [];
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'attendance-card.schema.json',
      type: 'object',
      allOf: [
        { $ref: 'card.schema.json' },
        {
          properties: {
            details: {
              options: { type: 'array' },
              selectedOptions: { type: 'array' },
            },
          },
        },
      ],
      required: Object.keys(new BuaAttendanceCard()),
    };
  }
}

tv4.addSchema(Card.schema());

module.exports = BuaAttendanceCard;
