import { CdkFixedSizeVirtualScroll, CdkVirtualForOf } from '@angular/cdk/scrolling';
import { NgIf, NgStyle } from '@angular/common';
import { Component } from '@angular/core';
import { FormBuilder, ReactiveFormsModule, UntypedFormGroup } from '@angular/forms';
import { MatDialogClose } from '@angular/material/dialog';
import { DetailsRadioOption } from '@common-ui/inputs/details-radio/details-radio-option';
import { DetailsRadioComponent } from '@common-ui/inputs/details-radio/details-radio.component';
import { ReplaceNbspPipe } from '@common-ui/pipes/replace-nbsp.pipe';
import {
    CircleButtonComponent,
    HasScrollbarDirective,
    HelpButtonComponent,
    IconButtonComponent,
    IconButtonTypes,
    IqserDialogComponent,
} from '@iqser/common-ui';
import { TranslateModule } from '@ngx-translate/core';
import { getRemoveRedactionOptions } from '../../../utils/dialog-options';
import { RemoveAnnotationData, RemoveAnnotationOption, RemoveAnnotationOptions, RemoveAnnotationResult } from '../../../utils/dialog-types';

@Component({
    templateUrl: 'remove-annotation-dialog.component.html',
    styleUrls: ['remove-annotation-dialog.component.scss'],
    standalone: true,
    imports: [
        ReactiveFormsModule,
        TranslateModule,
        CdkFixedSizeVirtualScroll,
        NgStyle,
        CdkVirtualForOf,
        ReplaceNbspPipe,
        DetailsRadioComponent,
        HasScrollbarDirective,
        IconButtonComponent,
        CircleButtonComponent,
        MatDialogClose,
        NgIf,
        HelpButtonComponent,
    ],
})
export class RemoveAnnotationDialogComponent extends IqserDialogComponent<
    RemoveAnnotationDialogComponent,
    RemoveAnnotationData,
    RemoveAnnotationResult
> {
    readonly iconButtonTypes = IconButtonTypes;
    readonly options: DetailsRadioOption<RemoveAnnotationOption>[];
    readonly redactedTexts: string[];

    form!: UntypedFormGroup;

    constructor(private readonly _formBuilder: FormBuilder) {
        super();
        this.options = getRemoveRedactionOptions(this.data, this.data.applyToAllDossiers, true);
        this.redactedTexts = this.data.redactions.map(annotation => annotation.value);
        this.form = this.#getForm();
    }

    get isFalsePositive(): boolean {
        return this.form.get('option').value.value === RemoveAnnotationOptions.FALSE_POSITIVE;
    }

    get #applyToAllDossiers(): boolean {
        const selectedOption = this.form.get('option').value.value;
        return selectedOption === RemoveAnnotationOptions.IN_DOSSIER || selectedOption === RemoveAnnotationOptions.FALSE_POSITIVE;
    }

    save(): void {
        this.dialogRef.close({ ...this.form.getRawValue(), applyToAllDossiers: this.#applyToAllDossiers });
    }

    #getForm(): UntypedFormGroup {
        return this._formBuilder.group({
            comment: [null],
            option: [this.options[0]],
        });
    }
}
