import { Component, OnInit } from '@angular/core';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { IconButtonTypes, ListingComponent, listingProvidersFactory, LoadingService, TableColumnConfig } from '@iqser/common-ui';
import { getParam } from '@iqser/common-ui/lib/utils';
import { Dictionary, DOSSIER_TEMPLATE_ID, DossierTemplateStats } from '@red/domain';
import { DictionariesMapService } from '@services/entity-services/dictionaries-map.service';
import { DictionaryService } from '@services/entity-services/dictionary.service';
import { DossierTemplateStatsService } from '@services/entity-services/dossier-template-stats.service';
import { PermissionsService } from '@services/permissions.service';
import { firstValueFrom, Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { AdminDialogService } from '../../services/admin-dialog.service';

@Component({
    templateUrl: './entities-listing-screen.component.html',
    styleUrls: ['./entities-listing-screen.component.scss'],
    providers: listingProvidersFactory(EntitiesListingScreenComponent),
})
export class EntitiesListingScreenComponent extends ListingComponent<Dictionary> implements OnInit {
    readonly #dossierTemplateId = getParam(DOSSIER_TEMPLATE_ID);
    readonly iconButtonTypes = IconButtonTypes;
    readonly tableHeaderLabel = _('entities-listing.table-header.title');
    readonly tableColumnConfigs: TableColumnConfig<Dictionary>[] = [
        { label: _('entities-listing.table-col-names.type'), sortByKey: 'searchKey', width: '2fr' },
        { label: _('entities-listing.table-col-names.rank'), sortByKey: 'rank', class: 'flex-center' },
        { label: _('entities-listing.table-col-names.hint-redaction'), class: 'flex-center' },
        { label: _('entities-listing.table-col-names.dictionary-entries') },
    ];
    readonly templateStats$: Observable<DossierTemplateStats>;

    constructor(
        private readonly _loadingService: LoadingService,
        private readonly _dictionariesMapService: DictionariesMapService,
        private readonly _dialogService: AdminDialogService,
        private readonly _dictionaryService: DictionaryService,
        private readonly _dossierTemplateStatsService: DossierTemplateStatsService,
        readonly permissionsService: PermissionsService,
    ) {
        super();
        this.templateStats$ = this._dossierTemplateStatsService.watch$(this.#dossierTemplateId).pipe(tap(() => this._loadDictionaryData()));
    }

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

    openDeleteEntitiesDialog(types = this.listingService.selected) {
        this._dialogService.openDialog('confirm', null, async () => {
            this._loadingService.start();
            await firstValueFrom(
                this._dictionaryService.deleteDictionaries(
                    types.map(t => t.type),
                    this.#dossierTemplateId,
                ),
            );
            this._loadDictionaryData();
            this._loadingService.stop();
        });
    }

    openAddEntityDialog() {
        this._dialogService.openDialog('addEntity', { dossierTemplateId: this.#dossierTemplateId });
    }

    private _loadDictionaryData(): void {
        const entities: Dictionary[] = this._dictionariesMapService.get(this.#dossierTemplateId).filter(d => !d.virtual);
        this.entitiesService.setEntities(entities);
    }
}
