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

@Component({
    templateUrl: './entities-listing-screen.component.html',
    styleUrls: ['./entities-listing-screen.component.scss'],
    providers: listingProvidersFactory(EntitiesListingScreenComponent),
})
export class EntitiesListingScreenComponent extends ListingComponent<Dictionary> {
    readonly #dossierTemplateId = getParam(DOSSIER_TEMPLATE_ID);
    readonly iconButtonTypes = IconButtonTypes;
    readonly circleButtonTypes = CircleButtonTypes;
    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()));
    }

    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);
    }
}
