import { Component, Input, OnInit } from '@angular/core';
import { Dossier, DossierAttributeWithValue, DossierStats } from '@red/domain';
import { DossierTemplatesService } from '@services/dossier-templates/dossier-templates.service';
import { FilesService } from '@services/files/files.service';
import { firstValueFrom, Observable } from 'rxjs';
import { DossierStatsService } from '@services/dossiers/dossier-stats.service';
import { DossiersDialogService } from '../../../shared-dossiers/services/dossiers-dialog.service';
import { getConfig } from '@iqser/common-ui';

@Component({
    selector: 'redaction-dossier-details-stats',
    templateUrl: './dossier-details-stats.component.html',
    styleUrls: ['./dossier-details-stats.component.scss'],
})
export class DossierDetailsStatsComponent implements OnInit {
    @Input() dossierAttributes: DossierAttributeWithValue[];
    @Input() dossier: Dossier;

    attributesExpanded = false;
    dossierTemplateName: string;
    dossierStats$: Observable<DossierStats>;
    readonly isDocumine = getConfig().IS_DOCUMINE;

    constructor(
        private readonly _dossierTemplatesService: DossierTemplatesService,
        private readonly _dialogService: DossiersDialogService,
        private readonly _filesService: FilesService,
        private readonly _dossierStatsService: DossierStatsService,
    ) {}

    ngOnInit() {
        this.dossierStats$ = this._dossierStatsService.watch$(this.dossier.id);
        this.dossierTemplateName = this._dossierTemplatesService.find(this.dossier.dossierTemplateId)?.name || '-';
    }

    openEditDossierDialog(section: string): void {
        const data = { dossierId: this.dossier.id, section };
        this._dialogService.openDialog('editDossier', data, async () => {
            await firstValueFrom(this._filesService.loadAll(this.dossier.id));
        });
    }
}
