import { Component } from '@angular/core';
import { DetailsRadioOption, IconButtonTypes, IqserDialogComponent } from '@iqser/common-ui';
import { FormBuilder, UntypedFormGroup } from '@angular/forms';
import { RemoveAnnotationData, RemoveAnnotationResult } from '../../../utils/dialog-types';
import { getRemoveRedactionOptions, RemoveAnnotationOption, RemoveAnnotationOptions } from '../../../utils/dialog-options';

@Component({
    templateUrl: 'remove-annotation-dialog.component.html',
    styleUrls: ['remove-annotation-dialog.component.scss'],
})
export class RemoveAnnotationDialogComponent extends IqserDialogComponent<
    RemoveAnnotationDialogComponent,
    RemoveAnnotationData,
    RemoveAnnotationResult
> {
    readonly iconButtonTypes = IconButtonTypes;
    readonly options: DetailsRadioOption<RemoveAnnotationOption>[];
    readonly redactedTexts: string[];

    form!: UntypedFormGroup;

    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;
    }

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

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

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