import { Injectable } from '@angular/core';
import { TableColumnConfig } from '@iqser/common-ui';
import { Dossier, User } from '@red/domain';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { dossierMemberChecker, dossierMemberQuickChecker, dossierOwnerQuickChecker, dossierTemplateChecker } from '@utils/index';
import { UserService } from '@users/user.service';
import { TranslateService } from '@ngx-translate/core';
import { DossierTemplatesService } from '@services/dossier-templates/dossier-templates.service';
import { IFilterGroup, keyChecker, NestedFilter } from '@iqser/common-ui/lib/filtering';

@Injectable()
export class ConfigService {
    constructor(
        private readonly _userService: UserService,
        private readonly _translateService: TranslateService,
        private readonly _dossierTemplatesService: DossierTemplatesService,
    ) {}

    get tableConfig(): TableColumnConfig<Dossier>[] {
        return [
            { label: _('archived-dossiers-listing.table-col-names.name'), sortByKey: 'searchKey', width: '2fr' },
            { label: _('archived-dossiers-listing.table-col-names.last-modified'), sortByKey: 'archivedTime' },
            { label: _('archived-dossiers-listing.table-col-names.owner'), class: 'user-column' },
            { label: _('archived-dossiers-listing.table-col-names.dossier-state'), class: 'flex-end', width: '2fr' },
        ];
    }

    get _currentUser(): User {
        return this._userService.currentUser;
    }

    filterGroups(entities: Dossier[]) {
        const allDistinctPeople = new Set<string>();
        const allDistinctDossierTemplates = new Set<string>();

        const filterGroups: IFilterGroup[] = [];

        entities?.forEach(entry => {
            entry.memberIds.forEach(f => allDistinctPeople.add(f));
            allDistinctDossierTemplates.add(entry.dossierTemplateId);
        });

        const peopleFilters = [...allDistinctPeople].map(
            userId =>
                new NestedFilter({
                    id: userId,
                    label: this._userService.getName(userId),
                }),
        );

        filterGroups.push({
            slug: 'peopleFilters',
            label: this._translateService.instant('filters.people'),
            icon: 'red:user',
            filters: peopleFilters,
            checker: dossierMemberChecker,
        });

        const dossierTemplateFilters = [...allDistinctDossierTemplates].map(
            id =>
                new NestedFilter({
                    id: id,
                    label: this._dossierTemplatesService.find(id)?.name || '-',
                }),
        );

        filterGroups.push({
            slug: 'dossierTemplateFilters',
            label: this._translateService.instant('filters.dossier-templates'),
            icon: 'red:template',
            hide: dossierTemplateFilters.length <= 1,
            filters: dossierTemplateFilters,
            checker: dossierTemplateChecker,
        });

        filterGroups.push({
            slug: 'quickFilters',
            filters: this._quickFilters(entities),
            checker: (dw: Dossier, filter: NestedFilter) => filter.checked && filter.checker(dw),
        });

        const dossierFilters = entities.map(
            dossier =>
                new NestedFilter({
                    id: dossier.dossierName,
                    label: dossier.dossierName,
                }),
        );
        filterGroups.push({
            slug: 'dossierNameFilter',
            label: this._translateService.instant('dossier-listing.filters.label'),
            icon: 'red:folder',
            filters: dossierFilters,
            filterceptionPlaceholder: this._translateService.instant('dossier-listing.filters.search'),
            checker: keyChecker('dossierName'),
        });

        return filterGroups;
    }

    private _quickFilters(entities: Dossier[]): NestedFilter[] {
        const userId = this._currentUser.id;
        return [
            {
                id: 'owner',
                label: this._translateService.instant('dossier-listing.quick-filters.owner'),
                checker: dossierOwnerQuickChecker(userId),
                disabled: entities.filter(dossierOwnerQuickChecker(userId)).length === 0,
                helpModeKey: 'quick_filter_dossiers',
            },
            {
                id: 'member',
                label: this._translateService.instant('dossier-listing.quick-filters.member'),
                checker: dossierMemberQuickChecker(userId),
                disabled: entities.filter(dossierMemberQuickChecker(userId)).length === 0,
                helpModeKey: 'quick_filter_dossiers',
            },
        ].map(filter => new NestedFilter(filter));
    }
}
