import { IListable } from '@iqser/common-ui';
import { List } from '@iqser/common-ui/lib/utils';
import { DownloadFileType } from '../shared';
import { ARCHIVE_ROUTE, DOSSIERS_ROUTE } from './constants';
import { IDossier } from './dossier';

export class Dossier implements IDossier, IListable {
    readonly dossierId: string;
    readonly dossierTemplateId: string;
    readonly ownerId: string;
    readonly memberIds: List;
    readonly approverIds: List;
    readonly reportTemplateIds: List;
    readonly dossierName: string;
    readonly dossierStatusId: string;
    readonly date: string;
    readonly dueDate?: string;
    readonly description?: string;
    readonly downloadFileTypes?: List<DownloadFileType>;
    readonly hardDeletedTime?: string;
    readonly softDeletedTime?: string;
    readonly startDate?: string;
    readonly watermarkId?: number;
    readonly previewWatermarkId?: number;
    readonly archivedTime: string;
    readonly hasApprovers: boolean;
    readonly routerLink: string;
    readonly dossiersListRouterLink: string;
    readonly id: string;
    changedDate?: string;
    readonly isSoftDeleted: boolean;
    readonly isArchived: boolean;
    readonly isActive: boolean;

    constructor(dossier: IDossier) {
        this.dossierId = dossier.dossierId;
        this.approverIds = dossier.approverIds;
        this.date = dossier.date;
        this.description = dossier.description;
        this.dossierName = dossier.dossierName;
        this.dossierStatusId = dossier.dossierStatusId;
        this.dossierTemplateId = dossier.dossierTemplateId;
        this.downloadFileTypes = dossier.downloadFileTypes ?? [];
        this.dueDate = dossier.dueDate;
        this.hardDeletedTime = dossier.hardDeletedTime;
        this.memberIds = dossier.memberIds;
        this.ownerId = dossier.ownerId;
        this.reportTemplateIds = dossier.reportTemplateIds ?? [];
        this.softDeletedTime = dossier.softDeletedTime;
        this.startDate = dossier.startDate;
        this.watermarkId = dossier.watermarkId;
        this.previewWatermarkId = dossier.previewWatermarkId;
        this.archivedTime = dossier.archivedTime;
        this.hasApprovers = !!this.approverIds && this.approverIds.length > 1;

        this.isSoftDeleted = this.softDeletedTime !== null;
        this.isArchived = this.archivedTime !== null;
        this.isActive = !this.isSoftDeleted && !this.isArchived;

        this.id = this.dossierId;
        const routerPath = this.isArchived ? ARCHIVE_ROUTE : DOSSIERS_ROUTE;
        this.dossiersListRouterLink = `/main/${this.dossierTemplateId}/${routerPath}`;
        this.routerLink = `${this.dossiersListRouterLink}/${this.dossierId}`;
    }

    get searchKey(): string {
        return this.dossierName;
    }

    hasMember(memberId: string): boolean {
        return !!this.memberIds && this.memberIds.indexOf(memberId) >= 0;
    }
}
