import { AsyncPipe, NgForOf, NgIf } from '@angular/common';
import { Component, EventEmitter, Input, OnChanges, Output, SimpleChanges } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatFormField } from '@angular/material/form-field';
import { MatMenu, MatMenuTrigger } from '@angular/material/menu';
import { MatOption, MatSelect, MatSelectTrigger } from '@angular/material/select';
import { MatSlideToggle } from '@angular/material/slide-toggle';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { ChevronButtonComponent } from '@common-ui/buttons/chevron-button';
import { EditableInputComponent } from '@common-ui/inputs/editable-input/editable-input.component';
import { RoundCheckboxComponent } from '@common-ui/inputs/round-checkbox/round-checkbox.component';
import { CircleButtonComponent, IqserListingModule, ListingComponent, listingProvidersFactory, TableColumnConfig } from '@iqser/common-ui';
import { TranslateModule } from '@ngx-translate/core';
import { FileAttributeConfigTypes, IField } from '@red/domain';
import { fileAttributeTypesTranslations } from '@translations/file-attribute-types-translations';

@Component({
    selector: 'redaction-active-fields-listing',
    templateUrl: './active-fields-listing.component.html',
    styleUrls: ['./active-fields-listing.component.scss'],
    providers: listingProvidersFactory(ActiveFieldsListingComponent),
    standalone: true,
    imports: [
        IqserListingModule,
        CircleButtonComponent,
        MatMenuTrigger,
        TranslateModule,
        AsyncPipe,
        ChevronButtonComponent,
        MatMenu,
        EditableInputComponent,
        MatFormField,
        MatSelectTrigger,
        MatSelect,
        MatOption,
        FormsModule,
        MatSlideToggle,
        RoundCheckboxComponent,
        NgForOf,
        NgIf,
    ],
})
export class ActiveFieldsListingComponent extends ListingComponent<IField> implements OnChanges {
    readonly translations = fileAttributeTypesTranslations;
    readonly tableHeaderLabel = _('file-attributes-csv-import.table-header.title');
    readonly tableColumnConfigs: TableColumnConfig<IField>[] = [
        {
            label: _('file-attributes-csv-import.table-col-names.name'),
            class: 'name',
            width: 'minmax(0, 350px)',
        },
        {
            label: _('file-attributes-csv-import.table-col-names.type'),
            width: '150px',
        },
        {
            label: _('file-attributes-csv-import.table-col-names.read-only'),
            class: 'flex-center',
            leftIcon: 'red:read-only',
            width: 'auto',
        },
        {
            label: _('file-attributes-csv-import.table-col-names.primary'),
            class: 'flex-center',
            rightIcon: 'red:status-info',
            rightIconTooltip: _('file-attributes-csv-import.table-col-names.primary-info-tooltip'),
            width: 'auto',
        },
    ];
    readonly typeOptions = Object.keys(FileAttributeConfigTypes);
    @Input() entities: IField[];
    @Output() readonly entitiesChange = new EventEmitter<IField[]>();
    @Output() readonly setHoveredColumn = new EventEmitter<string>();
    @Output() readonly toggleFieldActive = new EventEmitter<IField>();

    ngOnChanges(changes: SimpleChanges): void {
        if (changes.entities) {
            this.entitiesService.setEntities(this.entities);
        }
    }

    deactivateSelection() {
        this.allEntities.filter(field => this.listingService.isSelected(field)).forEach(field => (field.primaryAttribute = false));
        this.entitiesService.setEntities(this.allEntities.filter(field => !this.listingService.isSelected(field)));
        this.entitiesChange.emit(this.allEntities);
        this.listingService.setSelected([]);
    }

    setAttributeForSelection(attribute: string, value: any) {
        for (const item of this.listingService.selected) {
            this.allEntities.find(f => f.csvColumn === item.csvColumn)[attribute] = value;
        }
    }

    togglePrimary(field: IField) {
        if (field.primaryAttribute) {
            field.primaryAttribute = false;
            return;
        }

        for (const f of this.allEntities) {
            f.primaryAttribute = false;
        }
        field.primaryAttribute = true;
    }

    itemMouseEnterFn = (field: IField) => this.setHoveredColumn.emit(field.csvColumn);
    itemMouseLeaveFn = () => this.setHoveredColumn.emit();
}
