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

const SEVERITY = require('../../constant/reminder/severity.constant');

class Todo {
  constructor(opt) {
    this.title = null;
    this.description = null;
    this.severity = null;
    this.link = null;
    this.sendDate = null;
    if (opt && opt.details) {
      this.fillObject(clone(opt.details));
    }
  }

  fillObject(details) {
    this.title = details.title;
    this.description = details.description || null;
    this.severity = details.severity || SEVERITY.LOW;
    this.link = details.link;
    this.sendDate = details.sendDate ? new Date(details.sendDate) : null;
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'todo.schema.json',
      type: 'object',
      properties: {
        title: { type: 'string' },
        description: { type: ['string', 'null'] },
        severity: { enum: Object.keys(SEVERITY) },
        link: { type: 'string' },
        sendDate: { type: ['object', 'null'] },
      },
      required: ['title', 'link', 'description', 'severity'],
    };
  }
}

module.exports = Todo;
