import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import {
    IconButtonTypes,
    IqserPermissionsService,
    ListingComponent,
    listingProvidersFactory,
    LoadingService,
    TableColumnConfig,
} from '@iqser/common-ui';
import { getCurrentUser } from '@iqser/common-ui/lib/users';
import { DossierTemplate, User } from '@red/domain';
import { DossierTemplatesService } from '@services/dossier-templates/dossier-templates.service';
import { RouterHistoryService } from '@services/router-history.service';
import { Roles } from '@users/roles';
import { UserPreferenceService } from '@users/user-preference.service';
import { firstValueFrom } from 'rxjs';
import { AdminDialogService } from '../../../services/admin-dialog.service';

@Component({
    templateUrl: './dossier-templates-listing-screen.component.html',
    styleUrls: ['./dossier-templates-listing-screen.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
    providers: listingProvidersFactory<DossierTemplate>({
        entitiesService: DossierTemplatesService,
        component: DossierTemplatesListingScreenComponent,
    }),
})
export class DossierTemplatesListingScreenComponent extends ListingComponent<DossierTemplate> implements OnInit {
    readonly iconButtonTypes = IconButtonTypes;
    readonly roles = Roles;
    readonly currentUser = getCurrentUser<User>();
    readonly tableHeaderLabel = _('dossier-templates-listing.table-header.title');
    readonly tableColumnConfigs: TableColumnConfig<DossierTemplate>[] = [
        { label: _('dossier-templates-listing.table-col-names.name'), sortByKey: 'searchKey', width: '3fr' },
        { label: _('dossier-templates-listing.table-col-names.created-by'), class: 'user-column' },
        { label: _('dossier-templates-listing.table-col-names.created-on'), sortByKey: 'dateAdded' },
        { label: _('dossier-templates-listing.table-col-names.modified-on'), sortByKey: 'dateModified' },
        { label: _('dossier-templates-listing.table-col-names.valid-from') },
        { label: _('dossier-templates-listing.table-col-names.valid-to') },
        { label: _('dossier-templates-listing.table-col-names.status'), sortByKey: 'dossierTemplateStatus' },
    ];

    constructor(
        private readonly _loadingService: LoadingService,
        private readonly _dialogService: AdminDialogService,
        readonly routerHistoryService: RouterHistoryService,
        readonly userPreferenceService: UserPreferenceService,
        readonly permissionsService: IqserPermissionsService,
        private readonly _dossierTemplatesService: DossierTemplatesService,
    ) {
        super();
    }

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

    openBulkDeleteTemplatesDialog() {
        return this._dialogService.openDialog('confirm', null, () => {
            this._loadingService.loadWhile(this._deleteTemplates());
        });
    }

    openAddDossierTemplateDialog() {
        this._dialogService.openDialog('addEditCloneDossierTemplate');
    }

    private async _deleteTemplates(templateIds = this.listingService.selected.map(d => d.dossierTemplateId)) {
        await firstValueFrom(this._dossierTemplatesService.delete(templateIds));
    }
}
