import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnChanges, Output } from '@angular/core';
import { Dossier, DossierStats, TrashDossier, TrashFile, TrashItem } from '@red/domain';
import { ActiveDossiersService } from '@services/dossiers/active-dossiers.service';
import { DossierStatsService } from '@services/dossiers/dossier-stats.service';
import { Observable } from 'rxjs';

@Component({
    selector: 'redaction-trash-table-item [item]',
    templateUrl: './trash-table-item.component.html',
    styleUrls: ['./trash-table-item.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TrashTableItemComponent implements OnChanges {
    @Input() item: TrashItem;
    @Output() restore = new EventEmitter<TrashItem[]>();
    @Output() hardDelete = new EventEmitter<TrashItem[]>();
    fileDossier$: Observable<Dossier>;
    dossierStats$: Observable<DossierStats>;

    constructor(private readonly _activeDossiersService: ActiveDossiersService, readonly dossierStatsService: DossierStatsService) {}

    file(item: TrashItem): TrashFile {
        return item as TrashFile;
    }

    dossier(item: TrashItem): TrashDossier {
        return item as TrashDossier;
    }

    ngOnChanges(): void {
        if (this.item.isFile) {
            this.fileDossier$ = this._activeDossiersService.getEntityChanged$(this.file(this.item).dossierId);
        }
        if (this.item.isDossier) {
            this.dossierStats$ = this.dossierStatsService.watch$(this.dossier(this.item).id);
        }
    }
}
