import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { ReactiveFormsModule, Validators } from '@angular/forms';
import { FileAttributeEncodingTypes, IFileAttributesConfig } from '@red/domain';
import { fileAttributeEncodingTypesTranslations } from '@translations/file-attribute-encoding-types-translations';
import { BaseDialogComponent, CircleButtonComponent, IconButtonComponent } from '@iqser/common-ui';
import { NgForOf, NgIf } from '@angular/common';
import { TranslateModule } from '@ngx-translate/core';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatSelectModule } from '@angular/material/select';
import { MatSlideToggleModule } from '@angular/material/slide-toggle';

@Component({
    templateUrl: './file-attributes-configurations-dialog.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush,
    standalone: true,
    imports: [
        ReactiveFormsModule,
        MatSlideToggleModule,
        NgIf,
        TranslateModule,
        MatFormFieldModule,
        MatSelectModule,
        NgForOf,
        IconButtonComponent,
        CircleButtonComponent,
    ],
})
export class FileAttributesConfigurationsDialogComponent extends BaseDialogComponent {
    readonly encodingTypeOptions = Object.keys(FileAttributeEncodingTypes);
    readonly translations = fileAttributeEncodingTypesTranslations;
    readonly #configuration = inject<IFileAttributesConfig>(MAT_DIALOG_DATA);

    constructor(protected readonly _dialogRef: MatDialogRef<FileAttributesConfigurationsDialogComponent, IFileAttributesConfig>) {
        super(_dialogRef, true);
        this.form = this.#getForm();
        this.initialFormValue = this.form.getRawValue();
    }

    get disabled() {
        if (!this.changed) {
            return true;
        }

        if (!this.form.get('supportCsvMapping').value) {
            return false;
        }

        return !this.valid;
    }

    save() {
        this._dialogRef.close(this.#getConfiguration());
    }

    #getConfiguration(): IFileAttributesConfig {
        const supportCsvMapping = this.form.get('supportCsvMapping').value;
        if (supportCsvMapping) {
            return {
                ...this.#configuration,
                filenameMappingColumnHeaderName: this.form.get('keyColumn').value,
                delimiter: this.form.get('delimiter').value,
                encoding: this.form.get('encodingType').value,
            };
        }

        return {
            ...this.#configuration,
            filenameMappingColumnHeaderName: '',
        };
    }

    #getForm() {
        return this._formBuilder.group({
            supportCsvMapping: [!!this.#configuration.filenameMappingColumnHeaderName],
            keyColumn: [this.#configuration.filenameMappingColumnHeaderName || '', [Validators.required]],
            delimiter: [this.#configuration.delimiter || '', [Validators.required]],
            encodingType: [this.#configuration.encoding || FileAttributeEncodingTypes['UTF-8'], [Validators.required]],
        });
    }
}
