import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { ListingComponent, listingProvidersFactory, LoadingService, TableColumnConfig } from '@iqser/common-ui';
import { SortingOrders } from '@iqser/common-ui/lib/sorting';
import { TrashItem } from '@red/domain';
import { TrashService } from '@services/entity-services/trash.service';
import { RouterHistoryService } from '@services/router-history.service';
import { firstValueFrom, Observable } from 'rxjs';
import { distinctUntilChanged, map } from 'rxjs/operators';
import { TrashDialogService } from '../services/trash-dialog.service';

@Component({
    templateUrl: './trash-screen.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush,
    providers: listingProvidersFactory({
        entitiesService: TrashService,
        component: TrashScreenComponent,
    }),
})
export class TrashScreenComponent extends ListingComponent<TrashItem> implements OnInit {
    readonly tableHeaderLabel = _('trash.table-header.title');
    readonly canRestoreSelected$ = this._canRestoreSelected$;
    readonly canHardDeleteSelected$ = this._canHardDeleteSelected$;
    readonly tableColumnConfigs: TableColumnConfig<TrashItem>[] = [
        { label: _('trash.table-col-names.name'), sortByKey: 'name' },
        { label: _('trash.table-col-names.owner'), class: 'user-column', sortByKey: 'ownerName' },
        { label: _('trash.table-col-names.dossier'), sortByKey: 'fileDossierName' },
        { label: _('trash.table-col-names.deleted-on'), sortByKey: 'softDeletedTime', id: 'deleted-on' },
        { label: _('trash.table-col-names.time-to-restore'), sortByKey: 'restoreDate' },
    ];

    constructor(
        private readonly _loadingService: LoadingService,
        private readonly _trashService: TrashService,
        readonly routerHistoryService: RouterHistoryService,
        private readonly _dialogService: TrashDialogService,
    ) {
        super();

        this.sortingService.setSortingOption({
            column: 'type',
            order: SortingOrders.asc,
        });
    }

    private get _canRestoreSelected$(): Observable<boolean> {
        return this.listingService.selectedEntities$.pipe(
            map(entities => entities.length && !entities.find(dossier => !(dossier.canRestore && dossier.hasRestoreRights))),
            distinctUntilChanged(),
        );
    }

    private get _canHardDeleteSelected$(): Observable<boolean> {
        return this.listingService.selectedEntities$.pipe(
            map(entities => entities.length && !entities.find(dossier => !dossier.hasHardDeleteRights)),
            distinctUntilChanged(),
        );
    }

    ngOnInit() {
        this._loadingService.stop();
    }

    disabledFn = (dossier: TrashItem) => !dossier.canRestore;

    hardDelete(items = this.listingService.selected): void {
        this._dialogService.confirmHardDelete(items);
    }

    async restore(items = this.listingService.selected): Promise<void> {
        this._loadingService.start();
        await firstValueFrom(this._trashService.restore(items));
        this._loadingService.stop();
    }
}
