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

class TextCard extends Card {
  constructor(opt) {
    super(opt);
    this.type = 'TEXT';
    this.details = {
      draftJs: null,
      header: null,
      text: '',
    };

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

  fillObject(details) {
    this.details.draftJs = details.draftJs || this.details.draftJs;
    this.details.header = details.header || null;
    this.details.text = details.text || this.details.text;
    this.isValid = !!this.details.text.length;
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'text-card.schema.json',
      type: 'object',
      allOf: [
        { $ref: 'card.schema.json' },
        {
          properties: {
            details: {
              draftJs: { anyOf: [{ type: 'null' }, { type: 'object' }] },
              header: 'string',
              text: 'string',
            },
          },
        },
      ],
      required: ['details'],
    };
  }
}

tv4.addSchema(Card.schema());

module.exports = TextCard;
