import {JsonObject, JsonProperty} from "json2typescript";

import {Message} from "../../../entity/message";
import {TimestampDateConverter} from "../parser/converters/timestamp-date.converter";
import {AttachmentSerialized} from "./attachment.serialized";

@JsonObject('MessageSerialized')
export class MessageSerialized {

  @JsonProperty('id', Number)
  id: number = undefined;

  @JsonProperty('body', String)
  body: string = undefined;

  @JsonProperty('sentBy', String)
  sentBy: string = undefined;

  @JsonProperty('createdOn', TimestampDateConverter)
  createdOn: Date = undefined;

  @JsonProperty('updatedOn', TimestampDateConverter)
  updatedOn: Date = undefined;

  @JsonProperty('attachments', [AttachmentSerialized])
  attachments: AttachmentSerialized[];

  public static async createFromObj(message: Message): Promise<MessageSerialized> {
    const inst = new MessageSerialized();
    inst.id = message.id;
    inst.body = message.body;
    inst.sentBy = message.sentBy;
    inst.createdOn = message.createdOn;
    inst.updatedOn = message.updatedOn;
    inst.attachments = (await message.attachments).map(a => AttachmentSerialized.createFromObj(a));
    console.log('a', inst);
    return inst;
  }

}
