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

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

class LotoElectricalCard extends Card {
  constructor(opt) {
    super(opt);
    this.type = 'LOTO_ELECTRICAL';
    this.details = {
      amps: null,
      volts: null,
      phase: null,
      currentType: 'AC',
    };

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

  fillObject(details) {
    this.details.amps = details.amps || null;
    this.details.volts = details.volts || null;
    this.details.phase = details.phase || null;
    this.details.currentType = details.currentType || 'AC';
    this.isValid = !!(this.details.amps && this.details.volts && this.details.phase);
  }

  validate() {
    return tv4.validateMultiple(this, LotoElectricalCard.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: {
              amps: { type: 'number' },
              currentType: { type: 'string' },
              phase: { type: ['number', 'null'] },
              volts: { type: 'number' },
            },
          },
        },
      ],
      required: Object.keys(this),
    };
  }
}

tv4.addSchema(Card.schema());

module.exports = LotoElectricalCard;
