import {orNull, validateBoolean, validateDate, validateString} from '../validators';

export class Picture {

  static trackBy(_: number, picture: Picture) {
    return picture.uuid;
  }

  constructor(
    readonly uuid: string,
    readonly originalDate: Date | null,
    readonly uploadDate: Date,
    readonly visible: boolean,
  ) {
    //
  }

  static fromJson(json: any): Picture {
    return new Picture(
      validateString(json.uuid),
      orNull(json.originalDate, validateDate),
      validateDate(json.uploadDate),
      validateBoolean(json.visible),
    );
  }

  static byUploadDateDesc(a: Picture, b: Picture): number {
    return b.uploadDate.getTime() - a.uploadDate.getTime();
  }

  static byOriginalDateDesc(a: Picture, b: Picture): number {
    const aa = a.originalDate === null ? a.uploadDate.getTime() : a.originalDate.getTime();
    const bb = b.originalDate === null ? b.uploadDate.getTime() : b.originalDate.getTime();
    return bb - aa;
  }

}

