const ObjectId = require('bson').ObjectID;
const clone = require('clone');
const tv4 = require('tv4');

const TemplateSection = require('./template-section');

const TEMPLATE_STATUS = require('../../constant/procedure/template-status.constant');
const TEMPLATE_TYPES = require('../../constant/procedure/template-type.constant').keys;

class Template {
  constructor(opt) {
    this._id = new ObjectId();
    this.acknowledgement = {
      showAcknowledgement: false,
      acknowledgeLabel: '',
      prompt: '',
    };
    this.name = null;
    this.types = [];
    this.status = TEMPLATE_STATUS.DRAFT;
    this.sectionDefinitions = [];
    this.maxExpirationDays = null;
    this.tenantCode = [];
    this.campusCode = [];
    this.preventClone = false;
    this.preventRemove = false;
    this.preventShare = false;
    this.preventEditTitle = false;
    this.createBySystem = false;
    this.ruleSet = '';

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

  fillObject(opt) {
    this._id = new ObjectId(opt._id);
    if (opt.acknowledgement) {
      this.acknowledgement.showAcknowledgement =
        opt.acknowledgement.showAcknowledgement || this.acknowledgement.showAcknowledgement;
      this.acknowledgement.acknowledgeLabel =
        opt.acknowledgement.acknowledgeLabel || this.acknowledgement.acknowledgeLabel;
      this.acknowledgement.prompt = opt.acknowledgement.prompt || this.acknowledgement.prompt;
    }
    this.name = opt.name;
    this.types = opt.types || [];
    this.status = opt.status ? opt.status : TEMPLATE_STATUS.DRAFT;
    this.maxExpirationDays = opt.maxExpirationDays ? opt.maxExpirationDays : null;
    this.tenantCode = opt.tenantCode || [];
    this.campusCode = opt.campusCode || [];
    this.preventClone = opt.preventClone || false;
    this.preventRemove = opt.preventRemove || false;
    this.preventShare = opt.preventShare || false;
    this.preventEditTitle = opt.preventEditTitle || false;
    this.createBySystem = opt.createBySystem || false;
    this.ruleSet = opt.ruleSet || 'procedures/default.rules';

    if (opt.sectionDefinitions) {
      opt.sectionDefinitions.forEach((definition) => this.sectionDefinitions.push(new TemplateSection(definition)));
    }
  }

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

  static schema() {
    return {
      $schema: 'http://json-schema.org/draft-04/schema#',
      id: 'template.schema.json',
      type: 'object',
      additionalProperties: false,
      properties: {
        _id: { type: 'object' },
        acknowledgement: {
          type: 'object',
          properties: {
            acknowledgeLabel: { type: ['null', 'string'] },
            prompt: { type: ['null', 'string'] },
            showAcknowledgement: { type: 'boolean' },
          },
        },
        name: { type: 'string' },
        types: { type: 'array', items: { type: 'string', enum: TEMPLATE_TYPES } },
        status: { type: 'string', enum: Object.keys(TEMPLATE_STATUS) },
        maxExpirationDays: { type: ['number', 'null'] },
        sectionDefinitions: { type: 'array', item: { $ref: 'template-section.schema.json' } },
        tenantCode: { type: 'array', items: { type: 'string' } },
        campusCode: { type: 'array', items: { type: 'string' } },
        preventClone: { type: 'boolean' },
        preventRemove: { type: 'boolean' },
        preventShare: { type: 'boolean' },
        preventEditTitle: { type: 'boolean' },
        createBySystem: { type: 'boolean' },
        ruleSet: { type: 'string' },
      },
      required: Object.keys(this),
    };
  }

  static clone(template, properties) {
    const copy = {
      ...template,
      name: `COPY of ${template.name}`,
      status: TEMPLATE_STATUS.DRAFT,
      ...properties,
    };
    delete copy._id;

    return new this(copy);
  }
}

tv4.addSchema(TemplateSection.schema());

module.exports = Template;
