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

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

class AttendanceCard extends Card {
  constructor(opt) {
    super(opt);
    this.type = '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, AttendanceCard.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(this),
    };
  }
}

tv4.addSchema(Card.schema());

module.exports = AttendanceCard;
