import { Component, EventEmitter, Input, Output } from '@angular/core';
import {
    DonutChartConfig,
    Dossier,
    DOSSIER_ID,
    DossierAttributeWithValue,
    DossierStats,
    File,
    IDossierRequest,
    ProcessingTypes,
    StatusSorter,
    User,
} from '@red/domain';
import { TranslateChartService } from '@services/translate-chart.service';
import { UserService } from '@users/user.service';
import { getConfig, ProgressBarConfigModel, Toaster } from '@iqser/common-ui';
import { workflowFileStatusTranslations } from '@translations/file-status-translations';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { combineLatestWith, firstValueFrom } from 'rxjs';
import { DossierStatsService } from '@services/dossiers/dossier-stats.service';
import { map, tap } from 'rxjs/operators';
import { DossiersService } from '@services/dossiers/dossiers.service';
import { FilesMapService } from '@services/files/files-map.service';
import { Roles } from '@users/roles';
import { getCurrentUser } from '@iqser/common-ui/lib/users';
import { FilterService, INestedFilter } from '@iqser/common-ui/lib/filtering';
import { ContextComponent, getParam, shareLast } from '@iqser/common-ui/lib/utils';

interface DossierDetailsContext {
    needsWorkFilters: INestedFilter[] | undefined;
    dossier: Dossier;
    dossierStats: DossierStats;
    statusConfig: ProgressBarConfigModel[];
}

@Component({
    selector: 'redaction-dossier-details',
    templateUrl: './dossier-details.component.html',
    styleUrls: ['./dossier-details.component.scss'],
})
export class DossierDetailsComponent extends ContextComponent<DossierDetailsContext> {
    #currentChartSubtitleIndex = 0;
    readonly #dossierId = getParam(DOSSIER_ID);
    @Input() dossierAttributes: DossierAttributeWithValue[];
    @Output() readonly toggleCollapse = new EventEmitter();
    editingOwner = false;
    readonly roles = Roles;
    readonly currentUser = getCurrentUser<User>();
    readonly collapseTooltip = _('dossier-details.collapse');
    readonly expandTooltip = _('dossier-details.expand');
    chartConfig: DonutChartConfig[] = [];
    readonly isDocumine = getConfig().IS_DOCUMINE;

    constructor(
        private readonly _toaster: Toaster,
        private readonly _userService: UserService,
        private readonly _dossiersService: DossiersService,
        private readonly _filesMapService: FilesMapService,
        readonly dossierStatsService: DossierStatsService,
        readonly filterService: FilterService,
        readonly translateChartService: TranslateChartService,
    ) {
        super();
        const dossier$ = _dossiersService.getEntityChanged$(this.#dossierId).pipe(shareLast());
        const filesChanged$ = _filesMapService.watchChanged$(this.#dossierId).pipe(shareLast());
        const dossierStats$ = dossierStatsService.watch$(this.#dossierId).pipe(shareLast());
        const dossierStatsWithEffects$ = dossierStats$.pipe(
            combineLatestWith(filesChanged$),
            tap(([stats]) => this.#calculateChartConfig(stats)),
            map(([stats]) => stats),
        );
        const statusConfig$ = dossierStats$.pipe(map(stats => this.#calculateStatusConfig(stats)));

        super._initContext({
            needsWorkFilters: filterService.getFilterModels$('needsWorkFilters'),
            dossier: dossier$,
            dossierStats: dossierStatsWithEffects$,
            statusConfig: statusConfig$,
        });
    }

    get managers() {
        return this._userService.all.filter(u => u.isManager).map(u => u.id);
    }

    async assignOwner(user: User | string, dossier: Dossier) {
        const owner = typeof user === 'string' ? this._userService.find(user) : user;
        const dossierRequest: IDossierRequest = { ...dossier, ownerId: owner.id };
        await firstValueFrom(this._dossiersService.createOrUpdate(dossierRequest));

        const ownerName = this._userService.getName(owner.id);
        const dossierName = dossier.dossierName;
        this._toaster.success(_('assignment.owner'), { params: { ownerName, dossierName } });
    }

    onSubtitleChanged(subtitleIndex: number) {
        this.#currentChartSubtitleIndex = subtitleIndex;
        this.#calculateChartConfig(this.dossierStatsService.get(this.#dossierId));
    }

    #calculateChartConfig(stats: DossierStats): void {
        const documentsChartConfig: DonutChartConfig[] = Object.keys(stats.fileCountPerWorkflowStatus).map(status => ({
            value: !this.#currentChartSubtitleIndex
                ? stats.fileCountPerWorkflowStatus[status]
                : this.#getPageCountPerWorkflowStatus(status),
            color: status,
            label: workflowFileStatusTranslations[status],
            key: status,
        }));
        documentsChartConfig.sort((a, b) => StatusSorter.byStatus(a.key, b.key));
        this.chartConfig = this.translateChartService.translateLabels(documentsChartConfig);
    }

    #getPageCountPerWorkflowStatus(status: string) {
        const files = this._filesMapService.get(this.#dossierId);
        return files
            .filter((file: File) => file.workflowStatus === status)
            .reduce((sum: number, file: File) => sum + file.numberOfPages, 0);
    }

    #calculateStatusConfig(stats: DossierStats): ProgressBarConfigModel[] {
        return [
            {
                id: ProcessingTypes.pending,
                label: _('processing-status.pending'),
                total: stats.numberOfFiles,
                count: stats.processingStats.pending,
                icon: 'red:reanalyse',
            },
            {
                id: ProcessingTypes.ocr,
                label: _('processing-status.ocr'),
                total: stats.numberOfFiles,
                count: stats.processingStats.ocr,
                icon: 'iqser:ocr',
            },
            {
                id: ProcessingTypes.processing,
                label: _('processing-status.processing'),
                total: stats.numberOfFiles,
                count: stats.processingStats.processing,
                icon: 'red:reanalyse',
            },
            {
                id: ProcessingTypes.processed,
                label: _('processing-status.processed'),
                total: stats.numberOfFiles,
                count: stats.processingStats.processed,
                icon: 'red:ready-for-approval',
            },
        ].filter(config => config.count > 0);
    }
}
