import { IListable } from '@iqser/common-ui';
import { CountByStatus, CountPerProcessingStatus, CountPerWorkflowStatus, IDashboardStats } from './dashboard-stats';
import { DonutChartConfig, StatusSorter } from '../shared';
import { DossierTemplateStatus, DossierTemplateStatuses } from './constants';
import { List } from '@iqser/common-ui/lib/utils';

export class DashboardStats implements IListable, IDashboardStats {
    readonly dossierCountByStatus: List<CountByStatus>;
    readonly dossierTemplateId: string;
    readonly dossiersInTemplate: List<string>;
    readonly fileCountPerProcessingStatus: List<CountPerProcessingStatus>;
    readonly fileCountPerWorkflowStatus: List<CountPerWorkflowStatus>;
    readonly name: string;
    readonly numberOfActiveDossiers: number;
    readonly numberOfActiveFiles: number;
    readonly numberOfArchivedDossiers: number;
    readonly numberOfDeletedDossiers: number;
    readonly numberOfExcludedPages: number;
    readonly numberOfPages: number;
    readonly numberOfPeople: number;
    readonly numberOfSoftDeletedFiles: number;
    readonly dossiersChartConfig: DonutChartConfig[];
    readonly documentsChartConfig: DonutChartConfig[];
    readonly dossierTemplateStatus: DossierTemplateStatus;

    constructor(stats: IDashboardStats) {
        this.dossierCountByStatus = stats.dossierCountByStatus;
        this.dossierTemplateId = stats.dossierTemplateId;
        this.dossiersInTemplate = stats.dossiersInTemplate;
        this.fileCountPerProcessingStatus = stats.fileCountPerProcessingStatus;
        this.fileCountPerWorkflowStatus = stats.fileCountPerWorkflowStatus;
        this.name = stats.name;
        this.numberOfActiveDossiers = stats.numberOfActiveDossiers;
        this.numberOfActiveFiles = stats.numberOfActiveFiles;
        this.numberOfArchivedDossiers = stats.numberOfArchivedDossiers;
        this.numberOfDeletedDossiers = stats.numberOfDeletedDossiers;
        this.numberOfExcludedPages = stats.numberOfExcludedPages;
        this.numberOfPages = stats.numberOfPages;
        this.numberOfPeople = stats.numberOfPeople;
        this.numberOfSoftDeletedFiles = stats.numberOfSoftDeletedFiles;
        this.dossierTemplateStatus = stats.dossierTemplateStatus;

        this.dossiersChartConfig = this.#dossiersChartConfig;
        this.documentsChartConfig = this.#documentsChartConfig;
    }

    get isEmpty(): boolean {
        return this.numberOfActiveDossiers === 0 && this.numberOfArchivedDossiers === 0;
    }

    get isActive(): boolean {
        return this.dossierTemplateStatus === DossierTemplateStatuses.ACTIVE;
    }

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

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

    get #dossiersChartConfig(): DonutChartConfig[] {
        return this.dossierCountByStatus.map((d, index) => ({
            id: `dossier-chart-filter-${index}`,
            value: d.count,
            color: '#e2e4e9',
            label: 'dossier-state.placeholder',
            key: d.statusId ?? 'undefined',
        }));
    }

    get #documentsChartConfig(): DonutChartConfig[] {
        const configArray: DonutChartConfig[] = this.fileCountPerWorkflowStatus.map((d, index) => ({
            id: `document-chart-filter-${index}`,
            value: d.count,
            color: d.workflowStatus,
            label: d.workflowStatus,
            key: d.workflowStatus,
        }));

        return configArray.sort((a, b) => StatusSorter.byStatus(a.label, b.label));
    }
}
