import _ from 'lodash';
import {IdModel} from './id.model';
import {MaterialModel} from './material.model';
import {MeasurementModel} from './measurement.model';
import {BaseModel} from './base.model';

export class SampleModel extends BaseModel {
  _id: IdModel = null;
  color = '';
  number = '';
  type = '';
  batch = '';
  condition: {condition_template: string, [prop: string]: string} | {} = {};
  material_id: IdModel = null;
  material: MaterialModel;
  measurements: MeasurementModel[] = [];
  note_id: IdModel = null;
  user_id: IdModel = null;
  notes: {comment: string, sample_references: {sample_id: IdModel, relation: string}[], custom_fields: {[prop: string]: string}} = {comment: '', sample_references: [], custom_fields: {}};
  added: Date = null;

  deserialize(input: any): this {
    Object.assign(this, input);
    if (input.hasOwnProperty('material')) {
      this.material = new MaterialModel().deserialize(input.material);
      this.material_id = input.material._id;
    }
    if (input.hasOwnProperty('measurements')) {
      this.measurements = input.measurements.map(e => new MeasurementModel().deserialize(e));
    }
    if (input.hasOwnProperty('added')) {
      this.added = new Date(input.added);
    }
    return this;
  }

  sendFormat() {
    return _.pick(this, ['color', 'type', 'batch', 'condition', 'material_id', 'notes']);
  }
}