import { IListable } from '@iqser/common-ui';
import { DownloadStatusType, IDownloadStatus } from './download-status';
import { DownloadFileType } from '../shared';
import { List } from '@iqser/common-ui/lib/utils';

export class DownloadStatus implements IDownloadStatus, IListable {
    readonly storageId: string;
    readonly creationDate?: string;
    readonly dossierId?: string;
    readonly downloadFileTypes?: List<DownloadFileType>;
    readonly fileIds?: List;
    readonly fileSize: number;
    readonly filename?: string;
    readonly lastDownload?: string;
    readonly mimeType?: string;
    readonly status?: DownloadStatusType;
    readonly userId?: string;
    readonly size?: string;
    inProgress = false;

    constructor(downloadStatus: IDownloadStatus) {
        this.creationDate = downloadStatus.creationDate;
        this.dossierId = downloadStatus.dossierId;
        this.downloadFileTypes = downloadStatus.downloadFileTypes;
        this.fileIds = downloadStatus.fileIds;
        this.fileSize = downloadStatus.fileSize;
        this.filename = downloadStatus.filename;
        this.lastDownload = downloadStatus.lastDownload;
        this.mimeType = downloadStatus.mimeType;
        this.status = downloadStatus.status;
        this.storageId = downloadStatus.storageId;
        this.userId = downloadStatus.userId;
        this.size = this._size;
    }

    get id(): string {
        return this.storageId;
    }

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

    get isReady(): boolean {
        return this.status === 'READY';
    }

    private get _size() {
        const i = this.fileSize === 0 ? 0 : Math.floor(Math.log(this.fileSize) / Math.log(1000));
        return (this.fileSize / Math.pow(1000, i)).toFixed(2) + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
    }
}
